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

时间:2015-11-06 21:54:28

标签: java spring junit aspectj spring-test

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

我已经添加了一个新的Integration测试,它利用了Spring Context等,并使用了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上下文 - 这是一个简单的vanilla Junit测试!)。

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

我是否必须做一些特别的事情以确保一旦测试用例完成后Spring将自己清除出JVM?为什么我的集成测试后会出现一些Spring配置?

1 个答案:

答案 0 :(得分:3)

我在测试套件中注意到一些基于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,pattern:*SecurityTest需要启用弹簧安全性的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>