Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是对象I';我在嘲笑我认为是什么?需要但未调用的错误_Java_Mockito - Fatal编程技术网

Java 是对象I';我在嘲笑我认为是什么?需要但未调用的错误

Java 是对象I';我在嘲笑我认为是什么?需要但未调用的错误,java,mockito,Java,Mockito,我有一个setter,它设置某个文档对象的哈希值。当我添加/设置属性文档(文本、发布日期、作者)时,它应该进入每个if语句并完成stringbuilder。我想检查是否调用了update方法,是否使用“连接字符串”调用了该方法。在本例中,我不确定应该使用update()调用什么,因此我放置了一个空的“字符串 我希望验证看起来像这样,verify(..).update(.getBytes()) 我在模仿正确的对象吗 Wanted but not invoked: messageDigest.upd

我有一个setter,它设置某个文档对象的哈希值。当我添加/设置属性文档(文本、发布日期、作者)时,它应该进入每个
if
语句并完成stringbuilder。我想检查是否调用了
update
方法,是否使用“连接字符串”调用了该方法。在本例中,我不确定应该使用
update()
调用什么,因此我放置了一个空的
字符串

我希望验证看起来像这样,
verify(..).update(.getBytes())

我在模仿正确的对象吗

Wanted but not invoked:
messageDigest.update([]);

重构方法
setHash()
,使其返回字符串(从set更改为get)。并使
hash=getHash()
在这种情况下,我会说单元测试是个坏主意。因为你要测试的只是你的模仿和StringBuilder行为。只需进行一些集成测试:

1.Was returned String with all components(text, publicationDate, authors)
2.Was returned empty String (without all components)
3.Was thrown exception

测试
Mockito.doNothing().when(msgDigest.update)(Mockito.any(byte[].class))中
msgDigest
从何而来在PowerMockito.spy(MessageDigest.class)之后,您需要添加
Mockito.when(MessageDigest.getInstance(“md5”)。然后返回(msgDigest)
Oops,
MessageDigest-msgDigest=Mockito.mock(MessageDigest.class);
之前在我的
@中声明过,但忘记了包含它。如果您调用一个方法(比如
testDocument.setText()
)在模拟中,它不会产生效果。因为您正在尝试测试
文档
类,所以根本不想模拟它。我为
getInstance
添加了模拟,尽管我仍然收到与上面相同的错误。
Wanted but not invoked:
messageDigest.update([]);
1.Was returned String with all components(text, publicationDate, authors)
2.Was returned empty String (without all components)
3.Was thrown exception