Raj*_*eka 5 java spring mockito springmockito
我想在我的IT之一中使用Springockito模拟DAO bean。在我的IT中,我必须使用spring context.xml来自动装配某些服务,还必须使用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;
Run Code Online (Sandbox Code Playgroud)
我已经包括了模拟上下文 @ContextConfiguration(loader = SpringockitoContextLoader.class, locations = {"classpath*:/MockApplicationContext.xml"})
但是我还必须包括春季背景 @ContextConfiguration(locations = {"classpath*:/testApplicationContext.xml"})
问候拉吉卜
Springockito-annotations 可以完全避免额外的模拟上下文的需要。
只需在同一测试用例中枚举要模拟的 DAO:
@ReplaceWithMock
DAO dao;
Run Code Online (Sandbox Code Playgroud)
该 dao 将在主应用程序上下文中自动替换。控制器将看到模拟的 bean。
ContextConfiguration.locations是一个数组,因此您可以指定您想要的位置。
@ContextConfiguration(
loader = SpringockitoContextLoader.class,
locations = {"classpath*:/MockApplicationContext.xml",
"classpath*:/testApplicationContext.xml"}
)
Run Code Online (Sandbox Code Playgroud)
顺便说一句:(这只是我记忆中的一个提示,我不知道问题是否仍然存在,或者我是否做错了什么)很久以前,我在使用两个位置参数时注意到一些问题,因为它表明 spring 创建了两个上下文(每个地点一个)。因此,我使用一个配置文件来inculde代替两个普通配置文件。(@参见/sf/answers/239026861/)