5

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

Vadzim
  • 24,954
  • 11
  • 143
  • 151
Rajib Deka
  • 551
  • 1
  • 7
  • 22

2 Answers2

5

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.

Vadzim
  • 24,954
  • 11
  • 143
  • 151
  • `@Autowired` is required before `@ReplaceWithMock`!! – MariuszS Jan 11 '14 at 22:53
  • 2
    `@Autowired` is needed only in case you need to access mocked instance directly in the test case class. It's not required if you just need to replace bean with mock in spring context. Then all other beans would obtain mocked version. It's the most cool feature of Springockito-annotations. – Vadzim Jan 12 '14 at 16:42
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)

Community
  • 1
  • 1
Ralph
  • 118,862
  • 56
  • 287
  • 383