Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMock assertIsSatisfied in TearDown?

I don't know why, but I have always written my JMock tests like this:

@Test
public void testMyThing() throws Exception {
    mockery.checking(new Expectations() {{
        oneOf(mockObj).foo();
    }});
    testObj.bar(); // calls mockObj.foo()
    mockery.assertIsSatisfied();
}

But when there are many tests, is it better to move assertIsSatisfied to the tear-down?

@After
public void tearDown() throws Exception {
    mockery.assertIsSatisfied();
}
like image 947
Paul McKenzie Avatar asked Jul 08 '11 12:07

Paul McKenzie


1 Answers

The recommended way to do this is to use the JMock runner. Annotate the class with

@RunWith(JMock.class)
public class TestClass {

This will call the assertion at the right place in the test lifecycle. Teardown isn't the right place as a failure might not be reported in correctly and might mess up other cleanup.

We also have a mockery rule in the repository that works with the new @Rule infrastructure.

like image 123
Steve Freeman Avatar answered Oct 27 '22 00:10

Steve Freeman