java-即使未引发异常,JMock也会导致JUnit ExpectedException通过
作者:互联网
当我将JMock与JUnit ExpectedException一起使用时,即使未引发异常,测试似乎也可以通过.例如,下面的测试应该失败.但是,如果我取消对两条注释行的注释,则它会通过.难道我做错了什么?这两个组件之间是否存在不兼容?
//@RunWith(JMock.class)
public class JUnitJMockTest {
@Rule
public ExpectedException exception = ExpectedException.none();
//Mockery context = new JUnit4Mockery();
@Test
public void test() {
exception.expect(NullPointerException.class);
}
}
解决方法:
我认为您可能会发现this page有用.引用它:
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
.
简而言之,听起来您可能正在使用2.6.0之前的JMock版本,并且更新可能解决了您的问题.
标签:jmock,junit,java 来源: https://codeday.me/bug/20191101/1980358.html