Spring Top – Temp

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:

Lightrun – Third Party Code

We rely on other people’s code in our own work. Every day. It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production - debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky. It’s difficult to understand what talks to what and, specifically, which part of the underlying library is at fault.

Lightrun is a new kind of debugger.

It's one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics. No hotfixes, redeployments, or restarts required.

Learn more in this quick, 5-minute Lightrun tutorial:

>> The Essential List of Spring Boot Annotations and Their Use Cases

1. Overview

In this quick tutorial, we'll look at three different ways of creating mock objects with Mockito and with the Spring mocking support. We'll also discuss how they differ from each other.

Further reading:

Mockito ArgumentMatchers

Learn how to use the ArgumentMatcher and how it differs from the ArgumentCaptor.

Mocking Exception Throwing using Mockito

Learn to configure a method call to throw an exception in Mockito.

2. Mockito.mock()

The Mockito.mock() method allows us to create a mock object of a class or an interface.

We can then use the mock to stub return values for its methods and verify if they were called.

Let's look at an example:

public void givenCountMethodMocked_WhenCountInvoked_ThenMockedValueReturned() {
    UserRepository localMockRepository = Mockito.mock(UserRepository.class);

    long userCount = localMockRepository.count();

    Assert.assertEquals(111L, userCount);

We don't need to do anything else to this method before we can use it. We can use it to create mock class fields, as well as local mocks in a method.

3. Mockito's @Mock Annotation

This annotation is a shorthand for the Mockito.mock() method. It's important to note that we should only use it in a test class. Unlike the mock() method, we need to enable Mockito annotations to use this annotation.

We can do this either by using the MockitoJUnitRunner to run the test, or by calling the MockitoAnnotations.initMocks() method explicitly.

Let's look at an example using MockitoJUnitRunner:

public class MockAnnotationUnitTest {
    UserRepository mockRepository;
    public void givenCountMethodMocked_WhenCountInvoked_ThenMockValueReturned() {

        long userCount = mockRepository.count();

        Assert.assertEquals(123L, userCount);

Apart from making the code more readable, @Mock makes it easier to find the problem mock in case of a failure, as the name of the field appears in the failure message:

Wanted but not invoked:
-> at org.baeldung.MockAnnotationTest.testMockAnnotation(MockAnnotationTest.java:22)
Actually, there were zero interactions with this mock.

  at org.baeldung.MockAnnotationTest.testMockAnnotation(MockAnnotationTest.java:22)

Furthermore, when used in conjunction with @InjectMocks, it can reduce the amount of setup code significantly.

4. Spring Boot's @MockBean Annotation

We can use the @MockBean to add mock objects to the Spring application context. The mock will replace any existing bean of the same type in the application context.

If no bean of the same type is defined, a new one will be added. This annotation is useful in integration tests where a particular bean, like an external service, needs to be mocked.

To use this annotation, we have to use SpringRunner to run the test:

public class MockBeanAnnotationIntegrationTest {
    UserRepository mockRepository;
    ApplicationContext context;
    public void givenCountMethodMocked_WhenCountInvoked_ThenMockValueReturned() {

        UserRepository userRepoFromContext = context.getBean(UserRepository.class);
        long userCount = userRepoFromContext.count();

        Assert.assertEquals(123L, userCount);

When we use the annotation on a field, the mock will be injected into the field, as well as being registered in the application context.

This is evident in the code above. Here we used the injected UserRepository mock to stub the count method. Then we used the bean from the application context to verify that it is indeed the mocked bean.

5. Conclusion

In this article, we examined how the three methods for creating mock objects differ, and how we can use each of them.

The source code that accompanies this article is available over on GitHub.

Spring bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Junit footer banner
Inline Feedbacks
View all comments
Comments are closed on this article!