What is JMockit?
JMockit is open source software unit testing library, It includes APIs for mocking, faking, and combining with a code coverage tool. This library is used together with a testing framework such as JUnit or TestNG. IntelliJ IDEA is a good ide for testing with JMockit.
From https://jmockit.github.io/ site, JMockit is written with following features:
- out-of-container integration testing for Java EE and Spring-based apps
- mocking API with recording & verification syntax
- faking API for replacing implementations
- code coverage tool
Maven Dependency
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>1.41</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
Concepts and Structure of Jmockit test function:
@Test
public void testWireframe() {
// preparation code for inputting
..
new Expectations() {{
// define expected behaviour for mocks and result
}};
// execute code-under-test
//verify
new Verifications() {{
// verify mocks
}};
// assertions
}
Note that, The Assertion is used to validate critical functionality. If this validation fails, then the execution of that test method is stopped and marked as failed.
In Verification, the test method continues the execution even after the failure of an assertion statement. The test method will be marked as failed but the execution of remaining statements of the test method is executed normally.
@Mocked annotation will create mocks for every instance of the class of the annotated field
@Injectable annotation will create field variable in test class. These variables will be used in building test functions
@Tested annotation is marked for test object
Delegate interface is used for defining more complex return value when defining mocked method.
example:
@Test
public void testDelegate(@Mocked ExpectationsCollaborator mock) {
DeviceBean deviceBean = new DeviceBean();
new Expectations() {{
..
result = new Delegate() {
int delegate(int i) throws Exception {
if (booleanExpression1()) {
return 10;
} else if (booleanExpression2()) {
return 1;
} else {
throw new ServerSideException();
}
}
};
}};
assertEquals("Should return 10", 10, test1.getAllDevice(deviceBean));
}
JMockit can be used to test in all layer of Java Project, from Controller layer to DAO layer. It is easy library but so effective. You can apply it immediately after reading a test example using jmockit.
I'm cheerful I found this blog! Every now and then, understudies need to psychologically the keys of beneficial artistic articles forming. Your information about this great post can turn into a reason for such individuals.
ReplyDeleteServicenow Training in Hyderabad