问题
I want to mock DAO bean using Springockito in one of my IT. In my IT I have to use spring context.xml to autowire some services and also mockApplication.xml to mock the DAOs. So, how can I use both the xml configuration files at the same time?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = SpringockitoContextLoader.class, locations = {"classpath*:/MockApplicationContext.xml"})
public class PayRollComponentFacadeIT {
@Autowired
IPayRollComponentFacade payRollComponentFacade;
@ReplaceWithMock
@Autowired
IPayRollPersistenceManager payRollPersistenceManager;
I have included mock context as @ContextConfiguration(loader = SpringockitoContextLoader.class, locations = {"classpath*:/MockApplicationContext.xml"})
But I have to include the spring context also @ContextConfiguration(locations = {"classpath*:/testApplicationContext.xml"})
Regards Rajib
回答1:
ContextConfiguration.locations is an Array, so you can specifiy as may locaction you want.
@ContextConfiguration(
loader = SpringockitoContextLoader.class,
locations = {"classpath*:/MockApplicationContext.xml",
"classpath*:/testApplicationContext.xml"}
)
BTW: (this is only a hint from my memory, I dont know if the problem still exists, or if I have done something wrong)
Long time ago I noticed some problems when using two location parameters, because it seams that spring create two conexts (one for each location). Therefor I use an single configuration file that inculde
s the two normal configuration files. (@see https://stackoverflow.com/a/3414669/280244)
回答2:
Springockito-annotations make it possible to avoid the need of additional mock context at all.
Just enumerate DAO to be mocked in the same test case:
@ReplaceWithMock
DAO dao;
This dao would be automatically replaced in main application context. Controller would see the mocked bean.
来源:https://stackoverflow.com/questions/19354473/springockito-how-to