gpt4 book ai didi

java - 即使没有抛出异常,JMock 也会导致 JUnit ExpectedException 通过

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:21:30 27 4
gpt4 key购买 nike

当我将 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 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.

简而言之,听起来您可能正在使用 2.6.0 之前的 JMock 版本,更新可能会解决您的问题。

关于java - 即使没有抛出异常,JMock 也会导致 JUnit ExpectedException 通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11799634/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com