其他分享
首页 > 其他分享> > 春天-防止@EnableWebMvc注释的类被@ComponentScan拾取

春天-防止@EnableWebMvc注释的类被@ComponentScan拾取

作者:互联网

我有以下测试课:

@ActiveProfiles({ "DataTC", "test" })
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {BaseTestConfiguration.class, DataTestConfiguration.class, JpaConfiguration.class, PropertyPlaceholderConfiguration.class })
public class RegularDayToTimeSlotsTest {

    private static int NUMBER_OF_REGULAR_DAY_TO_TIME_SLOTS_IN_WEEK = 25;

    @Autowired
    private AdvertisementService advertisementService;

    @Test
    public void shouldNotContainSaturdayNorSunday() {
        Set<DayToTimeSlot> regularDayToTimeSlots = advertisementService.retrieveRegularDayToTimeSlots();
        assertThat(regularDayToTimeSlots).onProperty("day").excludes(Day.SATURDAY, Day.SUNDAY);
    }

    @Test
    public void shouldNotContainEveningNorNighttime() {
        Set<DayToTimeSlot> regularDayToTimeSlots = advertisementService.retrieveRegularDayToTimeSlots();
        assertThat(regularDayToTimeSlots).onProperty("timeSlot").excludes(TimeSlot.EVENING, TimeSlot.NIGHTTIME);
    }

    @Test
    public void shouldContainCorrectNumberOfDayToTimeSlots() {
        Set<DayToTimeSlot> regularDayToTimeSlots = advertisementService.retrieveRegularDayToTimeSlots();
        assertThat(regularDayToTimeSlots).hasSize(NUMBER_OF_REGULAR_DAY_TO_TIME_SLOTS_IN_WEEK);
    }
}

这是一个集成测试,不需要任何ServletContext.这是BaseTestConfiguration类:

@Configuration
@ComponentScan(basePackages = { "com.bignibou" }, excludeFilters = { @Filter(type = FilterType.CUSTOM, value = RooRegexFilter.class),
        @Filter(type = FilterType.ANNOTATION, value = Controller.class), @Filter(type = FilterType.ANNOTATION, value = ControllerAdvice.class),
        @Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) })
public class BaseTestConfiguration {

}

这只是一个Java配置类,旨在确保我的所有应用程序Bean均已实例化并正确连接,但不会考虑使用@EnableWebMvc注释的类,因为该集成测试不需要Servlet上下文. .

不幸的是,无论如何,我都会尝试排除Web / Mvc配置类,但是它无法正常工作,并且还使用了带@EnableWebMvc注释的@Configuration类.

我得到这个例外:

Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.<init>(DefaultServletHandlerConfigurer.java:54)
    at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:329)
    at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$d1263fa1.CGLIB$defaultServletHandlerMapping$22(<generated>)
    at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$d1263fa1$$FastClassByCGLIB$$e0423f09.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:326)
    at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$d1263fa1.defaultServletHandlerMapping(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
    ... 43 more

谁能帮忙吗?

解决方法:

实际上,存在一种更干净的方法来配置此类集成测试.

答案在于不仅在类级别而且在包级别也要适当地分离关注点.

始终建议将实现类和配置类都驻留在程序包层次结构中,以允许适当分离关注点.特别是对于Web应用程序,强烈建议确保Web组件和Web配置驻留在专用的“ Web”程序包中.例如,您可以考虑类似于以下内容的程序包层次结构:

> com.example.domain
> com.example.service
> com.example.repository
> com.example.web

通过这样的层次结构,您可以通过仅包括与当前应用程序或测试方案相关的那些基本软件包来简化组件扫描配置.通过这样做,通常不需要对不需要的东西使用排除过滤器.相反,只需指定您想要的软件包.

顺便说一句,将“ com.bignibou”指定为您的基本软件包实际上是最糟糕的做法,因为它包罗万象.

因此,使用显式包包含项(而不是排除@ Controller,@ ControllerAdvice等)进行尝试,然后看看这是否会使您的生活变得更轻松.

标签:spring-test,spring
来源: https://codeday.me/bug/20191122/2057564.html