gpt4 book ai didi

java - JMockit withCapture 不工作

转载 作者:行者123 更新时间:2023-11-28 20:17:36 25 4
gpt4 key购买 nike

在 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() 中的空指针。

最佳答案

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

关于java - JMockit withCapture 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17233776/

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