Mockito doReturn始终返回NULL

时间:2016-09-25 21:45:41

标签: java mockito

我有一个课我想用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");   
}

0 个答案:

没有答案