単体テストに 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;
}