4

I think the answer is yes, but I was unable to find anything definite in the easymock documentation.

I'm getting an error when first test is mocking a class, and then another test is using this class in a regular way. The class turns out to be mocked in the second test and fails with unexpected invocation.

Is there a way to automate the cleanup, besides using the EasyMockSupport and calling resetAll() in @After method (preferably something that doesn't have to be repeated in each class)?

Jakub Bochenski
  • 3,113
  • 4
  • 33
  • 61

2 Answers2

6

It seems you don't need to manually reset the mocks.

In my case the mocked class was stored in a static variable, once this was corrected the tests started running OK.

It would be nice if EasyMock reported the place where violated expectation was recorded - that would have made it much easier to find the source of the problem.

Jakub Bochenski
  • 3,113
  • 4
  • 33
  • 61
1

"I'm getting an error when first test is mocking a class, and then another test is using this class in a regular way. The class turns out to be mocked in the second test and fails with unexpected invocation."

Sounds to me like either of the following might be true:

  1. You've created a mock dependency that you've added to a factory, so it's always returning this mock dependency

  2. You've used power mock to prepare a class for test and this has persisted between your tests.

If either of those are true, or something else has occurred, I would suggest that there is a design flaw somewhere in your code base.

If option one has occurred (which it can when testing factories that return singletons for example) then it is the job of the individual test class to ensure that the mock dependency is replaced after it has finished with it.

If option two has occurred, then perhaps you'd actually like to use the @PrepareOnlythisForTest

Ultimately, what you're looking for should never be needed. Remember to always mock responsibily.

Dan Temple
  • 2,736
  • 2
  • 22
  • 39
  • 1
    Thanks, for not answering my actual question. Your answer was also totally not helpful in locating the problem in 250+ test cases I just saw for the first time. – Jakub Bochenski Dec 11 '13 at 12:59
  • So the mock you had was set as a static variable. That is similar in many ways to the mock dependency in the factory example I pointed out. If my answer really was of no use to you at all, I'll gladly remove it. – Dan Temple Dec 12 '13 at 10:44
  • 1
    Well I asked a specific question about EasyMock; you didn't provide a direct answer to it. The software engineering tutoring was uncalled for. Once I've got an answer from the tests author the solution became obvious. One helpful anwser would be: "You can do X to see where the mock that is breaking your test is being created" – Jakub Bochenski Dec 13 '13 at 17:05