AdBlock Detected

It looks like you're using an ad-blocker!

Our team work realy hard to produce quality content on this website and we noticed you have ad-blocking enabled. Advertisements and advertising enable us to continue working and provide high-quality content.

@MockBean vs @Mock

In this post, we are going to see the usage of @MockBean vs @Mock in Spring Boot. When creating the testing part of our Spring Boot applications, we sometimes wonder whether to use Mockito.mock() or @Mock or make use of @MockBean. In this article, we will see which one to apply based on our needs.

Both annotations are used for testing. @Mock belongs to the Mockito framework, and @MockBean belongs to the Spring framework.

What is Mockito?

Mockito is an open-source Java framework released under the MIT license. Mockito allows us to create and simulate objects for our automated tests

Annotation @Mock and Mockito.mock()

@Mock is the annotation or shorthand for Mockito.mock(). It allows us to create objects that mimic a class or interface.

Both @Mock and Mockito.mock() will return a value or object when invoked.

When using @Mock, we should consider that we can only use it in test classes, whereas Mockito.mock() can be applied in other parts. By using the Spring annotation @SpringBootTest, the annotations for @Mock are activated.

@SpringBootTest
public class UserRepositoryUnitTest {
    
    @Mock
    UserRepository userMockRepository;
    
    @Test
    public void given_id_when_get_user_by_id_then_return_user_mocked() {
        Mockito.when(userMockRepository.findById(anyString())).thenReturn(userMocked());

        User user = userMockRepository.findById("1");

        Assert.assertEquals("1", user.getId());
        Assert.assertEquals("Pepe", user.getName());

        Mockito.verify(userMockRepository,times(1)).findById("1");
    }
}

The usage of Mockito.verify() allows us to check if the method has been executed and how many times.

Spring Boot’s MockBean Annotation

We use @MockBean in Spring Boot when we want to mock an object that is present in the Spring application context. @MockBean takes care of replacing the bean with what we want to simulate in our test.

For cases where we don’t have a bean defined of the type we are mocking with @MockBean, a new bean will be created.

What Spring does behind the scenes is load and register this new “bean” in the application context.

@SpringBootTest
public class UserRepositoryIT {
    
    @MockBean
    UserRepository userMockRepository;
    
    @Autowired
    UserService userService;
    
    @Test
    public void given_id_when_get_user_by_id_then_return_user_mocked() {
        Mockito.when(userMockRepository.findById(anyString())).thenReturn(userMocked());

        User user = userMockRepository.findById("1");

        Assert.assertEquals("1", user.getId());
        Assert.assertEquals("Pepe", user.getName());

        Mockito.verify(userMockRepository,times(1)).findById("1");
    }
}

In the previous case, we used MockBean to simulate the UserRepository bean. Additionally, we used Mockito.verify to check the number of times the findById method is called.

When to use @Mock or @MockBean?

The choice between @Mock and @MockBean depends on the specific scenario and requirements of your Spring Boot application’s testing.

  1. Use @Mock:
    • When you want to create a mock object or interface within a test class.
    • When you need to mock dependencies or collaborators explicitly within the scope of a particular test case.
    • When you want fine-grained control over the creation and behavior of the mock object.
  2. Use @MockBean:
    • When you want to mock a bean that is part of the Spring application context.
    • When you need to replace or simulate a specific bean with a mock implementation during integration or end-to-end testing.
    • When you want to test the interaction between your code and Spring beans, such as services or repositories.
    • When you want to simulate the behavior of third-party components or external dependencies within the application context.

In summary, use @Mock when you want to create mock objects within a test class, and use @MockBean when you want to mock or replace Spring beans in the application context during integration testing.

Conclusion

In this post, we have seen the usage of @MockBean vs @Mock in Spring Boot for our Spring Boot tests. The use of both @Mock and @MockBean is essential for performing unit tests on our application.

If you need more information, you can leave us a comment or send an email to refactorizando.web@gmail.com You can also contact us through our social media channels on Facebook or twitter and we will be happy to assist you!!

Leave a Reply

Your email address will not be published. Required fields are marked *