Springockito怎么样?

我想在我的一个IT中使用Springockito来模拟DAO bean。 在我的IT中,我必须使用spring context.xml来自动assembly某些服务,并使用mockApplication.xml来模拟DAO。 那么,我如何同时使用两个xml配置文件呢?

@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

ContextConfiguration.locations是一个数组,因此您可以指定所需的位置。

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

BTW 🙁 这只是我记忆中的一个提示,我不知道问题是否仍然存在,或者我做错了什么 )很久以前我注意到使用两个位置参数时出现了一些问题,因为它接缝那个弹簧产生了两个共存点(每个位置一个)。 因此,我使用一个配置文件,其中包含两个正常配置文件。 (@see https://stackoverflow.com/a/3414669/280244

Springockito-annotations可以避免需要额外的模拟上下文。

只需枚举DAO就可以在相同的测试用例中进行模拟:

 @ReplaceWithMock DAO dao; 

这个dao将在主应用程序上下文中自动替换。 控制器会看到模拟的bean。