When I use JMock with JUnit ExpectedException
the tests seem to pass even if the exception is not thrown. For example, the test below fails, as it should. But if I uncomment the two commented lines, it passes. Am I doing something wrong? Is there an incompatibility between these two components?
//@RunWith(JMock.class)
public class JUnitJMockTest {
@Rule
public ExpectedException exception = ExpectedException.none();
//Mockery context = new JUnit4Mockery();
@Test
public void test() {
exception.expect(NullPointerException.class);
}
}
I think you may find this page useful. Quoting from it:
Beware though that if you combine the rule with certain
@RunWith
classes, you may get a false positive. Specifically, if you were to run with a class that extendsJUnit4ClassRunner
in the above example, the test would no longer fail. You’d get a false positive.For example, if you’re using a version of JMock prior to
2.6.0
and use@RunWith(JMock.class)
you’ll encounter this. Older versions of theJMock.class
extendJUnit4ClassRunner
andJUnit4ClassRunner
ignores rules. The newerBlockJUnit4ClassRunner
supports rules and JMock post 2.6.0 extend this inJMock.class
.
In short, it sounds like you may be using a version of JMock prior to 2.6.0 and an update may solve your issue.