Announcement Announcement Module
No announcement yet.
Mocked Objects in Context during tests execution Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mocked Objects in Context during tests execution


    I'm working on an application which has both component and integration tests. The differences between them is: a component test test more than one class (i.e., its inner objects aren't all mocked out - but some of them might be [such as JMS publishers]) and an Integration test is a test that nothing at all is mocked out. In another words, Spring gives you the object and you test it as it is.

    So far, so good.

    The problem is: in order to be able to replace one dependency or another from the Spring context, I used Springockito ( which offers you a way to mock out some beans from the Spring context.

    So - in the component tests - I have this:

    @DirtiesContext(classMode = AFTER_CLASS)
    @ContextConfiguration(loader = SpringockitoContextLoader.class, locations = "classpath:spring-classify-test.xml")
    public class....
        private SomeServiceInterface someServiceInterface;
        private Bean bean;
    Bean has SomeServiceInterface as a depedency.

    public class Bean { 
         private SomeServiceInterface...
    In the case above, SomeServiceInterface concrete implementation will be replaced by a mock. Of course, that example is an oversimplification of the problem - I replace beans with mocks that are dependecies further down in the object graph.

    It's worthy noticing that I load the context from this file: spring-classify-test.xml Also it's wothy noticing that I mark the context as dirty after the execution of the class - so, AFAIK, the next test class must reload the context.

    Ok, now the integration test:
    @ContextConfiguration(loader = SpringockitoContextLoader.class, locations = { "classpath:/spring-service-integration-test.xml" })
    public ...
        private Bean bean;
    I load the context from spring-service-integration-test.xml - but SomeServiceInterface inside of Bean is still mocked! The context used in the integration test was changed as well!

    If I mark the Integration test with @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD), the first test in the class will fail because SomeServiceInterface is mocked - but the next test will pass because the context has been refreshed already.

    Funny thing is:

    If I ask Spring to inject SomeServiceInterface in the Integration Test, it will inject a SomeServiceInterface concrete implementation - not a mock!

    I have tried many things to sort out that issue:
    1) Programatically override the beans in the context after the component tests are done using the registerBeanDefinition method from the context
    2) Create a TestExecution listener so I could try to manuallty refresh the context before the execution of an IntegrationTest - did not fix.
    3) Use the same loader for the different contexts....
    4) Go on and on.

    Does anyone have any idea?

    Last edited by cld_spring; Jan 31st, 2013, 07:24 AM.