Issue #16 resolved

Springockito gets incorrectly cached when run as part of Test Suite

Nathan McDonald
created an issue

I have a Springockito enabled test with annotations:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = SpringockitoContextLoader.class, locations = "/spring-test.xml")
public class SpringockitoTest {

    @ReplaceWithMock
    @Autowired
    private HttpService httpService;
...
}

If I run this on it's own, it runs fine.

However if I have another normal spring test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring-test.xml" })
public class StandardSpringTest{
...
}

and this runs BEFORE the Springockito test class, the Springockito test gets the normal loaded spring-test.xml loaded context rather than the ones with mocks. It seems like spring/junit, when running a group of tests (either through IDE or maven) collates the test runners up front, and appear to cache the location on startup.

Since there is little control over the order tests get run in a suite, this means the tests may pass when we run a subset, but fail when running all. Not immediately obvious the original context is loading, basically just get an error like:

"org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class ..."

Showing that our original HttpServiceImpl in the original spring context is coming back, rather than the mocked HttpService interface we are trying to replace it with.

Comments (8)

  1. kubek2k repo owner

    Hi again,

    please take a look at the branch: context-cache

    if it works for You - the changes will make it to the new release.

    thanks

    PS: I've fixed it by adding a custom classpath*:/ entries to the application context created for each mock and spy :) #hack

  2. madsteer

    I am experiencing problems that mimic the problem Nathan had with pre version 1.0.5 and a mix of springockito and non-springockito tests. The springockito tests run fine by themselves, but when I run the entire suite of tests, some (but not all) of the springockito tests fail to put mocks int for the @ReplaceWithMock objects. Since I can't recreate this with a small test project, and not 100% of the springockito tests are failing, I'm not ready to call this a bug in springockito yet. However, I can't figure out what the problem is. Can you recommend any steps I can take to track down what the issue is?

    The symptoms are when I run the springockito test by itself, it works. When I run a suite of tests, the

    @ Before / setup
    

    method fails when it tries to do a Mockito.reset() on the mocks, with a NotAMockException. I can recreate in debug, and see that in fact, they are not mocks.

    Running version 1.0.8

  3. Log in to comment