Loading
 
What's Next: View All Tutorial →

Learn Jmockit Tutorial - How to use it with Code coverage

JMockit is a Java toolkit for automated developer testing.   Features mocks static and final methods, constructors, and future instances mocks final classes, enums, annotations, and multiple interfaces mocks implementation classes gi...

Published Date: 17/02/2015  Last Modified Date: 17/02/2015    New Post



JMockit is a Java toolkit for automated developer testing.
 

Features

  • mocks static and final methods, constructors, and future instances
  • mocks final classes, enums, annotations, and multiple interfaces
  • mocks implementation classes given just the interface or base class
  • expressive recording & verification API, with elegant and consistent syntax
  • concise argument matchers and argument capturing
  • dependency injection for both unit and integration tests


Download jmockit-1.17.zip or use Maven:

<dependency>
   <groupId>org.jmockit</groupId> <artifactId>jmockit</artifactId> <version>1.17</version>
   <scope>test</scope>
</dependency>


Important Keywords to Know

  1. @MockUp                    - To mockup the class in state-based testing
  2. @Mock                        - To mock a method
  3. Expectation                 
  4. NonStrictExpectations
  5. VerificationsInOrder
  6. Deencapuslation           - To mock private method using reflecion utils
  7. @Mocked
  8. @Tested
  9. @Injectable
  10. @Capturing
  11. Invocations

Please refer the following articles, where we used and provide details abount the each keyword
 

Code Coverage with JMockit Plugin in Maven

 
<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jmockit</groupId>
            <artifactId>jmockit</artifactId>
            <version>1.17</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jmockit</groupId>
            <artifactId>jmockit-coverage</artifactId>
            <version>1.17</version>
            <scope>runtime</scope>
        </dependency>
        
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <systemPropertyVariables>
                        <coverage-outputDir>target/my-coverage-report</coverage-outputDir>
                        <!-- other properties, if needed -->
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
    </build>
 

Output

C:/my-app/target/my-coverage-report/index.html

Sample Output of the coverate Result
 
All Packages and Files
my-app\src\main\java, my-app\src
Packages: 1 Files: 4 Line
com.mycompany.app
App.java 0%
Car.java 0%
User.java 33%
Util.java 89%
37%
Total   37%

 Generated on Thu Apr 30 01:16:32 IST 2015


 
Awaiting for Administrator approval





Tags: JMockit JUnit

← Back to list


Related Post




×