Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Springockito how to?

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

like image 346
Rajib Deka Avatar asked Oct 14 '13 06:10

Rajib Deka


2 Answers

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.

like image 182
Vadzim Avatar answered Sep 26 '22 06:09

Vadzim


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)

like image 28
Ralph Avatar answered Sep 25 '22 06:09

Ralph