2016-08-19 47 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