JUnit中的Matchers.refEq()比较对象的引用

时间:2014-11-18 21:56:32

标签: java testing junit mockito

我试图验证我在测试方法中创建的对象和实际对象在所有字段中是否具有相同的值。我使用Matchers.refEq()来验证。在我的代码中,两个对象的实际引用正在验证,而不是字段值,而不是文档对Matchers.refEq()所说的。

以下测试用例有什么问题?

@Test
public void sendEmailMsgTest() throws MessagingException{

    PowerMockito.mockStatic(Transport.class);
    PowerMockito.doNothing().when(Transport.class);
    Transport.send(Matchers.any(MimeMessage.class));

    Properties systemProperties = System.getProperties();
    Session session = Session.getDefaultInstance(systemProperties);
    session.setDebug(debug);
    MimeMessage mimeMessage = new MimeMessage(session);
    mimeMessage.setFrom(new InternetAddress(sendFrom));
    mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(
            validEmailAddress));
    mimeMessage.setSubject("HTML/CSS grade report");
    mimeMessage.setSentDate(new Date());
    final BodyPart textBodyPart = new MimeBodyPart();
    textBodyPart
            .setText("Here is your score card for the HTML/CSS assessment");
    final BodyPart fileBodyPart = new MimeBodyPart();
    final DataSource source = new FileDataSource(outputFile);
    fileBodyPart.setDataHandler(new DataHandler(source));
    fileBodyPart.setFileName(new File(outputFile).getName());
    final Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(textBodyPart);
    multipart.addBodyPart(fileBodyPart);
    mimeMessage.setContent(multipart);

    WriteGradeReportUtil.emailGrade(validEmailAddress, outputFile);
    Mockito.verify(Transport.class);
    Transport.send(Matchers.refEq(mimeMessage));
}

方法I尝试测试:

static void emailGrade(){
//Some code
MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(sendFrom));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(
                    sendTo));
            message.setSubject("HTML/CSS grade report");
            message.setSentDate(new Date());
            final BodyPart textBodyPart = new MimeBodyPart();
            textBodyPart
                    .setText("Here is your score card for the HTML/CSS assessment");
            final BodyPart fileBodyPart = new MimeBodyPart();
            final DataSource source = new FileDataSource(outputFile);
            fileBodyPart.setDataHandler(new DataHandler(source));
            fileBodyPart.setFileName(new File(outputFile).getName());
            final Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(textBodyPart);
            multipart.addBodyPart(fileBodyPart);
            message.setContent(multipart);
            Transport.send(message);
}

1 个答案:

答案 0 :(得分:2)

Mockito 1.x在内部使用hamcrest匹配器,在内部匹配器使用Apache commons org.mockito.internal.matchers.apachecommons.EqualsBuilder#reflectionEquals(java.lang.Object, java.lang.Object, java.lang.String[])。 Mockito 2.x直接使用Apache commons org.mockito.internal.matchers.apachecommons.EqualsBuilder#reflectionEquals(java.lang.Object, java.lang.Object, java.lang.String[])

因此,如果此调用显示对象不相等,则可能您的某个字段不相等。

另请注意,Mockito javadoc警告您:<b>Warning</b> The equality check is shallow!这些字段也可能不会实现equals,因此它们不能相等。


除了这个答案的范围外,我还想对代码片段做一个评论。很难理解,然后重构。嘲笑者的座右铭之一是不要嘲笑你不拥有的类型。您应该使用模拟邮件服务器 - 那里有很多 - 并直接发送邮件。