Can*_*ell 4 java junit unit-testing mockito
我有以下代码
public void testInitializeButtons() {
model.initializeButtons();
verify(controller, times(1)).sendMessage(
eq(new Message(eq(Model.OutgoingMessageTypes.BUTTON_STATUSES_CHANGED),
eq(new ButtonStatus(anyBoolean(), eq(false), eq(false), eq(false), eq(false))),
anyObject())));
}
Run Code Online (Sandbox Code Playgroud)
抛出以下异常
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
1 matchers expected, 9 recorded.
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
For more info see javadoc for Matchers class.
at se.cambiosys.client.medicalrecords.model.MedicalRecordPanelModelTest.testInitializeButtons(MedicalRecordPanelModelTest.java:88)
Run Code Online (Sandbox Code Playgroud)
有人能指点我如何正确地编写测试吗?
你不能这样做:eq()只能用于模拟的方法参数,而不能用在其他对象中(就像你在构造函数中所做的那样Message).我看到三个选择:
ArgumentCaptor,并使用asserts()测试Message的属性equals()在Message类中实现,以便根据您实际要验证的字段测试与另一个Message的相等性.