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

StackOverflow https://stackoverflow.com/questions/11799634

  •  24-06-2021
  •  | 
  •  

Pregunta

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

    }

}
¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top