【问题标题】:JMockit withCapture not workingJMockit withCapture 不工作
【发布时间】:2013-06-18 11:57:45
【问题描述】:

在 jmockit 中使用 withCapture 捕获对象的验证不太有效。有什么帮助吗?

@Injectable
private API _api;

@Tested
private ServiceImpl _service;

@Test
public void test(){
    new Verifications() {
        {
            VirtualUser user;
            _api.add(user = withCapture());
            Assert.assertEquals("1", user.getId());
            Assert.assertEquals("user", user.getUsername());
        }
    };
    _service.add("1","user");
}

异常:user.getId() 中的空指针。

【问题讨论】:

  • 你能展示一个可执行的测试吗?此片段未显示 _api 的模拟位置。
  • @Rogério 在这里被嘲笑了。
  • 谢谢,我现在看到了错误(以前应该看到过...)。

标签: java unit-testing testing junit jmockit


【解决方案1】:

验证块 (new Verifications() { ... }) 应该在 执行了被测代码之后出现。在示例测试中,验证是在之前执行的,因此此时没有任何内容可以捕获到user 变量中。因此,它仍然是null。只需将验证块移动到测试方法的末尾即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多