在Spring Boot应用程序的单元测试和集成测试中禁用Spring Session

时间:2019-05-16 12:01:14

标签: spring-boot spring-boot-test spring-session

我有一个带有这个主要类的Spring Boot 1.5.21应用程序:

package com.myorg.myapp

@EnableSpringHttpSession
@EnableCaching
@SpringBootApplication
class MySpringBootApplication(private val env: Environment) {

我要在没有外部缓存配置的情况下启动集成(@DataJpaTest)测试。

package com.myorg.myapp.repository

@ExtendWith(SpringExtension::class)
@DataJpaTest
class MyRepositoryTest(@Autowired private val libraryRepository: LibraryRepository) {

    @Test
    fun shouldDoSomething() {
        ...
    }
}

问题是因为它找不到SessionRepository bean而引发了异常:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.session.SessionRepository<?>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1097)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1059)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    ... 63 more

@DataJpaTest内部扫描包含MySpringBootApplication批注的主类配置(@EnableSpringHttpSession)。这将导入需要SessionRepository bean的@Import(SpringHttpSessionConfiguration.class)

    @Bean
    public <S extends ExpiringSession> SessionRepositoryFilter<? extends ExpiringSession> springSessionRepositoryFilter(
            SessionRepository<S> sessionRepository) {

那么如何使我的@DataJpaTest类不初始化外部会话配置?

我尝试使用excludeFilter,但是没有用:

@DataJpaTest(excludeFilters = [ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value= [SpringHttpSessionConfiguration::class])])

0 个答案:

没有答案