JMockit An automated testing toolkit for Java

Features

See the Mocking Toolkit Comparison Matrix for more features.

Example

public class ExampleTest {
   @Tested ServiceAbc tested;
   @Injectable DependencyXyz mockXyz;

   @Test
   public void doOperationAbc(@Mocked final AnotherDependency anyInstance) {
      new Expectations() {{
         anyInstance.doSomething(anyString); result = 123;
         AnotherDependency.someStaticMethod(); result = new IOException();
      }};

      tested.doOperationAbc("some data");

      new Verifications() {{ mockXyz.complexOperation(true, anyInt, null); times = 1; }};
   }
}

Get JMockit

Download jmockit-1.21.zip or use Maven:

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