Marco Marani
Marco Marani

Reputation: 91

"No qualifying bean of type 'org.springframework.web.context.WebApplicationContext' available" while testing a controller

Following many examples of RestController found on the Internet, I've set the test as follows, but Spring isn't able to autowire the WebApplicationContext:

@RunWith(SpringRunner.class)
@WebMvcTest(CollectionController.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = DefaultTestConfiguration.class)
public class CollectionControllerTest {

    private MockMvc mvc;

    @Autowired
    private WebApplicationContext webApplicationContext;
    
    @Before
    public void setUp() {
        mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Test
    public void postTest() throws Exception {
        mvc.perform(post("/collection/")
                  .contentType(MediaType.APPLICATION_JSON))
                  .andExpect(status().isOk());
    }
}

The same happens with MockMvc if I try to autowire it instead of setting it explicitly in the @Before method.

My configuration class:

@Configuration
public class DefaultTestConfiguration {
    @Bean
    public ICollectionService collectionService() {
        return new MockCollectionService();
    }
}

The controller:

@RestController
@RequestMapping("/collection")
public class CollectionController {
    
    private ICollectionService collectionService;
    
    @Autowired
    public CollectionController(ICollectionService collectionService) {
        super();
        this.collectionService = collectionService;
    }

    @RequestMapping(value = "/", method=RequestMethod.POST)
    public CreateCollectionDTO createCollection(@RequestParam String collectionId) {
        return new CreateCollectionDTO(collectionService.createCollection(collectionId));
    }
}

and my spring dependencies in pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

Upvotes: 6

Views: 6751

Answers (1)

Marco Marani
Marco Marani

Reputation: 91

Finally I find it myself. The issue was with the @ContextConfiguration statement, more precisely: the value AnnotationConfigContextLoader.class for the loader parameter was preventing the WebApplicationContext to be instantiated.

After removing the "loader" parameter, everything went fine: the @Bean methods of my configuration class where properly executed and the MockMvc was created by simply Autowiring it (no @Before method needed).

Upvotes: 3

Related Questions