JMockit is a Java (programming language) testing toolkit released under an open source MIT License.[3] JMockit designed to include additional important
| JMockit | |
|---|---|
| Stable release | 1.19[1]
/ August 23, 2015 |
| Preview release | 1.20[2]
/ October 25, 2015 |
| Written in | Java |
| Operating system | Cross-platform |
| Type | Unit testing tool |
| License | MIT License |
| Website | jmockit |
JMockit is a Java (programming language) testing toolkit released under an open source MIT License.[3] JMockit designed to include additional important mocking and testing capabilities that were not present in other common mocking frameworks.[4] JMockit is compatible with common Java test automation frameworks like JUnit and TestNG. To achieve additional functionality, JMockit uses features common to debugging Java code to modify code in a JVM that is currently executing. This allows JMockit to temporarily change values of variables, methods, and more when running a test.[3]
JMockit was originally released on June 6th, 2009 as version 0.81 and has and extensive revision history that has been regularly updated since then. [5] It was primarily hosted on http://jmockit.googlecode.com[6] but was later moved to http://www.jmockit.org [7].
The JMockit toolkit contains many features similar to its competitors such as Mockito, Unitils Mock, and PowerMock.[8] However, it has three[8] unique features as well as a different approach to mocking that allows it to address the following three issues that other tool kits cannot address:
Therefore JMockit has the following capabilities:
JMockit also comes with a code coverage tool that contains a set of metrics that can cover purely quantitative data of the target code. This tool is useful for test driven development of code as well as writing tests for existing production code. [10]
JMockit is often compared to its contemporaries such as: Mockito, JMock, and EasyMock. It has been reported that JMockit is more powerful and feature rich than these other mocking frameworks.[8] However, discussions have also suggested that JMockit is harder to use and has a steeper learning curve.[11]
The following examples use JMockit with JUnit to illustrate some of JMockit's mocking capabilities.
Consider the following class that one may wish to test.
public class Cat {
private String color;
public Cat() {
color = "grey";
}
public String toString() {
return "This cat is " + color + ".";
}
}
Using JMockit, one is able to mock the private field called from the toString() method with a JUnit 4 test.
public class CatTest {
@Test
public void test() {
Cat cat = new Cat();
assertEquals("This cat is grey.", cat.toString());
Deencapsulation.setField(cat, "color", "orange");
assertEquals("This cat is orange.", cat.toString());
}
}
JMockit is a notable for being mocking framework which can mock Enums.[12] The following shows an Enum that represents different cat breeds.
enum Breed {
BENGAL("Bengal"), BOMBAY("Bombay"), BRITISH_SHORTHAIR("British Shorthair");
private final String str;
private Breed(String str) {
this.str = str;
}
public String getStr() {
return str;
}
}
Using JMockit, it is possible to mock out various parts of the Enum.
public class BreedTest {
@Test
public void test(@Mocked final Breed mock) {
new Expectations() {
{
mock.getStr();
result = "bengal";
}
};
assertEquals("bengal", Breed.BENGAL.getStr());
}
}
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.