Contents

Complete guide of creating mocks with mockito

Introduction

Mockito is an awesome framework that offers multiple ways to create a mock instance. Each way has its pros and cons. In this post, I will show you every way (that I know of) to create a mock instance.

For the examples, I will use this small Car class. It has everything we need to create a mock instance of it.

1
public class Car { }

I used Mockito version 4.6.1 for the examples. You can find the Maven dependency below. The Junit4 examples use an older Mockito version that you can find later in the article.

1
2
3
4
5
6
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>4.6.1</version>
    <scope>test</scope>
</dependency>

Using Mockito.mock()

The Mockito.mock(Car.class) method is the most straightforward way to create a mock object of a class or interface. We can use the object it creates to alter the behavior of the methods and how it is used.

This is an example of how you could use the method:

1
2
3
4
5
6
7
8
class UsingMockitoCalls {
    
    @Test
    void simpleTest() {
        Car car = Mockito.mock(Car.class);
    }

}

But we can make this example a little less verbose and more compact.

Using a static import

When you include the following static import we can call the mock() method directly.

1
import static org.mockito.Mockito.mock;

With the static import included, we can drop the Mockito. from line 5 and use the method directly to create a mock for us.

1
2
3
4
5
6
7
8
class UsingMockitoCalls {
    
    @Test
    void simpleTest() {
        Car otherCar = mock(Car.class);
    }

}

Using @Mock and Junit5

We can combine the power of Junit with Mockito. To do that, we need to add this dependency:

1
2
3
4
5
6
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-junit-jupiter</artifactId>
    <version>4.6.1</version>
    <scope>test</scope>
</dependency>

With the mockito-junit-jupiter dependency included, we can use the @ExtendWith(MockitoExtension.class) annotation. This will create a mock for every field in your test class that is annotated with @Mock. This increases the readability of your test class because you don't have to call mock() for each mock anymore.

Below you will see an example of using Junit5 with Mockito:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
@ExtendWith(MockitoExtension.class)
public class UsingExtension {

    @Mock
    Car car;

    @Test
    void simpleTest(){
        // testing code
    }
    
}

Using @MockitoSettings

@MockitoSettings extends the MockitoExtension.class, meaning it can do the same and more. It will create a mock for each @Mock annotated field in your test class.

With @MockitoSettings you can also change the strictness of your tests. The strictness helps you by giving hints to improve how you use your mocks. You can read more about it in this post about setting the strictness of Mockito

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
@MockitoSettings
class UsingMockitoSettings {

    @Mock
    Car car;

    @Test
    void simpleTest() {
        // testing code
    }

}

Using @Mock and Junit4

For the Junit4 examples, I these dependencies of Junit4 and Mockito:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>2.0.2-beta</version>
    <scope>test</scope>
</dependency>

As with Junit5, we can combine the power of Junit with Mockito. To use @Mock with Junit4, we need a different annotation above the class @RunWith(MockitoJUnitRunner.class). This will create a mock for every @Mock annotated field in your test class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@RunWith(MockitoJUnitRunner.class)
public class CarTest {
    
    @Mock
    public Car car;

    @Test
    public void test() {
        // testing code
    }
}

Using Junit rule

We can also use a @Rule to create mock objects for the @mock annotated field. This is handy for when you can't or don't want to use the Junit4 test runner.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public class CarTest {

    @Rule
    public MockitoRule rule = MockitoJUnit.rule();

    @Mock
    public Car car;

    @Test
    public void test() {
        // testing code
    }
    
}

Using @Mock without a test framework

You can also initialize all your annotated fields by calling the MockitoAnnotations.openMocks(this).close(); method. It behaves the same as @ExtendWith(MockitoExtension.class), it will create a mock for each annotated field in your test class.

In earlier versions of Mockito, you could also use MockitoAnnotations.initMocks(this);. The method is still available but marked as deprecated. Calling openMocks(...).close() is recommended and behaves mostly the same.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class WithoutARunner {

    @Mock
    Car car;

    @BeforeEach
    void setUp() throws Exception {
        MockitoAnnotations.openMocks(this).close();

        // Old and deprecated
        MockitoAnnotations.initMocks(this);
    }

    @Test
    void simpleTest() {
      // testing code
    }
    
}

Conclusion

This article looked at the possibilities available to create Mock objects and how to use them.

Further reading

More about testing in Java:

If you want to read more about Java when I write about it – you can follow me on Twitter.