2013-10-14 16 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