JUnit测试一起运行时失败,但单独传递
我有一堆JUnit测试,它们都是单独运行的。每一个都是一个真正的独立单元测试 - 单一课堂测试。没有上下文是必需的。我可以将它们全部单独或全部运行在Eclipse中或通过maven/surefire插件运行。JUnit测试一起运行时失败,但单独传递
我已经添加了一个新的集成测试,它利用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缓存标注有以下配置:
@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插件定义中,所有测试都会通过,但我对该解决方案/解决方法不满意。它减慢了构建速度,并且在Eclipse中不受尊重 - 那就是我只需一次运行一个JUnit测试运行器即可完成整个项目,而不会失败。
我是否必须做一些特殊的事情来确保Spring在测试用例完成后自行脱离JVM?为什么我会在发布我的集成测试时挂上一些Spring配置?
我注意到在我的测试套件,一些基于AspectJ的实现具有引用一些春天豆静态字段,而这个参考获取时不会更新配置文件甚至整个春天测试环境的变化 (我观察这个春季,安全,但也许有类似的问题与@Cache
)
我的解决办法是,把试验例通过在不同的目录(例如src/test/javaSecurity
)中使用弹簧配置并且还使用命名模式将它们分开。 - 分离的目录/源文件夹用于eclipse,所以我可以单击目录根文件夹并指示eclipse运行此源文件夹中的所有测试。 - 命名模式用于执行与行家试验(因为万无一失具有的特征选择由类命名模式的执行的测试,而不是通过根文件夹)
- 目录/模式
-
src/test/java
,模式:*Test
- 包含一个Spring配置测试,而弹簧安全 -
src/test/javaSecurity
,模式:需要启用安全春春*SecurityTest
测试
-
行家
<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>
什么是你IntegrationTestConfiguration?当你的测试完成时,Spring上下文将被销毁,我怀疑你的EhCache。 – codesalsa
@codesalsa我同意 - 上下文理论上应该被销毁并且所有相关的Spring bean。那么为什么Spring缓存拦截器甚至被首先调用呢?我已经使用其他配置数据更新了我的帖子。 –
在集成Spring之前发生问题了吗? 如果是这样,您在测试之前是否使用setup或beforeClass进行设置? – Beefster