How to use @MockBean Annotation

In this tutorial, you will learn how to use @MockBean annotation in your Unit tests.

@MockBean annotation is used to create and place mocks into Spring Application Contexts. If Spring Application Context does already have a bean of the same type then this bean will be replaced with a mock object. Otherwise, a new bean(mock) will be created and added to Spring Application Context.

As a Class-Level Annotation

@MockBean annotation can be used as a class-level annotation or as a field-level annotation. To use it as a class-level annotation:

  1. Place the @MockBean annotation right above the class name.
  2. Then inject the created mock object, using the @Autowired annotation as shown in the code example below.
@WebMvcTest(controllers = UsersController.class)
@MockBean({UsersService.class})
public class UsersControllerWebLayerTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired 
    private UsersService usersService;
    ...

}

Use @MockBeans for Multiple Mocks

It is possible that you will need to mock more than one bean. In this case, you will need to use the @MockBean annotation together with a @MockBeans annotation.

@WebMvcTest(controllers = UsersController.class)
@MockBeans({ @MockBean(UsersService.class), @MockBean(UsersRepository.class) })
public class UsersControllerWebLayerTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired 
    private UsersService usersService;

    @Autowired 
    private UsersRepository usersRepository;
    ...

}

As a Field-Level Annotation

Instead of placing @MockBean annotation above the class name, you can place it above the member variable. In this case:

  1. Spring Framework will create a new mock object and will add it to the Spring Application Context,
  2. Then, Spring Framework will automatically inject this mock object and will assign it to a member variable.
@WebMvcTest(controllers = UsersController.class)
public class UsersControllerWebLayerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    UsersService usersService;

    ...

}

If needed, Use with @Qualifier Annotation

When you use @MockBean annotation, Spring Framework will create a mock object of the requested type and place it into the application context. Now, it is possible that your application will have more than one bean of the same type in the Spring Application Context. In this case, use @Qualifier annotation to specify the bean you would like to mock and work with.

@WebMvcTest(controllers = UsersController.class)
public class UsersControllerWebLayerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    @Qualifier("usersService")
    UsersService usersService;

    ...

}

Video lessons

I hope this tutorial was of some value to you. If you like learning by watching a series of step-by-step video lessons then have a look at my video course called “Testing Java with JUnit and Mockito“. This video course is for absolute beginners and you do not need to have any prior knowledge of testing Java applications to enrol.

Happy learning!