我有一堆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配置?
什么是你IntegrationTestConfiguration?当你的测试完成时,Spring上下文将被销毁,我怀疑你的EhCache。 – codesalsa
@codesalsa我同意 - 上下文理论上应该被销毁并且所有相关的Spring bean。那么为什么Spring缓存拦截器甚至被首先调用呢?我已经使用其他配置数据更新了我的帖子。 –
在集成Spring之前发生问题了吗? 如果是这样,您在测试之前是否使用setup或beforeClass进行设置? – Beefster