Skip to main content
Version: 10.x

autoparams-mockito

autoparams-mockito is an extension of AutoParams that enables the automatic creation of test doubles for interfaces and abstract classes using Mockito, a widely used mocking framework for Java. With this extension, AutoParams can generate test doubles seamlessly—with minimal setup.

Install

Maven

For Maven, you can add the following dependency to your pom.xml:

<dependency>
<groupId>io.github.autoparams</groupId>
<artifactId>autoparams-mockito</artifactId>
<version>10.2.0</version>
</dependency>

Gradle (Groovy)

For Gradle Groovy DSL, use:

testImplementation 'io.github.autoparams:autoparams-mockito:10.2.0'

Gradle (Kotlin)

For Gradle Kotlin DSL, use:

testImplementation("io.github.autoparams:autoparams-mockito:10.2.0")

Generating Test Doubles with Mockito

Suppose you have an interface that represents a dependency:

public interface Dependency {

String getName();
}

And a system under test that relies on this dependency:

public class SystemUnderTest {

private final Dependency dependency;

public SystemUnderTest(Dependency dependency) {
this.dependency = dependency;
}

public String getMessage() {
return "Hello " + dependency.getName();
}
}

By using the @Customization(MockitoCustomizer.class) annotation, AutoParams will automatically generate Mockito-based test doubles for eligible parameters (such as interfaces and abstract classes).

Here’s an example:

@Test
@AutoParams
@Customization(MockitoCustomizer.class)
void testMethod(@Freeze Dependency stub, SystemUnderTest sut) {
when(stub.getName()).thenReturn("World");
assertEquals("Hello World", sut.getMessage());
}

In this test:

  • stub is a test double automatically generated by Mockito.
  • The @Freeze annotation ensures that the same stub instance is injected into the SystemUnderTest.
  • You can configure the test double using standard Mockito syntax.

This integration streamlines test setup, enabling you to focus on verifying behavior rather than wiring dependencies manually.