User:Meschwei/JMockit

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 (2015-08-23)
Preview release
1.20[2] / October 25, 2015 (2015-10-25)
Written inJava
Operating systemCross-platform
TypeUnit testing tool
LicenseMIT License
Websitejmockit.org

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]

History

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].

Features

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:

  1. Each class that needs to be mocked must have a separate interface implemented or must not be final.
  2. Tests will not be able to pass mock implementation of dependencies unless the dependencies are provided via a configurable instance creation method, or be exposed for dependency injection. [9]
  3. Only Non-final instance methods can be mocked, so classes that are being tested cannot call or instantiate any static or final methods of the class dependencies. [3]

Therefore JMockit has the following capabilities:

  • Mocks static and final methods, constructors and future instances
  • Mocks final classes, enums, annotation and multiple interfaces
  • Mocks implementation classes given just the interface or base class
  • Expressive recording and verification API
  • Concise argument matches and argument capturing
  • Dependency injection on both unit and integration tests [7]

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]

Usage

Comparison to Other Mocking Frameworks

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]

Examples

The following examples use JMockit with JUnit to illustrate some of JMockit's mocking capabilities.

Accessing Private Fields

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());
	}
}

Accessing Enums

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());
	}
}

See Also

References

Content Disclaimer

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.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.