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

0 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));
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试测试的方法:

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);
}
Run Code Online (Sandbox Code Playgroud)

Bri*_*ice 5

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 commonsorg.mockito.internal.matchers.apachecommons.EqualsBuilder#reflectionEquals(java.lang.Object, java.lang.Object, java.lang.String[])

\n\n

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

\n\n

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

\n\n
\n\n

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

\n