2017-09-05 15 views
1

아래 테스트를 실행 한 후 다음 예외가 표시됩니다. 코드베이스에서 변경된 내용은 모두 SimpleMailMessage 대신 MimeMessage의 사용법입니다.SimpleMailMessage에서 MimeMessage로 코드 기반을 변경 한 후에 유닛 테스트가 중단됩니다.

안내하십시오.

예외

------------------------------------------------------- 
T E S T S 
------------------------------------------------------- 
Running com.ebayenterprise.publicapi.events.webhooks.WebHookMailSenderServiceTest 
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.548 sec <<< FAILURE! - in com.ebayenterprise.publicapi.events.webhooks.WebHookMailSenderServiceTest 
sendNotification(com.ebayenterprise.publicapi.events.webhooks.WebHookMailSenderServiceTest) Time elapsed: 1.159 sec <<< FAILURE! 
org.mockito.exceptions.verification.junit.ArgumentsAreDifferent: 
Argument(s) are different! Wanted: 
mailSender.send(
    (hasProperty("from", "[email protected]")) 
); 
-> at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
Actual invocation has different arguments: 
mailSender.send(
    mimeMessage 
); 
-> at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124) 
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200) 
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153) 
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103) 

Java HotSpot(TM) 64-Bit Server VM warning: ignoring option UseSplitVerifier; support was removed in 8.0 

Results : 

Failed tests: 
    WebHookMailSenderServiceTest.sendNotification 
Argument(s) are different! Wanted: 
mailSender.send(
    (hasProperty("from", "[email protected]")) 
); 
-> at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
Actual invocation has different arguments: 
mailSender.send(
    mimeMessage 
); 
-> at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 


Tests run: 1, Failures: 1, Errors: 0, Skipped: 0 

------------------------------------------------------------------------ 
BUILD FAILURE 
------------------------------------------------------------------------ 
Total time: 10.053 s 
Finished at: 2017-09-05T15:24:11-04:00 
Final Memory: 19M/318M 
------------------------------------------------------------------------ 

WebHookMailSenderServiceTest.java

public class WebHookMailSenderServiceTest { 

    @Rule 
    public MockitoJUnitRule rule = new MockitoJUnitRule(this); 
    private static final String EMAIL_SUBJECT = "Error Report For Webhooks Job Status"; 
    private static final String EMAIL_RECIPIENT_ADDRESS = "[email protected]"; 
    private static final String EMAIL_RECIPIENT_ADDRESSES = "[email protected],[email protected]"; 
    private static final String EMAIL_SENDER_ADDRESS = "[email protected]"; 
    private static final int SUCCESS_ROUTE_ID = 101; 
    private static final int FAILURE_ROUTE_ID = 201; 
    private static final String STORE_ID = "TMSUS"; 
    private static final String ENDPOINT = "http://tmsus-endpoint.com/pss"; 
    private static final String EVENT_TYPE = "PaymentSettlementStaus"; 
    private static final int MESSAGES_PROCESSED = 30; 
    private static final String STATUS = "Success"; 
    private static final String ERROR_MESSAGE = "404 (Not Found)"; 
    private final Map<Integer, WebHookJobStatusCollector> successCollectorMap = new HashMap<>(); 
    private final Map<Integer, WebHookJobStatusCollector> failureCollectorMap = new HashMap<>(); 
    private WebHookMailSenderService webHookMailSenderService; 
    private WebHookMailSenderService webHookMailSenderServiceWMultiRecipients; 
    @Mock 
    private JavaMailSender mailSender; 
    @Mock 
    private MimeMessage mimeMessage; 

    @Before 
    public void setup() { 
     webHookMailSenderService = new WebHookMailSenderService(EMAIL_SENDER_ADDRESS, EMAIL_RECIPIENT_ADDRESS, mailSender); 
     successCollectorMap.put(SUCCESS_ROUTE_ID, new WebHookJobStatusCollector(STORE_ID, ENDPOINT, EVENT_TYPE, MESSAGES_PROCESSED, STATUS, null)); 
     failureCollectorMap.put(FAILURE_ROUTE_ID, new WebHookJobStatusCollector(STORE_ID, ENDPOINT, EVENT_TYPE, MESSAGES_PROCESSED, STATUS, ERROR_MESSAGE)); 
    } 

    @Test 
    public void sendNotification() throws MessagingException { 
     when(mailSender.createMimeMessage()).thenReturn(mimeMessage); 
     webHookMailSenderService.sendNotification(successCollectorMap, failureCollectorMap); 
     verify(mailSender).send((MimeMessage) argThat(allOf(
       hasProperty("from", equalTo(EMAIL_SENDER_ADDRESS)) 
     ))); 
    } 

    @After 
    public void cleanup() { 
     webHookMailSenderService = null; 
     assertNull(webHookMailSenderService); 
    } 
} 

WebHookMailSenderService.java

public class WebHookMailSenderService { 

    private static final Logger LOGGER = LoggerFactory.getLogger(WebHookMailSenderService.class); 
    private static final String EMAIL_SUBJECT = "Error Report For Webhooks Job Status"; 
    private String emailSenderAddress; 
    private JavaMailSender mailSender; 
    private String[] emailRecipientAddresses; 
    private static final FastDateFormat EXECUTION_TIME_DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss Z"); 
    private static final String VELOCITY_TEMPLATE_NAME = "webhooksjobstatus-error-summary.vm"; 
    private static final boolean HTML_FLAG = true; 

    public WebHookMailSenderService() { 
    } 

    public WebHookMailSenderService(String emailSenderAddress, String emailRecipientAddresses, JavaMailSender mailSender) { 
     this.emailSenderAddress = emailSenderAddress; 
     this.emailRecipientAddresses = StringUtils.split(emailRecipientAddresses, ','); 
     this.mailSender = mailSender; 
    } 

    public void sendNotification(Map<Integer, WebHookJobStatusCollector> successCollectorMap, 
      Map<Integer, WebHookJobStatusCollector> failureCollectorMap) throws MessagingException { 
     MimeMessage message = mailSender.createMimeMessage(); 
     MimeMessageHelper helper = new MimeMessageHelper(message); 
     helper.setSubject(EMAIL_SUBJECT + " on " + GenericUtil.getHostName()); 
     helper.setFrom(emailSenderAddress); 
     helper.setTo(emailRecipientAddresses); 
     String emailContent = getEmailContent2(successCollectorMap, failureCollectorMap); 
     helper.setText(emailContent, HTML_FLAG); 
     mailSender.send(message); 
    } 

    //code removed for brevity 
} 

답변

1
생산 코드에

@Mock 
private MimeMessage mimeMessage; 

when(mailSender.createMimeMessage()).thenReturn(mimeMessage); 

이 같은 예를 들어 채워집니다 :

helper.setFrom(emailSenderAddress); 

그러나 생산 코드가 조롱 인스턴스와 작동하기 때문에 아무 일도없는 모두 무시 테스트에서

당신은이 message 조롱 제공 setXXX() 호출. send() 전화를 결과로

는 여전히 빈 조롱 message와 함께 작동 : MimeMessage의 일부 스텁과 테스트 작업에

mailSender.send(message); 

시도 :

when(mailSender.createMimeMessage()).thenReturn(new MimeMessage(mockedSession)); 

이 경우 MimeMessageHelper에 의해 설정 데이터를 조롱하지 않은 실제 인스턴스로 가져옵니다.