Issue #6 new

Ability to reinitialize/reset all Mockito mocks in Spring context

szpak
created an issue

Usually "normal" mocks are (re)initialized before every test. Mocks in a Spring context lives as long as a context. In the situation when multiple tests using the same context defined behavior is propagated through the tests. It would be useful to have an ability in springockito API to reinitialize all mockito mocks living in a Spring context (to be used together with MockitoAnnotations.initMocks(this)). Technically it shouldn't be a problem. There is an issue that those tests probably couldn't be used in parallel mode - mocks as Spring beans are singletons, but I don't know how often tests using Spring context are run in parallel mode withing the same context.

Comments (6)

  1. kubek2k repo owner

    that's actually a good point - in most cases the spring context is not reloaded - temp solution would be to use @DirtiesContext for each of your test cases.

    For the reset and multithreaded usage I would see something like this:

    • Create a proxy mechanism on the foundations of the mockito proxies that would contain actual ThreadLocal for the current mock instance
    • In case someone wants to invoke a method on the instance, and we don't have such instance, we create a new one on the flight
    • The reset function could be done by adding a special interface to the created proxy (we would exchange the mock in the thread local with a new one)

    What do you think?

  2. Log in to comment