junit - Junit 测试用例中 NonStrictExpections 到期望转换的问题
问题描述
这是我的示例代码示例,当我进行 jmockit 更新相关更改时它开始失败。
class A {
public static boolean validate(String name, int age, boolean flag) {
boolean result = false;
//actual code
return result;
}
}
class B {
public void cal() {
if (A.validate(name, age, flag)) {
// some calculations
}
}
}
class TestB {
public B b;
@Before
public void setUp() {
b = new B();
}
@Test
public void testCal() {
new Expectations() {
{
A.validate(anyString, anyInt, anyBoolean);
times= -1;
result = Boolean.TRUE;
}};
b.cal();
new Verifications() {
{
A.validate(anyString, anyInt, anyBoolean);
}
};
}
}
这失败并出现错误
mockit.internal.UnexpectedInvocation: Unexpected invocation of:
A#validate(String name, int age, boolean flag)
我刚刚将 NonStrictExpectations 块更改为 Expectations 块,因为最新的 jmockit 不支持 NonStrictExpectations 块。
new NonStrictExpectations() {
{
A.validate(anyString, anyInt, anyBoolean);
returns(Boolean.TRUE);
}
};
有了这个块,一切正常。
请让我知道问题出在哪里?
解决方案
首先,对 使用负值times
不是有效值。
其次,如果你需要一个可选的调用,你应该使用minTimes
andmaxTimes
变量。在您的情况下,maxTimes = 1
您将实现一个可选的模拟调用。
推荐阅读
- .net - SQL Server 找不到运行的任何服务
- linux - 缺少 Linux 字体。Stackoverflow 代码示例显示完全空白
- javascript - 将隐藏的表格复制到剪贴板
- javascript - 将 JSON 响应中的值解析并显示为 HTML
- python - 更好的方法来测试功能
- kubernetes - 掌舵错误“TILLER:意外EOF”的原因可能是什么
- rest - TFS 2013 rest api 基本身份验证不起作用
- java - org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“loginController”的bean时出错
- ruby-on-rails - 查询获取的活动记录数据
- sql - Oracle中的左外连接和带日期的必要子查询