Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMock causes JUnit ExpectedException to pass even if an exception is not thrown

Tags:

java

junit

jmock

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);

    }

}
like image 721
robingrindrod Avatar asked Jan 27 '26 18:01

robingrindrod


1 Answers

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 extends JUnit4ClassRunner 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 the JMock.class extend JUnit4ClassRunner and JUnit4ClassRunner ignores rules. The newer BlockJUnit4ClassRunner supports rules and JMock post 2.6.0 extend this in JMock.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.

like image 51
Duncan Jones Avatar answered Jan 30 '26 08:01

Duncan Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!