我有这个sendmail函数,我想使用MockMvc在Junit 4中进行测试。 我已经提供了测试用例和用于测试功能的 WiserAssertions.java 文件。
在测试Junit测试用例时,我得到了AssertionError
java.lang.AssertionError:未找到来自[demoprojintern@gmail.com]的消息! at com.appointment.request.controller.WiserAssertions.lambda $ 7(WiserAssertions.java:67)
有人可以帮我解决我的失踪吗?
sendmail()功能
public void sendmail(Request request) throws AddressException, MessagingException, IOException {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
String email = request.getEmail();
int requestId1 = request.getRequestId();
String requestId = Integer.toString(requestId1);
String name = request.getLegalName();
String cause = request.getCause();
String doa = request.getDateOfAppointment();
String toa = request.getAppointmentTime();
int status = request.getRequestStatus();
String generic = "Hello, " + name + "<br><br>" + "Your request with Request ID: " + requestId + " for " + "the cause of "
+ cause + " on " + doa + " at " + toa + " has been ";
String regards = "<br><br><br>Thanks and Regards,<br>";
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("<my-email-id>", "<my-email-id's password");
}
});
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("<my-email-id>", false));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
msg.setSubject("Regarding your Appointment Request");
msg.setContent(" ", "text/html");
msg.setSentDate(new Date());
MimeBodyPart messageBodyPart = new MimeBodyPart();
if (status == 0) {
String messageForPendingRequest = generic + "<strong>RECEIVED</strong>." + regards;
messageBodyPart.setContent(messageForPendingRequest, "text/html");
}
if (status == 1) {
String messageForPendingRequest = generic + "<strong>APPROVED</strong>." + regards;
messageBodyPart.setContent(messageForPendingRequest, "text/html");
}
if (status == 2) {
String messageForPendingRequest = generic + "<strong>SUGGESTED</strong>." + regards;
messageBodyPart.setContent(messageForPendingRequest, "text/html");
}
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
Transport.send(msg);
}
这些是我pom.xml分别用于发送电子邮件和测试邮件的依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.subethamail</groupId>
<artifactId>subethasmtp</artifactId>
<version>3.1.7</version>
<scope>test</scope>
</dependency>
这是我的junit测试用例
private Wiser wiser;
@Test
public void send() throws Exception {
Request request = new Request(1234, "Richie Rich", "<patient's-email-id>", "Brain", 27, "2019-12-17", "13:00", 0, "", "", "");
String email = request.getEmail();
int requestId1 = request.getRequestId();
String requestId = Integer.toString(requestId1);
String name = request.getLegalName();
String cause = request.getCause();
String doa = request.getDateOfAppointment();
String toa = request.getAppointmentTime();
String generic = "Hello, " + name + "<br><br>" + "Your request with Request ID: "
+ requestId + " for " + "the cause of "
+ cause + " on " + doa + " at " + toa + " has been ";
String regards = "<br><br><br>Thanks and Regards,<br>";
String jsonRequest = objectMapper.writeValueAsString(request);
this.mockMvc.perform(post("/sendemail").content(jsonRequest).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk());
// assert
WiserAssertions.assertReceivedMessage(wiser)
.from("<sender-mail-id>")
.to(email)
.withSubject("Regarding your Appointment Request")
.withContent(generic + "<strong>RECEIVED</strong>." + regards);
}
这是我从博客中获得的 WiserAssertions.java
public class WiserAssertions {
private final List<WiserMessage> messages;
private WiserAssertions(List<WiserMessage> messages) {
this.messages = messages;
}
public static WiserAssertions assertReceivedMessage(Wiser wiser) {
return new WiserAssertions(wiser.getMessages());
}
private static Supplier<AssertionError> assertionError(String errorMessage, String... args) {
return () -> new AssertionError(MessageFormat.format(errorMessage, args));
}
public static <T> T unchecked(ThrowingSupplier<T> supplier) {
try {
return supplier.get();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
public WiserAssertions from(String from) {
findFirstOrElseThrow(m -> m.getEnvelopeSender().equals(from),
assertionError("No message from [{0}] found!", from));
return this;
}
public WiserAssertions to(String to) {
findFirstOrElseThrow(m -> m.getEnvelopeReceiver().equals(to),
assertionError("No message to [{0}] found!", to));
return this;
}
public WiserAssertions withSubject(String subject) {
Predicate<WiserMessage> predicate = m -> subject.equals(unchecked(getMimeMessage(m)::getSubject));
findFirstOrElseThrow(predicate,
assertionError("No message with subject [{0}] found!", subject));
return this;
}
public WiserAssertions withContent(String content) {
findFirstOrElseThrow(m -> {
ThrowingSupplier<String> contentAsString =
() -> ((String) getMimeMessage(m).getContent()).trim();
return content.equals(unchecked(contentAsString));
}, assertionError("No message with content [{0}] found!", content));
return this;
}
private void findFirstOrElseThrow(Predicate<WiserMessage> predicate, Supplier<AssertionError> exceptionSupplier) {
messages.stream().filter(predicate)
.findFirst().orElseThrow(exceptionSupplier);
}
private MimeMessage getMimeMessage(WiserMessage wiserMessage) {
return unchecked(wiserMessage::getMimeMessage);
}
interface ThrowingSupplier<T> {
T get() throws Throwable;
}
}