我使用 mockito
编写了以下单元测试来测试我的 EmailService.java
类。我不确定我是否正确地测试了这个(快乐路径和异常场景)。
此外,我收到此错误:此处不允许使用“void”类型
在我的单元测试中的以下代码片段
when(mockEmailService.notify(anyString())).thenThrow(MailException.class);
我知道,由于我的 notify()
方法返回 void,所以我收到了该异常。但不确定如何解决这个问题。我的单元测试或实际类或两者都需要更改任何代码吗?
有人可以指导吗?
EmailServiceTest.java
public class EmailServiceTest {
@Rule
public MockitoJUnitRule rule = new MockitoJUnitRule(this);
@Mock
private MailSender mailSender;
@Mock
private EmailService mockEmailService;
private String emailRecipientAddress = "recipient@abc.com";
private String emailSenderAddress = "sender@abc.com";
private String messageBody = "Hello Message Body!!!";
@Test
public void testNotify() {
EmailService emailService = new EmailService(mailSender, emailRecipientAddress, emailSenderAddress);
emailService.notify(messageBody);
}
@Test(expected = MailException.class)
public void testNotifyMailException() {
when(mockEmailService.notify(anyString())).thenThrow(MailException.class);
EmailService emailService = new EmailService(mailSender, emailRecipientAddress, emailSenderAddress);
emailService.notify(messageBody);
}
}
EmailService.java
public class EmailService {
private static final Log LOG = LogFactory.getLog(EmailService.class);
private static final String EMAIL_SUBJECT = ":: Risk Assessment Job Summary Results::";
private final MailSender mailSender;
private final String emailRecipientAddress;
private final String emailSenderAddress;
public EmailService(MailSender mailSender, String emailRecipientAddress,
String emailSenderAddress) {
this.mailSender = mailSender;
this.emailRecipientAddress = emailRecipientAddress;
this.emailSenderAddress = emailSenderAddress;
}
public void notify(String messageBody) {
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject(EMAIL_SUBJECT);
message.setTo(emailRecipientAddress);
message.setFrom(emailSenderAddress);
message.setText(messageBody);
try {
mailSender.send(message);
} catch (MailException e) {
LOG.error("Error while sending notification email: ", e);
}
}
}
最佳答案
我将假设此处的 EmailService
实现是正确的,并重点关注测试。他们俩都有缺陷。虽然 testNotify
执行时没有错误,但它实际上并没有测试任何内容。从技术上讲,它至少会确认当 mailService
不抛出异常时,notify
不会抛出异常。我们可以做得更好。
编写好的测试的关键是问自己,“这个方法应该做什么?”在编写方法之前您应该能够回答这个问题。对于特定的测试,询问“它应该如何处理这个输入?”或“当它的依赖项执行此操作时它应该做什么?”
在第一种情况下,您创建一个 MailService
,向其传递 MailSender
以及收件人和发件人地址。当 notify
方法被调用时,MailService
实例应该做什么?它应该通过 send
方法将 SimpleMailMessage
传递给 MailSender
。下面是如何做到这一点(注意,我假设 MailSender
实际上采用 MailMessage
接口(interface)而不是 SimpleMailMessage
):
@Mock
private MailSender mailSender;
private EmailService emailService;
private String emailRecipientAddress = "recipient@abc.com";
private String emailSenderAddress = "sender@abc.com";
private String messageBody = "Hello Message Body!!!";
@Before
public void setUp(){
MockitoAnnotations.initMocks(this);
emailService = new EmailService(mailSender, emailRecipientAddress, emailSenderAddress);
}
@Test
public void testMessageSent() throws MailException {
ArgumentCaptor<MailMessage> argument = ArgumentCaptor.forClass(MailMessage.class);
emailService.notify(messageBody);
Mockito.verify(mailSender).send(argument.capture());
Assert.assertEquals(emailRecipientAddress, argument.getValue().getTo());
Assert.assertEquals(emailSenderAddress, argument.getValue().getFrom());
Assert.assertEquals(messageBody, argument.getValue().getText());
}
这可以确保 EmailService
实际上根据传递给其构造函数和 notify
方法的参数发送您期望的消息。我们不关心 MailSender
在此测试中是否正确完成其工作。我们只是假设它有效 - 大概是因为它要么在其他地方经过测试,要么是提供的库的一部分。
异常的测试稍微微妙一些。由于异常被捕获、记录,然后被忽略,所以没有太多需要测试的地方。我个人懒得检查是否记录了任何内容。我们真正想做的是确认如果 MailSender
抛出 MailException
那么 notify
不会抛出异常。如果 MailException
是 RuntimeException
那么我们应该对此进行测试。基本上,您只需模拟 mailSender
即可引发异常。如果 EmailService
没有正确处理它,那么它将抛出异常并且测试将失败(这使用与前面的示例相同的设置):
@Test
public void testMailException() throws MailException {
Mockito.doThrow(Mockito.mock(MailException.class)).when(mailSender).send(Mockito.any(MailMessage.class));
emailService.notify(messageBody);
}
或者,我们可以捕获MailException
,然后显式地使测试失败:
@Test
public void testMailExceptionAlternate() {
try {
Mockito.doThrow(Mockito.mock(MailException.class)).when(mailSender).send(Mockito.any(MailMessage.class));
emailService.notify(messageBody);
} catch (MailException ex){
Assert.fail("MailException was supposed to be caught.");
}
}
两种方法都确认了所需的行为。第二个更清楚地表明它正在测试什么。但缺点是,如果允许 notify
在其他情况下抛出 MailException
,那么该测试可能不起作用。
最后,如果 MailException
是一个已检查异常 - 即它不是一个 RuntimeException
- 那么您甚至不需要测试它。如果 notify
可能抛出 MailException
那么编译器会要求它在方法签名中声明它。
关于java - 无法使用mockito对该方法中引发的异常进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45038141/