views:

239

answers:

2

I have designed a small test case class that is designed to make mocking easier (in this case, with JUnit 4 and EasyMock). Part of this is verifying the mocks after the test has finished, so the mocks are verified in a method annotated with @After.

However, if there is a failure in the test method itself, which causes the test not to be completed and the mock verification to fail, the failure reported by JUnit is the failure of verification. However, it would be more useful if the failure reported was the failure in the test itself.

So, is there a way in JUnit to make sure that errors/failures in the test method are always displayed in preference to errors/failures that arise in methods annotated with @After?

+2  A: 

Generally, the @After annotation is the equivalent of tearDown() which carries the implication that it should only be doing test cleanup, not running tests or making assertions.

I would have your test cases call your "verify" method at the end of each test case instead of having JUnit call it for you

Kevin
Since many tests will be checking that mock expectations were met, it's fairly conventional to have that code in an @After method. Also, you can have multiple @After methods, so you might have one do mock verification and one do teardown. Don't repeat if you don't need to.
cynicalman
@After methods are not intended for assertions, they're there to cleanup. Does EasyMock not have a JUnit 4 test runner (or maybe now a @Rule) to help with this?
Steve Freeman
A: 

What asserts are you doing in your tests that the verify in @After is being reported? If there is an assertion failure (or the fail() method is called) then that will be reported, and the after method will not be reported. What does the test method look like?

cynicalman