2017-09-19 59 views
-1

我有一個小的Spring web應用程序。除了我簡單的單元測試之外,我正在編寫一個單元測試來驗證所需的bean佈線。我正在使用默認的applicationContext.xml文件,而不是「測試」版本。我確實有一些通常在我的Tomcat JNDI上下文中定義的假測試資源。如何在彈出的測試中覆蓋/關閉計劃任務

該測試基本上可行,但有一個煩惱是,在默認環境中定義的某些計劃任務啓動併發出一些不影響測試結果的錯誤消息。

預定的任務可在上下文定義是這樣的:

<task:scheduled-tasks> 
    <task:scheduled ref="ratesQueryProcessor" method="run" fixed-rate="30000"/> <!-- Every 120 seconds --> 
</task:scheduled-tasks> 

是否有東西,我可以從默認的applicationContext.xml和我的「測試資源」 XML文件所造成的Spring上下文做的,也許是一個JavaConfig類,它會「覆蓋」這些計劃任務以關閉它們?

如果它的事項,下面是從我的單元測試類小摘錄:

@RunWith(SpringRunner.class) 
@ContextConfiguration(value = {"file:src/main/webapp/WEB-INF/applicationContext.xml", "/testResources.xml"}) 
//@ContextHierarchy({ 
// @ContextConfiguration("file:src/main/webapp/WEB-INF/applicationContext.xml"), 
// @ContextConfiguration(classes = SpringWiringTest.Config.class) 
//}) 
@TestPropertySource(properties = { "env = tomcat", "doNotifications = false" }) 
public class SpringWiringTest { 

註釋掉部分是因爲我試圖在JavaConfig類來定義我的測試資源,但在這一點上我無法使用一個XML文件和一個JavaConfig類(我有另一個SO發帖詢問這個)。

回答