JUnit测试在一起运行时失败,但是单独传递

Eri*_* B. 11 java junit spring aspectj spring-test

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

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

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

我不确定这有什么价值,但我也在这里发布我的配置类:

@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 {
}
Run Code Online (Sandbox Code Playgroud)

如果我的实际@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() {
        ...
        ...
    }
        ...
}
Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

所以我很清楚Spring在完成之后并没有清理它(我的后续类甚至没有加载Spring上下文 - 它是一个简单的vanilla Junit测试!).

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

我是否必须做一些特别的事情以确保一旦测试用例完成,Spring就会从JVM中清除出来?为什么我的集成测试后会出现一些Spring configuraiton?

Ral*_*lph 4

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

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

  • 目录/模式
    • src/test/java,pattern: *Test- 包含使用没有 spring-security 的 spring 配置的测试
    • src/test/javaSecurity,模式:*SecurityTest需要启用 Spring Security 的 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>
Run Code Online (Sandbox Code Playgroud)