2013-10-14 17 views
4

我想在我的IT中使用Springockito模擬DAO bean。在我的IT中,我必須使用spring context.xml來自動調用一些服務,還需要使用mockApplication.xml來模擬DAO。那麼,我怎樣才能同時使用xml配置文件呢?Springockito如何?

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(loader = SpringockitoContextLoader.class, locations = {"classpath*:/MockApplicationContext.xml"}) 
public class PayRollComponentFacadeIT { 
    @Autowired 
    IPayRollComponentFacade payRollComponentFacade; 
    @ReplaceWithMock 
    @Autowired 
    IPayRollPersistenceManager payRollPersistenceManager; 

我已經包括模擬背景爲@ContextConfiguration(loader = SpringockitoContextLoader.class, locations = {"classpath*:/MockApplicationContext.xml"})

但我必須包括Spring上下文也@ContextConfiguration(locations = {"classpath*:/testApplicationContext.xml"})

問候 Rajib

回答

1

ContextConfiguration.locations是一個數組,所以你可以指定地址爲可能你想要的locaction。

@ContextConfiguration(
     loader = SpringockitoContextLoader.class, 
     locations = {"classpath*:/MockApplicationContext.xml", 
        "classpath*:/testApplicationContext.xml"} 
) 

BTW:(這只是從我的記憶的暗示,我不知道如果問題仍然存在,或者如果我做錯了) 很久以前,我注意到,當一些問題使用兩個位置參數,因爲它會在彈簧接縫處創建兩個錐體文字(每個位置一個)。因此,我使用一個配置文件inculde兩個正常的配置文件。 (@see https://stackoverflow.com/a/3414669/280244

4

Springockito註解使得有可能避免額外的模擬環境的需要在所有。

只是列舉DAO在相同的測試案例被嘲笑:

@ReplaceWithMock 
DAO dao; 

這道將在主應用程序上下文被自動替換。控制器會看到嘲笑的豆子。

+0

'@ Autowired'之​​前'@ ReplaceWithMock'是必需的! – MariuszS

+2

'@ Autowired'僅在情況下,你需要直接在測試用例類訪問嘲笑實例所需。如果你只是需要在春天的上下文中用模擬來替換bean,這並不是必需的。然後所有其他豆都會獲得模擬版本。這是Springockito的註解中最涼爽的特點。 – Vadzim

+1

哇,很好的功能,謝謝:) – MariuszS