【发布时间】:2026-02-27 07:20:10
【问题描述】:
我有考试
Document document = spy(new Document());
Whitebox.setInternalState(documentReceiverInteractor, "document", document);
String text= "string";
Whitebox.invokeMethod(documentReceiverInteractor, "saveFields", anyString(), eq(text), anyString(),
anyString(), anyString(), anyString(), anyString());
verify(document).setText(text);
启动后,我得到这个错误:
Argument(s) are different! Wanted:
document.setText(
<any string>
);
-> at ru.psbank.msb.dev.business.document.edit.receiver.DocumentReceiverInteractorTest.saveFields(DocumentReceiverInteractorTest.java:98)
Actual invocation has different arguments:
document.setText(
null
);
eq 可以很好地处理原语并且没有对象。我该怎么办?
【问题讨论】:
-
您是否尝试过调试测试调用? null 是否真的通过了?
-
你为什么首先使用 Matchers 调用方法?你期望那里发生什么。
eq("text")不是字符串“文本”。匹配器用于verify、when等,但不用于实际的方法调用。anyString()例如返回一个空字符串而不是任何字符串。eq("text")在直接调用中使用时可能会返回 null。您只是在错误的地方使用了 Matchers。从您的方法调用中删除匹配器,一切都会好起来的。
标签: android testing mockito powermock