Monday, 16 September 2019

after added @EnableWebMvc my test with @DataMongoTest stop working.

properties = {"webEnvironment=false" },after added @EnableWebMvc my test with @DataMongoTest stop working, i got below error:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.servlet.ServletContext' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}


Frist, @DataMongoTest is very special it stops all auto wire.

and you cannot use @springboottest with it, because it extended @springboottest


i have tried :


  • properties = {"webEnvironment=false" },
  • @EnableAutoConfiguration(exclude= {WebMvcAutoConfiguration.class, SecurityAutoConfiguration.class, ServletManagementContextAutoConfiguration.class})
  • add @WebAppConfiguration
  • @Profile("!test") in the class with  @EnableWebMvc 

none of them worked.


my solution is:

@DataMongoTest(
includeFilters = @ComponentScan.Filter({Service.class, Component.class, Configuration.class}),
excludeFilters=@ComponentScan.Filter({EnableWebMvc.class, EnableSwagger2.class})
)

1 comment:

  1. @Configuration
    @EnableSwagger2
    @Profile("!test")
    public class SwaggerConfig { - this worked by excluding in the test profile.

    ReplyDelete