【问题标题】:JUnit tests fail when run together, but pass individuallyJUnit 测试在一起运行时失败,但单独通过
【发布时间】:2015-11-06 21:54:28
【问题描述】:

我有一堆单独运行的 JUnit 测试。每一个都是真正的独立单元测试——被测的单个类。不需要上下文。我可以在 Eclipse 或通过 maven / surefire-plugin 单独或一起运行它们。

我已经添加了一个新的集成测试,它利用了 Spring 上下文等,并使用了 SpringJUnit4ClassRunner。一旦我将此测试添加到我的套件中,任何测试用例都会在此类失败后运行。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = IntegrationTestConfiguration.class)
@DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
@ActiveProfiles("test")
public class ImportServiceIntegrationTest {
   ...
}

我不确定这是否具有巨大的价值,但我也在此处发布我的配置类:

@EnableAutoConfiguration(exclude = { WebMvcAutoConfiguration.class,
        DispatcherServletAutoConfiguration.class,
        EmbeddedServletContainerAutoConfiguration.class,
        WebSocketAutoConfiguration.class })
@ComponentScan(basePackages = "com.rtc.synchronize",
        excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern="com\\.rtc\\.synchronize\\.config\\.AppConfig"))
@EnableJpaRepositories("com.util.veracode.rtc.synchronize")
@EntityScan("com.util.veracode.rtc.synchronize")
public class IntegrationTestConfiguration {
}

如果我的实际 @Configuration 课程有用,我也可以发布这些课程,但为简洁起见,我已避免使用它们(我不完全确定它们会有多大用处)。

我怀疑在测试类终止后,JVM 中维护了一些东西(一些静态数据)。

我正在使用带有以下配置的 Spring Cache 注释:

@Configuration
@EnableCaching(mode=AdviceMode.ASPECTJ)
public class CacheConfig extends CachingConfigurerSupport{

    /**
     * EhCache configuration.  Used to minimize calls to Veracode
     * 
     * @return
     */
    @Bean(destroyMethod="shutdown")
    public net.sf.ehcache.CacheManager ehCacheManager() {
        ...
        ...
    }
        ...
}

一旦我的集成测试类完成,我的后续测试就会抛出以下错误:

java.lang.IllegalStateException: The workItems Cache is not alive (STATUS_SHUTDOWN)
        at net.sf.ehcache.Cache$CacheStatus.checkAlive(Cache.java:4097)
        at net.sf.ehcache.Cache.checkStatus(Cache.java:2788)
        at net.sf.ehcache.Cache.get(Cache.java:1744)
        at org.springframework.cache.ehcache.EhCacheCache.get(EhCacheCache.java:65)
        at org.springframework.cache.interceptor.AbstractCacheInvoker.doGet(AbstractCacheInvoker.java:68)
        at org.springframework.cache.interceptor.CacheAspectSupport.findInCaches(CacheAspectSupport.java:461)
        at org.springframework.cache.interceptor.CacheAspectSupport.findCachedItem(CacheAspectSupport.java:432)
        at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:333)
        at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:299)
        at org.springframework.cache.aspectj.AbstractCacheAspect.ajc$around$org_springframework_cache_aspectj_AbstractCacheAspect$1$2bc714b5(AbstractCacheAspect.aj:74)
        at com.synchronize.repository.rtc.WorkItemRepositoryImpl.findById(WorkItemRepositoryImpl.java:192)
        at com.synchronize.repository.rtc.WorkItemRepositoryImpl.findById(WorkItemRepositoryImpl.java:192)
        at com.synchronize.repository.rtc.WorkItemRepositoryImpl.getState(WorkItemRepositoryImpl.java:179)
        at com.synchronize.repository.rtc.WorkItemRepositoryImplTest.testGetState(WorkItemRepositoryImplTest.java:178)

所以我很清楚,Spring 完成后并没有清理某些东西(我的后续课程甚至不加载 Spring 上下文 - 这是一个普通的 Junit 测试!)。

如果我将<resueForks>false</reuseForks> 添加到我的surefire-plugin 定义中,所有测试都会通过,但我对该解决方案/解决方法不满意。它减慢了构建速度,并且在 Eclipse 中不受尊重 - 也就是说,我只需一次针对整个项目运行 JUnit 测试运行程序而不会失败。

测试用例完成后,我是否必须做一些特别的事情来确保 Spring 将自己从 JVM 中清除?为什么我在集成测试后会有一些 Spring 配置?

【问题讨论】:

  • IntegrationTestConfiguration 中有什么?当您的测试完成时,Spring 上下文将被破坏,我怀疑您的 EhCache 也随之而来。
  • @codesalsa 我同意 - 理论上应该销毁上下文和所有相关的 Spring bean。那么为什么首先调用 Spring 缓存拦截器呢?我已经用额外的配置数据更新了我的帖子。
  • 问题是在您集成 Spring 之前发生的吗?如果是这样,您是在测试前使用 setup 还是 beforeClass 进行设置?
  • 我在我的测试套件中注意到一些基于 AspectJ 的实现(我在 Spring-Security 中观察到了这一点,但@Cache 可能存在类似的问题)具有引用一些 Spring bean 的静态字段。当配置文件甚至完整的弹簧测试上下文发生变化时,此引用不会更新。我的 Workarround 是将它们放在另一个导演 src/test/javaSecurity 中,然后显式配置此目录。而且我还使用命名模式,以便在单独的安全过程中运行它们
  • @samir.sales 抱歉 - 如果没记错的话,我最终使用了reuseForks 交付了测试套件,并随后转向了不同的项目。

标签: java spring junit aspectj spring-test


【解决方案1】:

我注意到在我的测试套件中,一些基于 AspectJ 的实现具有引用一些 spring bean 的静态字段,并且当配置文件甚至完整的 spring 测试上下文发生更改时,此引用不会更新 (我在 Spring-Security 中观察到了这一点,但@Cache 可能存在类似问题)

我的解决方法是通过在不同目录(例如src/test/javaSecurity)中使用的弹簧配置对测试用例进行分组,并使用命名模式将它们分开。 - 分开的目录/源文件夹用于eclipse,所以我可以点击目录根文件夹并指示eclipse运行这个源文件夹中的所有测试。 - 命名模式用于使用maven执行测试(因为surefire具有按类命名模式选择执行测试的功能,而不是根文件夹)

  • 目录/模式
    • src/test/java,模式:*Test - 包含没有 spring-security 的 spring 配置的测试
    • src/test/javaSecurity,模式:*SecurityTest 需要启用 Spring 安全性的 Spring 测试

行家

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>add-security-test-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-test-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${basedir}/src/test/javaSecurity</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>


<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <executions>
        <execution>
            <id>normal-test</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <excludes>
                    <exclude>**/Abstract*.java</exclude>
                    <exclude>**/*SecurityTest.java</exclude>
                </excludes>
                <includes>
                    <include>**/*Test.java</include>
                    <include>**/*Tests.java</include>
                </includes>
                <reportsDirectory>${project.build.directory}/surefire-reports/normaltest</reportsDirectory>
            </configuration>
        </execution>            
        <execution>
            <id>security-tests</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <excludes>
                    <exclude>**/Abstract*.java</exclude>
                </excludes>
                <includes>
                    <include>**/*SecurityTest.java</include>
                </includes>
                <reportsDirectory>${project.build.directory}/surefire-reports/security-test</reportsDirectory>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <!-- just needed to prevent some bugs -->
        <excludes />
        <includes>
            <include>nothing</include>
        </includes>
        <reportsDirectory>${project.build.directory}/surefire-reports/nothing</reportsDirectory>
    </configuration>
</plugin>

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多