[Solved] Spring Boot @WebMvcTest vs @SpringBootTest

I have a simple health controller defined as follows:

public class AdminController {

    String serviceName;

    String getHealth() {
        return serviceName + " up and running";

And the test class to test it:

class AdminControllerTest {

    private MockMvc mockMvc;

    public void healthShouldReturnDefaultMessage() throws Exception {
                .andExpect(content().string(containsString("live-data-service up and running")));

When running the test, I’m getting the below error:



Field configuration in com.XXXX.LiveDataServiceApplication required a bean of type 'com.XXXXX.AppConfiguration' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Consider defining a bean of type 'com.XXXX.AppConfiguration' in your configuration.

Here is AppConfiguration.java defined in the same package as the main spring boot app class:

public class AppConfiguration {

    private String redisHost;

    private int redisPort;

    private String redisPassword;
// getters and setters come here

Main class:

public class LiveDataServiceApplication {

    private AppConfiguration configuration;

    public static void main(String[] args) {
        SpringApplication.run(LiveDataServiceApplication.class, args);

    public RedisConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration redisConfiguration = new RedisStandaloneConfiguration(configuration.getRedisHost(), configuration.getRedisPort());
        return new LettuceConnectionFactory(redisConfiguration);

If I modify the annotation in the test class as follows, the test pass:

class AdminControllerTest {

What am I missing?

Enquirer: belgoros


Solution #1:

You should understand the usage of @WebMvcTest and @SpringBootTest

@WebMvcTest : annotation is only to instantiates only the web layer rather than the whole context, so all dependencies in controller class should be mocked, you can look at the documentation

Spring Boot instantiates only the web layer rather than the whole context. In an application with multiple controllers, you can even ask for only one to be instantiated by using, for example, @WebMvcTest(HomeController.class).

We use @MockBean to create and inject a mock for the GreetingService (if you do not do so, the application context cannot start)

SpringBootTest : Spring boot test annotation actual load the application context for test environment

The @SpringBootTest annotation tells Spring Boot to look for a main configuration class (one with @SpringBootApplication, for instance) and use that to start a Spring application context.

Respondent: Deadpool

Solution #2:

Define all properties in src/test/resource/application.file
example to use junit 5 for rest layer:

public class RestTest {

    private RestClass  restClass;
    private MockMvc mockMvc;
    public void init() throws Exception {
        mockMvc = MockMvcBuilders.standaloneSetup(restClass).build();
    public void test() throws Exception {
        String url = "/url";
        ResultActions resultActions = mockMvc.perform(get(url));

Respondent: Manju D

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.