我有一个课我想用Mockito测试。我面临的问题是,当调用Mock时,它总是返回null
值。
我可以看到,当我调试Mockito看到模拟但总是返回null
时。我的理解是,在CUT中,当client.sendMessage
被调用时,它将返回我在Transaction
中创建的@Test
的值。
有关我做错的任何建议吗?
受测试的课程:
public String SendMessage(String ID, String body) {
String receipt = null;
Map<String, Integer> Transaction = null;
try {
Transaction = client.sendMessage(ID, body);
} catch(Exception e) {
receipt = "FAIL";
return receipt;
}
receipt = Transaction.get("receipt").toString();
return receipt;
}
我的测试方法是:
@Mock
private MessageSenderClient client;
@InjectMocks
MessageSender ms;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testPushNotificationProcessorTransactionID() {
String Id = "1234";
String body = "Test Message";
Map<String, Integer> Transaction = new HashMap<String, Integer>();
Transaction.put("Id", 123456);
client = Mockito.mock(PushNotificationClient.class);
Mockito.doReturn(Transaction).when(client).sendMessage(Matchers.anyString(), Matchers.anyString());
String transactionID = ms.SendPushMessage(Id, body);
assertEquals(transactionID, "1");
}