在使用 Mockito 进行单元测试时,我需要您的帮助。我的项目只有 DAO 和服务,所以它是 Web 项目。我有接口EmailDao
,它是实现EmailDaoImpl
类。现在我有简单的 jUnit 测试来测试EmailDaoImpl
. 这是一种sendEmail(EmailParams params)
执行操作的方法,例如身份验证在对象中正确设置参数EmailParams
,调用其他方法将电子邮件副本保存到文件系统等。当然,发送电子邮件。
我的实际测试类如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/applicationContext.xml"})
@Transactional
@TransactionConfiguration(transactionManager = "txManagerTest")
public class EmailServiceTest {
@Autowired
private EmailDao emailDao;
@Test
//@Rollback(false)
public void sendSignedEmailTest() throws Exception {
System.out.println("-------------------------");
System.out.println("sendSignedEmail()");
System.out.println("-------------------------");
String body = "Hello, I'm evil!";
EmailParams params = new EmailParams();
params.setIsCourt(true);
params.setBody(body);
params.setFileName("test.eml");
params.setSaveToFile(false);
params.setSignMessage(false);
params.setFromAddress("xxxx@seznam.cz");
params.setToAddresses("yyyyy@gmail.com");
//params.setMailPriority(5);
//params.setCcs("zzzzz@seznam.cz");
params.setSubject("test");
params.setUserName("aaa");
List<Long> debtIds = new ArrayList<>();
debtIds.add(123018L);
//debtIds.add(184788L);
//debtIds.add(185864L);
params.setDebtIds(debtIds);
DebtEvent event = new DebtEvent();
event.setEventTypeId(5);
event.setStatusTypeId(50);
event.setDescription("description");
event.setEventText("event text");
event.setEventClientText("event client text");
params.setSaveDebtEvent(false);
params.setDebtEventTemplate(event);
boolean ff = emailDao.sendEmail(params);
System.out.println(ff);
}
现在我不想真正向目标地址发送电子邮件。所以,我拿 Mockito:
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(locations = {"classpath:/applicationContext.xml"})
@Transactional
@TransactionConfiguration(transactionManager = "txManagerTest")
public class EmailServiceTest {
@Mock
private EmailDao emailDao;
@Test
//@Rollback(false)
public void sendSignedEmailTest() throws Exception {
System.out.println("-------------------------");
System.out.println("sendSignedEmail()");
System.out.println("-------------------------");
String body = "Hello, I'm evil!";
EmailParams params = new EmailParams();
params.setIsCourt(true);
params.setBody(body);
params.setFileName("test.eml");
params.setSaveToFile(false);
params.setSignMessage(false);
params.setFromAddress("xxxx@seznam.cz");
params.setToAddresses("yyyyy@gmail.com");
//params.setMailPriority(5);
//params.setCcs("zzzzz@seznam.cz");
params.setSubject("test");
params.setUserName("aaa");
List<Long> debtIds = new ArrayList<>();
debtIds.add(123018L);
//debtIds.add(184788L);
//debtIds.add(185864L);
params.setDebtIds(debtIds);
DebtEvent event = new DebtEvent();
event.setEventTypeId(5);
event.setStatusTypeId(50);
event.setDescription("description");
event.setEventText("event text");
event.setEventClientText("event client text");
params.setSaveDebtEvent(false);
params.setDebtEventTemplate(event);
Mockito.when(emailDao.sendEmail(params)).thenReturn(Boolean.TRUE);
System.out.println(emailDao.sendEmail(params));
}
这段代码总是返回 true(没关系),但是 sendEmail 方法没有经过测试!
你能帮我解决这个问题吗?我不知道我还必须做什么:-(
谢谢!
EDIT:// 这是 EmailDaoImpl 类中的 sendEmail(EmailParams params) :
@Override
public boolean sendEmail(EmailParams params) throws MessagingException, EmailException {
String from = params.getFromAddress();
String to = params.getToAddresses();
String ccs = params.getCcs();
String bccs = params.getBccs();
String subject = params.getSubject();
String body = params.getBody();
boolean signMessage = params.isSignMessage();
boolean saveToFile = params.isSaveToFile();
String fileName = params.getFileName();
boolean isCourt = params.isIsCourt();
List<Long> debtIds = params.getDebtIds();
String userName = params.getUserName();
String priority = (params.getMailPriority() == null || params.getMailPriority() == 0) ? "3" : params.getMailPriority().toString();
String message;
// remove any last semicolon
if (StringUtils.hasText(to)) {
if (to.endsWith(";")) {to = to.substring(0, to.length()-1);}
if (to.startsWith(";")) {to = to.substring(1, to.length());}
}
if (StringUtils.hasText(bccs)) {
if (bccs.endsWith(";")) {bccs = bccs.substring(0, bccs.length()-1);}
if (bccs.startsWith(";")) {bccs = bccs.substring(1, bccs.length());}
}
if (StringUtils.hasText(ccs)) {
if (ccs.endsWith(";")) {ccs = ccs.substring(0, ccs.length()-1);}
if (ccs.startsWith(";")) {ccs = ccs.substring(1, ccs.length());}
}
// addresses checking
if (StringUtils.hasText(to)) {
to = this.checkEmailAddress(to);
}
else {
message = messageSource.getMessage("email.NoRecipientAddress.message", null, LocaleContextHolder.getLocale());
throw new EmailException(message);
}
if (StringUtils.hasText(bccs)) {
bccs = this.checkEmailAddress(bccs);
}
if (StringUtils.hasText(ccs)) {
ccs = this.checkEmailAddress(ccs);
}
// unless priority is set correctly, the end
if (!priority.matches("[135]")) {
message = messageSource.getMessage("email.InvalidPriorityType.message", null, LocaleContextHolder.getLocale());
throw new EmailException(message);
}
byte[] bytes = null;
// I get a template events
DebtEvent debtEventTemplate = params.getDebtEventTemplate();
// if to store the event
if (params.isSaveDebtEvent()) {
// If the template is not defined, then the end
if (debtEventTemplate == null) {
message = messageSource.getMessage("email.InvalidDebtEventTemplate.message", null, LocaleContextHolder.getLocale());
throw new EmailException(message);
}
// if the event does not have date, complementing the current date
if (debtEventTemplate.getEventDate() == null) debtEventTemplate.setEventDate(Calendar.getInstance());
// specify type of storage
debtEventTemplate.setRepositoryId((isCourt) ? 2 : 1);
}
// create a collection of the SMTP server settings
Properties props = System.getProperties();
props.put("mail.smtp.host", mailHost);
props.put("mail.smtp.port", mailPort);
props.put("mail.transport.protocol", mailProtocol);
props.put("mail.smtp.auth", mailAuth);
props.put("mail.smtps.debug", mailDebug);
// create email session
Session session = Session.getDefaultInstance(props, null);
// based on session I create the unsigned MimeMessage and configure it
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setText(body);
mimeMessage.setFrom(new InternetAddress(from));
mimeMessage.setRecipients(Message.RecipientType.TO, this.stringToEmailAddresses(to));
mimeMessage.setRecipients(Message.RecipientType.CC, this.stringToEmailAddresses(ccs));
mimeMessage.setRecipients(Message.RecipientType.BCC, this.stringToEmailAddresses(bccs));
mimeMessage.setSubject(subject);
mimeMessage.setSentDate(new Date());
mimeMessage.setHeader("X-Priority", priority);
mimeMessage.saveChanges();
// setup priority
switch (priority) {
case "1":
mimeMessage.setHeader("X-Priority", priority);
mimeMessage.setHeader("X-MSMail-Priority", "High");
mimeMessage.setHeader("Importance", "High");
break;
case "3":
mimeMessage.setHeader("X-Priority", priority);
mimeMessage.setHeader("X-MSMail-Priority", "Normal");
mimeMessage.setHeader("Importance", "Normal");
break;
case "5":
mimeMessage.setHeader("X-Priority", priority);
mimeMessage.setHeader("X-MSMail-Priority", "Low");
mimeMessage.setHeader("Importance", "Low");
break;
default:
mimeMessage.setHeader("X-Priority", priority);
mimeMessage.setHeader("X-MSMail-Priority", "Normal");
mimeMessage.setHeader("Importance", "Normal");
break;
}
// If the email sign
if (signMessage) {
// create and configure MailcapCommandMap and MIME types used for SMIME
final MailcapCommandMap mailcap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mailcap.addMailcap("application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature");
mailcap.addMailcap("application/pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_mime");
mailcap.addMailcap("application/x-pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_signature");
mailcap.addMailcap("application/x-pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_mime");
mailcap.addMailcap("multipart/signed;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.multipart_signed");
CommandMap.setDefaultCommandMap(mailcap);
// sign message
MimeMultipart mm = null;
try {
mm = signMessage(mimeMessage);
} catch (KeyStoreException ex) {
Logger.getLogger(EmailDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(EmailDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(EmailDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
} catch (CertificateException ex) {
Logger.getLogger(EmailDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnrecoverableKeyException ex) {
Logger.getLogger(EmailDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidAlgorithmParameterException ex) {
Logger.getLogger(EmailDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchProviderException ex) {
Logger.getLogger(EmailDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
} catch (CertStoreException ex) {
Logger.getLogger(EmailDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
} catch (SMIMEException ex) {
Logger.getLogger(EmailDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(EmailDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
}
// Copied headers from the original unsigned messages to new message
final Enumeration headers = mimeMessage.getAllHeaderLines();
// create a new envelope
mimeMessage = new MimeMessage(session);
while (headers.hasMoreElements()){
mimeMessage.addHeaderLine((String)headers.nextElement());
}
// I put the signed content
mimeMessage.setContent(mm);
mimeMessage.saveChanges();
}
// If email have to save to file
if (saveToFile) {
// too many lines of code
}
// send email
Transport.send(mimeMessage);
// return true
return true;
}