模拟不正常工作

时间:2017-09-10 10:44:57

标签: java android mockito

我想在我当前的应用程序中使用TDD并尝试创建一些测试。在这种情况下,我需要向支持发送消息,如果可以返回true。但是当我开始测试时,我会失败,因为实际和预期的结果是不一样的。

当我调试此测试时,我看到,mockito根据需要放置了Mocks,但sender.sendSupportMessage()钢返回false。任何人都可以解释我的错误吗?

public class SupportTests {

    @Rule
    public MockitoRule rule = MockitoJUnit.rule();

    @Mock
    SupportContract.ISupportMessageSender sender;
    @Mock
    CurrentUserOperations currentUserOperations;

    private SupportMessage stubSupportMessage;
    private String message;

    @Before
    public void init(){
        message = "message";
        stubSupportMessage = new SupportMessage(1, message, null);
    }

    @Test
    public void testSendMessageToSupportShouldBeDone (){
        Support support = new Support(sender, currentUserOperations); 

        when(currentUserOperations.getCurrentUserSystemId())
            .thenReturn(Long.valueOf(1));
        when(sender.sendSupportMessage(stubSupportMessage))
            .thenReturn(true);

        boolean actualResult = support.sendMessageToSupport(message);

        assertEquals("Sending message to support are failed.", true, actualResult);
    }

}


public class Support implements SupportContract.ISupport {

    private SupportContract.ISupportMessageSender sender;
    private CurrentUserOperations currentUserOperations;

    public Support(SupportContract.ISupportMessageSender sender, CurrentUserOperations currentUserOperations) {
    this.sender = sender;
    this.currentUserOperations = currentUserOperations;
    }

    @Override
    public boolean sendMessageToSupport(String message) {
        long userSystemId = currentUserOperations.getCurrentUserSystemId();

        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

        SupportMessage supportMessage = new SupportMessage(userSystemId, message,
            dateFormat.format(Calendar.getInstance().getTime()));

        return sender.sendSupportMessage(supportMessage);
    }
}

1 个答案:

答案 0 :(得分:0)

确定。我找到了解决方案。

如我们所见,在TestMethod中,我们使用stubSupportMessage,它在@Before init方法中实例化。

此外,在Support课程中,我们看到SupportMessage supportMessage = new SupportMessage(...),这些支持消息不同stubSupportMessage != supportMessage

所以,我们需要摆脱不同的对象。这个代码解决了问题:

when(sender.sendSupportMessage(any(SupportMessage.class))).thenReturn(true);

我们说any可以使用SupportMessage类的对象。

现在测试课程:

public class SupportTests {

@Rule
public MockitoRule rule = MockitoJUnit.rule();

@Mock
SupportContract.ISupportMessageSender sender;
@Mock
CurrentUserOperations currentUserOperations;

private String message;

@Before
public void init(){
    message = "message";
}

@Test
public void testSendMessageToSupportShouldBeDone (){
    Support support = new Support(sender, currentUserOperations);

    when(currentUserOperations.getCurrentUserSystemId()).thenReturn(Long.valueOf(1));
    when(sender.sendSupportMessage(any(SupportMessage.class))).thenReturn(true);

    boolean actualResult = support.sendMessageToSupport(message);

    assertEquals("Sending message to support are failed.", true, actualResult);
}

}