Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Jtest code in eclipse

subratdash8
subratdash8 Posts: 36

Hi,
I have generated test class using jtest IDE.
I want to run this code in my normal eclipse editor
How to do that?
How to use this sonarqube?

Best Answers

  • Andrey Madan
    Andrey Madan Posts: 388 ✭✭✭
    Answer ✓

    @subratdash8 : Which JTest version are you using? This is a very important piece of information we need to determine your next steps. Second question is what is the purpose of using SonarQube here? Display of test results and coverage in a dashboard? Correct?

  • subratdash8
    subratdash8 Posts: 36
    Answer ✓

    Hi @Andrey Madan
    I think it's 9.3.
    In our project currently we r using sonarqube to display code coverage and tesr case success rate to client.Recently we have started using JTest.
    So can we do that?
    Thanks in advance :smile:

  • Andrey Madan
    Andrey Madan Posts: 388 ✭✭✭
    Answer ✓

    @subratdash8 : JTest 9.3 is a pretty old version. If you generate tests using this version, you will have to use older maven/ant plugins to integrate with CI/CD process. How far have you advanced in using the JTest 9.3? JTest 9.x product line is not designed to submit data to SonarQube.
    The good news, the new JTest line of products which is giving you ability to not only create pure JUnits but also easily integrate with your CI/CD pipeline and publish results to a more advanced reporting and analytics ecosystem (DTP). Check out this awesome blog post from @Mark Lambert : https://blog.parasoft.com/why-people-hate-unit-testing-and-how-to-bring-back-the-love :). If you have further questions after reading and listening to the blog video, please contact your friendly Parasoft representative.

  • jakubiak
    jakubiak Posts: 813 admin
    Answer ✓

    Jtest 9 builds test cases that require Jtest libraries in order to run. It does not build test cases that use Mockito. Instead if you switch to Jtest 10, then you will be able to build maintainable test cases that run on their own without any Parasoft dependencies. The test cases built by Jtest 10 use Mockito for mocking.

«1

Answers

  • subratdash8
    subratdash8 Posts: 36

    @Andrey Madan thanks..
    But I have one doubt. After generating test classes in jtest IDE can I use same code in junit class in normal eclipse IDE.
    Currently we are using mockito framework to mock data service or any dependency call.In jtest I can see the stup is getting created for that call.
    But how to integrate this functionality in junit after creating stub code in jtest editor.
    Thanks in advance. :smile:

  • subratdash8
    subratdash8 Posts: 36

    @jakubiak
    Thanks..
    Can we use jtest library in junit test class??
    Thanks in advance. :smile:

  • Andrey Madan
    Andrey Madan Posts: 388 ✭✭✭

    @subratdash8 : It's just a library, so yes. However, I would strongly recommend upgrading to JTest 10 and Unit Test Assistant. The test cases created by UTA will be much easier integrated with your workflow (CI / Mockito, etc). You WILL save yourself a lot of headaches down the road.

  • jakubiak
    jakubiak Posts: 813 admin

    Do you mean to ask if you can use the Jtest 9 library in a JUnit test class when running in an IDE that does not have Jtest installed? I would suspect it is possible but I have not done it so I don't know how easy or if it is actually possible.

    Do you have existing test cases built with Jtest 9? Or are you just starting out in building test cases? Because if you are just starting out, you really want to use Jtest 10 - the test cases are MUCH cleaner and can be run independently of Parasoft.

  • maciekg
    maciekg Posts: 19 ✭✭
    edited August 2017

    Using Jtest 9.x library for test case execution.
    Generally, I could say that You need Jtest tool to correctly execute these test cases. These test cases are using few features like parameterization, stubs and other utilities to realize some functionality and should be executed by Jtest tool.
    Also Jtest tool is use to provide information about coverage.

  • subratdash8
    subratdash8 Posts: 36

    @jakubiak
    When you are saying jtest 9 or 10,what does it mean?
    It means only library like jar or it's version of editor that is used for jtest.
    Currently I am using parasoft jtest editor for that.
    I am confused what is actually jtest means.
    :neutral:

  • subratdash8
    subratdash8 Posts: 36

    @maciekg
    Thanks.
    Currently we are using maven and sonarqube for build and report generation to show code coverage.
    I don't have idea how to use this with jtest.
    I am new to this thing.
    Any pointers would be helpful for me.

  • maciekg
    maciekg Posts: 19 ✭✭

    By saying Jtest 9 vs 10 we say about tool - whole plugin to IDE.

    Jtest 9 was delivered only as plugin to Eclipse.
    Jtest 10 is devliery as pluign to Eclipse, IntelliJ or Netbeans.

    jtest.jar - is only small library which realize extra features in test cases generated by Jtest.

  • subratdash8
    subratdash8 Posts: 36

    Hi All,
    Finally I got jtest 10 version.. I am exploring its feature. One issue that I have noticed is
    For EJB object it is not creating stub method by itself. So it's initial with null
    So is it not possible for jtest to mock ejb object and if it is possible how to do that?
    Waiting for you response..

  • bmcglau
    bmcglau Posts: 14

    Depending on which action you used to create the test with Unit Test Assistant, objects like EJBs may not be instantiated by default. This gives you the opportunity, when building tests, to decide whether you want a real object or a mock in the test.

    If you select the local variable in the code editor, you can either instantiate it (using a constructor or factory method) or mock the object (using Mockito) using an action in the Unit Test Assistant view.

  • subratdash8
    subratdash8 Posts: 36

    Hi @bmcglau
    Thanks for your response. But as per jtest documentation if there is any other module call exist to do isolate test jtest will automatic mock dependent class objects.
    So why we can't do in case of EJB?
    Correct me if I am wrong.

  • subratdash8
    subratdash8 Posts: 36

    Hi @bmcglau
    Thanks for your response.
    When I am trying to create junit for 1 method I can see only 1 test methods getting formed. Buy we have different scenarios. So as per my understanding jtest should create junit based on different scenarios not only 1 method to cover only 1 scenario.
    Correct me if I am wrong *

  • bmcglau
    bmcglau Posts: 14

    Hi @subratdash8,

    In Jtest 9.x, generating tests did build multiple test methods for each method-under-test; with Jtest 10.x, we have moved away from this paradigm because it often resulted in incorrect and un-maintainable tests. Unit Test Assistant is designed to provide a guided test-creation workflow, where developers define the scenarios they need for each method. We have found that in nearly all cases, a using the guided test creation paradigm results in better, more maintainable tests than the all-automatic approach.

    I would also recommend reaching out directly to our support team or a Parasoft representative - we would be happy to take a closer look at what you are trying to accomplish and help you get there.

  • subratdash8
    subratdash8 Posts: 36

    Hi @bmcglau
    Thanks for your response.. I am arranging a meeting with jtest team. Before that I am just clarifying some open point so that I can explain to my team.
    So can we mock this type of object like ejb or if we are calling methods of different class then mocking of that function call part, by jtest instead manually after creating test methods.?
    I mean can we ask jtest to mock one class or one function call after its created jtest method?
    The answer to this question will solve our many doubts.
    Thanks in advance

  • bmcglau
    bmcglau Posts: 14

    Hi @subratdash8 ,
    Yes, you can mock an EJB and its methods. If you select a local variable that is initialized to null, you can use the Mock it action to instantiate the EJB as a mock. Alternatively, you can use Deep Setup to automatically mock the EJB and any method calls being made to it from the method under test.

    When tests are run, UTA will make recommendations for each method call on the mocked EJB, if the method is not yet mocked. There is a quick-fix link in the recommendation to automatically mock the method.

    Be sure to read the documentation on mocking here: https://docs.parasoft.com/display/JTEST1033/Creating+Mocks

  • subratdash8
    subratdash8 Posts: 36

    Hi @bmcglau
    I have read that documentation. But I can't see that mock option in recommendation tab. And there is only option for mockito. Power mockito option i cant see in preference. I can only see mock it option for getter and setter methods.
    And EJB object is initialized with null which is resulting in null pointer exception.
    Is any configuration missing from ur side?

  • jakubiak
    jakubiak Posts: 813 admin

    Hi @subratdash8,
    As suggested by @bmcglau, I would recommend you contact our support team as it looks like we are having a hard time understanding your use case on the forums. With that being said, here are some quick thoughts:

    • PowerMockito support was added in version 10.3.3 - what version are you using? Please note that the PowerMockito option appears under the Parasoft > Unit Test Assistant > Mocking preferences.
    • Quick fixes to add mocking for specific methods happens when your test already has a mocked object defined, and there are methods being called on that object for which there are no mocks configured.
    • If the EJB is initialized to null, then you can manually configure it to be initialized to something non-null. Unit Test Assistant has actions in the Unit Test Assistant view to help you with instantiating it. However, it's hard to explain exactly what Unit Test Assistant is doing, and how to work with it better, without seeing your exact use case. So please contact support, or consider posting sample code and the test that gets generated from it here.
  • subratdash8
    subratdash8 Posts: 36

    HI Everyone,
    We have installed 10.3.4 jtest. Now we are able use power mockito feature and we can mock static method. But there is still some issue.
    1.we are not able to still mock EJB object in this version. and if we are manually mocking EJB object still we can not see any recommendation to mock the function call made by that object.
    2.If we are using list of some data and we are passing to source method then after creating test method by jtest , we can see that Jtest is only creating a empty list without any content. So is it a expected behaviour and we need to add content or something is missing from our side
    3.If code contains some if else condition based on some scenario then Jtest is not covering all scenario.In this case do we need to manual modify value to cover all condition?

    Please let me know in case you need more details

    Thanks in advance

  • bmcglau
    bmcglau Posts: 14

    Hi @subratdash8 ,
    Regarding the EJB mocking issues, I would recommend contacting and working with our support team to look into the issue further. Perhaps there is something in your EJB or related code which we need to understand in order to provide a better answer.
    By default, Unit Test Assistant instantiates collections as empty lists and maps as empty HashMaps. This is expected behavior.
    Jtest does not currently analyze code branches to drive test creation. As the test developer, you would configure the test to take the code branches that you want it to take, in order to test a specific code-path.

  • subratdash8
    subratdash8 Posts: 36
    edited February 2018

    Hi @bmcglau and all
    Thnaks for your response. I have raised thia concerns to my tool team and they will contact jtest team soon.
    So now coming to list or map initialization and branch coverage..
    1.In jtest 10.3.2..we saw its creating mock of list or map.. And 10.3.4, it is creating a empty list and map.. So can we expect this feature in higher version.. Where jtest will analyze the list and it will add mock objects into that. This feature is basically required when list contains a user defined class with lot of properties and our functions invocation depend on this data.. So if jtest can analyze this and create mock 9f objects with some mock data and add it to list then it will reduce some more efforts given by developers.

    2.And same as for conditional scenarios also.. If higher version includes this..
    Waiting for your input and suggestions..
    Thanks

  • bmcglau
    bmcglau Posts: 14

    Hi @subratdash8 ,
    Yes, we intentionally changed our default instantiation of Collections and Maps (including Lists) to use concrete classes like ArrayList and HashMap instead of mocking them. This is because, in nearly all circumstances, the List or Map is not the object which needs to be mocked. However, when we generate tests we don't necessarily know what data you - the developer - want in the collection. In some tests, you probably want an empty collection, and in others you may want nulls or real objects, depending on the scenario you want to test. Instead of generating too much code (which quickly becomes unmanageable), we opted to initialize empty collections, and expect the developer to populate the collection. I should mention that you can easily create a local variable for an item in the list, and use UTA actions to instantiate or mock the item.
    We do eventually want to make improvements to UTA in which we are able to analyze code branches to generate more complete "deep setup" tests.

  • subratdash8
    subratdash8 Posts: 36

    Hi @bmcglau
    Thanks for you feedback.
    But one point i want to mention here.
    While creating test methods jtest is mocking al input l parameters. While writing test methods may be developer need empty or null or with some data added into the list.
    But anyhow we need to test scenarios where input list to function have at least one data or object for further operation which is more important in testing.
    So if jtest can create mock objects with mocking parameters and add into the list then later developer will write for null or empty.
    So if you can include at least one mock object in list then developer wll try for other scenarios in higher version of jtest then it will add new flavor.

    And for 2nd point waiting for new version of jtest f for condition coverage.

    Thanks

  • jakubiak
    jakubiak Posts: 813 admin

    Hi @subratdash8, thank you so much for your feedback. The suggestions that you are making directly relate to internal discussions we are having about how to make the created tests get better coverage. I have captured your feedback and we will use it as input to our decisions about what to do in the next release. I can't promise that we will include everything you are asking for in the next release - but we are working in this area and there will be improvements.

  • subratdash8
    subratdash8 Posts: 36

    Hi I have attched scrrenshot related to EJB Mock. Currently we are not able to mock EJB Object.

    Jtest is not mocking object which is being formed by EJB annotation not even in recommendation I can see that mock it option.

    Can anyone please have a look and let me know what is the issue?

    Let me know if any more details required

    Regards,
    Subrat

  • subratdash8
    subratdash8 Posts: 36

    Can anyone please help me out on above issue?

  • jakubiak
    jakubiak Posts: 813 admin

    Jtest does not currently have explicit support for the EJB annotations that are being used in your examples. However, Jtest Unit Test Assistant can still be useful for creating and updating tests even if it does not have specific support for a particular technology. I see from internal channels that you have filed a support case with Parasoft - we will be reaching out to you via our support team to set up a meeting with you to go through the specifics of your use case and how Jtest Unit Test Assistant can help you.

  • subratdash8
    subratdash8 Posts: 36

    Thanks @jakubiak for your response.
    So I am wondering if we will create mock ejb object manually and execute the source method, so does jtest figure it out and provide recommendation to mock the function call made by that particular EJB mock object?

    If that is possible then it can resolve our issue?

    Waiting for your response.
    Thanks in advance. :smile:

  • maciekg
    maciekg Posts: 19 ✭✭
    edited March 2018

    Hi Subrat

    There is no difference between mocks constructed by Jtest automatically and mocks created by user manually. If you are using same mock framework writing them manually or Jtest assist actions to make them semi-automatically.
    Next during execution if Jtest detect interaction with mock - it will catch this and propose for you mock methods used during test scenario. This way finalize control over test case.
    Please try to verify this in your case.
                                                                                                 regards Maciek
    
  • subratdash8
    subratdash8 Posts: 36

    HI @maciekg @jakubiak ,
    I have tried mocking ejb object by @mock annotation using mockito-all-1.10.19 lib provided by jtest itself.
    I can see while debugging ejb object is going as mock instance. and not as null.
    But jtest is not giving any suggestion for object which is mocked by @mock annotation.
    Instead of that the object which is being mocked by "mock(Class.class)", jtest is giving suggestion.
    So I think may be jtest is not able to idenitify the mocking that is done by annotation.
    I have attched acreenshot with code snippet. Please have a look and provide your feedback on same.
    Thanks,
    Subrat

  • subratdash8
    subratdash8 Posts: 36
    edited March 2018

    Please find snapshot while debugging showing value of ejb object by mocking and other one after running test method by UTA showing recommendation.

    let me know if any more detail required

    Thanks,
    Subrat

  • subratdash8
    subratdash8 Posts: 36

    Hi all,
    Can anyone please provide the update on below query?

    Thanks
    Subrat

  • subratdash8
    subratdash8 Posts: 36

    HI @bmcglau ,
    Thanks a lot for your response. Is there any update, when we can expect higher version of UTA and what all new feature we can get in that version?
    We are in the way to use this tool in our existing project. We are really interested in Jtest. But unfortunately because of some limitation we are not able to roll out in our project yet. So I need update on Jtest so that I can inform my team on same and we can wait for this. :smile:
    Waiting for your response.
    Regards,
    Subrat

  • jakubiak
    jakubiak Posts: 813 admin

    The main focus of our work for the next version is supporting IntelliJ within the Unit Test Assistant. There will also be some improvements in how the created test cases exercise different parts of the code under test. For more information on specific features or release dates, please contact your Parasoft representative. If you have specific requests for functionality that would help support your use cases, please connect with your Parasoft representative for that as well.

  • subratdash8
    subratdash8 Posts: 36

    HI team,

    Thanks to all for all responses to my query. As per last discussion ,EJB mocking issue will be resolved by introducing @Inject Mock and @mock feature in next version of tool. We are waiting for that tool .

    Now we have started POC on micro service using spring boot. As we discussed before in this thread I got to know Jtest wont support @EJB mocking as its old technology, but it supports Spring .

    Will Jtest mock the class if its annotated with @autowired or any kind of annotation to inject the dependecy, while generating Junit through Jtest tool.?

    Please let me know if any code snippet required

    Regads,
    Subrat

  • jmadru
    jmadru Posts: 3

    Yes, Jtest supports setting up dependencies in spring tests that are annotated with @autowired and will mock them if mocking is enabled in preferences. Here's the relevant section in our docs:

    Unit Test Assistant allows you to create and execute JUnit tests for Spring controller methods that implement HTTP endpoints using Web MVC. By analyzing code, it can generate and preconfigure runnable test templates that contain sample assertions and initial values. UTA can recognize and configure settings for request body, response body, response mime types, HTTP method, path parameters, query parameters, and HTTP headers. It supports Spring dependency injection through autowired fields, setter methods, and constructor arguments.

    Tests for handler methods in Spring MVC controllers are automatically run as Spring JUnit tests. The tested handler method is invoked with MockMvc, and an inner class is configured to provide beans to Spring for necessary dependencies. If the Mock option is enabled (see Configuring Preferences), all controller dependencies are added as mocked beans to the inner configuration class.

  • subratdash8
    subratdash8 Posts: 36

    Hi @jmadru,
    Thanks for response. Can you please provide link of this details, so that I can go through it.

    Because when I tried creating junit with tool in spring boot, it didn't create any mock objects for class annotated with @autowired. So I will verify if anything I am doing wrong.

    Pfb sudo code. Details of code snippet I will share if you need.

    @controller
    Public class Demo{

    @autowired
    private Dependency object;
    
    
    public void run{
    object.doSomething();
    

    }
    }

    So for this kind of code will jtesr help in writing junit with all mocking of dependency class annotated with @autowired.

    Please let me. Waiting for update. Thanks in advance

    Regarda,
    Subrat

  • jmadru
    jmadru Posts: 3

    You can find the link to the docs here. To create Spring tests you must have the Spring license feature.

    Using your example code, I generated a Spring test from Demo.run() and got the following test:

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest
    @AutoConfigureMockMvc
    @DirtiesContext
    public class DemoTest {
        @Autowired
        Demo controller;
    
        @MockBean
        Dependency dependency;
    
        @Test
        public void testRun() throws Throwable {
            controller.run();
        }
    }
    
  • subratdash8
    subratdash8 Posts: 36

    Hi @jmadru ,

    I have few question on this.

    1. Jtest genrated junit method will run always as spring boot. Is it possible to run it as normal class without running as spring boot?
    2. Jtest is able to identify only controller method in spring annotated with @controller or @restController. What about @repository and @Service annotated class, will it perform same for that class as well? Because I am able to use mocking of spring dependency if its a controller annotated class. But for @service and @repository, it is not mocking @autowired class. and default value is set as null same as

    Let me know if anything I missed here or any configuration is missing.

    Note: In spring @controller, @service, @repository all behave same but with different work. So we are dividing our code base like rest layer, business logic layer, database logic layer respectively. And in each layer we are using @autowired annotation for dependency injection and expecting Jtest should behave for all class same as
    @controller.

    But what Jtest is doing for Controller class that is really a good thing and will helpful as well :smile:

    Regards,
    Subrat

  • jmadru
    jmadru Posts: 3

    Jtest genrated junit method will run always as spring boot. Is it possible to run it as normal class without running as spring boot?

    Tests generated as spring tests will always run in the spring context. One workaround would be to remove the spring annotations on the test. We are working on a feature for Jtest 10.4.2 where you can decide to either generate a regular junit or a spring integration test.

    Jtest is able to identify only controller method in spring annotated with @controller or @restController. What about @repository and @Service annotated class, will it perform same for that class as well?

    As a workaround, you can change the test class to be a Controller, generate the test, and then change it back to Repository/Service. As part of Jtest 10.4.2 we will support generating tests from Repository/Service classes similar to how Controller tests are currently generated.

  • subratdash8
    subratdash8 Posts: 36

    Hi @jmadru,
    If I remove the spring annotations, then while running junit will it consider mockbean annotated class as mocked one and behave same or it will be simple plain juni code without any spring annotations

  • bmcglau
    bmcglau Posts: 14

    Hi @subratdash8,
    @MockBean is an annotation that the Spring Junit runner (SpringRunner, SpringJunit4ClassRunner, etc) looks for and sets up mocks for. If the test does not use this runner (or another runner is used and does not delegate to a Spring runner) then these mocks will not be set up automatically.
    When you remove the Spring annotations from your class, then generate a test, the test will not be generated with any Spring annotations such as SpringRunner or MockBean. If you need dependencies set up, you can mock them manually and use Spring's ReflectionUtils to inject the mock into the object under test.

  • subratdash8
    subratdash8 Posts: 36

    Hi @bmcglau,
    Thanks for your response. I will try that as well. and update here if any issues.

    I have some other query on use of jtest. Currently we are generating rest layer through maven archetype and swagger code gen tool and in that we are generating rest layer with all necessary details automatically based on service uri and input parameter getting from swagger yaml file.

    I am wondering is it possible to integrate jtest with maven or archetype somehow and tell jtest framework to generate 1st part of junit same way how we do from ide with all required details.

    Is there any way whete we can integrate jtest with maven and then from command prompt.

    Waiting for answer. Thanks in advance

    Regards
    Subrat

  • jakubiak
    jakubiak Posts: 813 admin

    Hi @subratdash8 - Jtest currently cannot create unit tests from CLI or maven, although it is something we have considered. It's not currently on the roadmap but we may do something like this in the future.

  • subratdash8
    subratdash8 Posts: 36

    Hi,
    Thanks for answering to all my questions. It is helping a lot.

    I need to know one thing. There are 3 options i can see while using jtest license.
    One is desktop edition, automation and final is custom edition.

    What is the difference between these and which one is better?
    I have read the documentation but didn't get clear idea.

    Regards
    Subrat