我有以下课程:
public class Listener{
@Autowired
private Handler handler;
public void receiveMessage(Message<String> message) {
String xmlMessage = message.getPayload().toString();
XStream xstream = new XStream();
xstream.processAnnotations(InfoTO.class);
infoTO = (InfoTO) xstream.fromXML(xmlMessage);
UserDetailTO[] userDetailTO = { new UserDetailTO(infoTO.getUserId(), null) };
handler.sendEmail(userDetailTO);
}
}
我的测试方法:
@Test
public void testRecieve() {
UserDetailTO[] userDetails={new UserDetailTO("zzzz",null)};
Handler handlerMock=EasyMock.createMock(Handler.class);
handlerMock.sendEmail(aryEq(userDetails));
EasyMock.expectLastCall();
EasyMock.replay(handlerMock);
ReflectionTestUtils.setField(listener, "handler", handlerMock);
String message = "Test Payload";
Message<String> finalMessage = MessageBuilder.withPayload(message).build();
listener.receiveMessage(finalMessage);
}
当我运行这个测试类时,我得到意外方法调用的断言错误。这是预期的,因为在测试类中,插入到数组中的 UserDetailTO 实例将与在 Listener 类中创建的实例不匹配。
但是,如何解决这个问题呢?