3

We are having an old codebase with few tests mocking global static object. That influences other tests that fail if being called in a same testsuite (the same JVM) after the test which mocks static property.

There's a lot of work involved in rewriting the production code into a proper dependency injection. Much faster way would be to remove expectations.

Is there a way to manually clear/remove all expectations that has been set in a specific test class?

Michal
  • 384
  • 3
  • 8
  • JMockit always "clears" all expectations recorded by a test once its execution is completed (in reality, the mocked class is simply unmocked). I suspect the real problem is that the *static initializer* of some mocked class got stubbed out. The `@Mocked` annotation allows such stubbing out to not be done, which is the only way to avoid issues later if and when the class, once unmocked, gets used. So, basically, the answer is to *not* stub out any static class initializers (or better yet, to not mock them in the first place). – Rogério May 16 '19 at 20:41
  • BTW, in my experience, both mocking and dependency injection are bad practices that should (in general) be avoided whenever possible. – Rogério May 16 '19 at 20:43

0 Answers0