Selected objects in context when executing tests

I am working on an application that has both component and integration tests. The difference between them is this: a test test of a component of more than one class (i.e. its internal objects are not all mocked, but some may be [such as JMS publishers]), and an integration test is a test that has nothing everything is mocking. In other words, Spring provides you with an object and you validate it as is.

So far so good.

The problem is that to replace one dependency from a Spring context, I used Springockito ( https://bitbucket.org/kubek2k/springockito/wiki/Home ) which offers you a way to mock a bean from a Spring context.

So - in component tests - I have this:

@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext(classMode = AFTER_CLASS)
@ContextConfiguration(loader = SpringockitoContextLoader.class, locations = "classpath:spring-classify-test.xml")
public class....

     @Autowired
     @ReplaceWithMock
     private SomeServiceInterface someServiceInterface;
     @Autowired
     private Bean bean;

      

The Bean has SomeServiceInterface as dependencies.

public class Bean { 
   private SomeServiceInterface...

      

In the above case, SomeServiceInterface will be replaced with mock. Of course this example is a simplification of the problem - I am replacing the bean with mock objects that are more dependent on each other in the object graph.

It's worth noting that I load the context from this file: spring-classify-test.xml Also he noticed that I mark the context as dirty after executing the class - so AFAIK the next test class should reload the context.

Ok, now the integration test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = SpringockitoContextLoader.class, locations = {"classpath:/spring-service-integration-test.xml" })
public class ...

    @Autowired
    private Bean bean;

      

I am loading context from spring-service-integration-test.xml, but SomeServiceInterface inside the bean is still mocked! The context used in the integration test has also been changed!

If I mark the Integration test with @DirtiesContext (classMode = ClassMode.AFTER_EACH_TEST_METHOD), the first test in the class fails because SomeServiceInterface will mock, but the next test passes because the context has already been updated.

Funny:

If I ask Spring to inject SomeServiceInterface in an integration test, it injects a concrete implementation of SomeServiceInterface - not a mock!

I've tried a lot of things to sort out this problem:

1) Programmatically override beans in context after component tests are done using registerBeanDefinition method from context

2) Create a TestExecution listener so I can try to manually update the context before executing the IntegrationTest

3) Use the same bootloader for different contexts ...

4) This story goes on and on.

Does anyone have any ideas?

PS: I quite understand that adopting Springockito was a dubious idea - but that decision was not made by me and we now have over 500 tests in the project, so refactoring all of them to remove Springockito will not be an easy task, therefore not a viable ATM option.

Greetings.

+3


source to share


2 answers


Annotation is @DirtiesContext

processed by the DirtiesContextTestExecutionListener when registered with the TestContextManager . With regular Spring vanilla tests, this listener is registered by default. Perhaps Springockito or something else in your test "component testing" is doing something that prevents the default logger from registering?



+1


source


I suggest trying the latest springockito-annotations 1.0.8 with

@DirtiesMocks(classMode = DirtiesMocks.ClassMode.AFTER_CLASS)

      



See https://bitbucket.org/kubek2k/springockito/wiki/springockito-annotations#!reseting-mocksspies-in-experimental-package-hope-youll-like-it .

0


source







All Articles