2016-08-19 48 views
1

我想在春天测试禁用@Schedule但我不能老是找到一个方法来做到这一点。禁用春@EnableScheduling在JUnit测试

我试图让不同的配置类测试环境,但任务仍然被解雇。 这是配置:

@Configuration 
@EnableTransactionManagement 
@EnableScheduling 
@ComponentScan({"de.package"}) 
@PropertySource(name="application.properties", value="classpath:application.properties") 
public class PersistenceJPAConfig { 
... 
} 

这是测试emvironment config.Just去除@EnableScheduling注释

@Configuration 
@EnableTransactionManagement 
@ComponentScan({"de.package"}) 
@PropertySource(name="application.properties", value="classpath:application.properties") 
public class PersistenceJPATestConfig { 
... 
} 

在测试我使用:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = { PersistenceJPATestConfig.class }, loader = AnnotationConfigContextLoader.class) 
@FixMethodOrder(MethodSorters.NAME_ASCENDING) 
public class GetArticlesTest { 
... 
} 

但是当我运行的任务仍然解雇测试..有没有办法在运行测试时停止执行任务?

+0

[春试验禁用@EnableScheduling]的可能的复制(http://stackoverflow.com/questions/29014496/disable-enablescheduling-on-spring-tests) – ali

回答

4

当你使用@ComponentScan放在同一个包了时间,似乎弹簧加载的其他配置也是。

你可以使用一些配置文件来过滤,比如增加此对您PersistenceJPATestConfig

@Profile("test") 

你的JUnit类添加此注释,因此将与“测试”的个人资料被执行

@ActiveProfiles("test") 

编辑: 你的主要的配置也应该严密,因此被忽略时,其个人资料无效,所以你应该比“测试”

0123不同的配置文件上的主要配置类再添@profile
+0

有多个复制到不同的答案这个问题。这是我认为唯一有效的。 – ali