JMockit An automated testing toolkit for Java

Features

Example

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

   @Test
   void doOperationAbc(@Mocked 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

In a Maven pom.xml file:

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

In a build.gradle file:

testImplementation 'org.jmockit:jmockit:1.xy'