Springockito how to?

百般思念 提交于 2019-12-01 18:33:37

问题


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 inculdes 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!