2015-04-05 20 views
0
  • 使用讹诈和JUnit与mokito但Junit的mokito util.properties显示空moking后

  • 我写电子邮件的测试,当我使用
    时(emailproperties.getUsername())。thenReturn(“ABC @ gmail.com“);

  • 显示空

  • 这里是我的代码

公共类EmailServiceImplTest {

@InjectMocks 
EmailServiceImpl emailServiceImpl = new EmailServiceImpl(); 
@Mock 
private MessageTemplateService messageTemplateService; 
@Mock 
private EmailProperties emailProperties; 
//private static final Logger LOGGER = Logger.getLogger(EmailServiceImplTest.class); 

private GreenMail greenMail; 
private static final String USER_NAME = "hascode"; 
    private static final String EMAIL_USER_ADDRESS = "[email protected]"; 
    private static final String EMAIL_TO = "[email protected]"; 
    private static final String EMAIL_SUBJECT = "Test E-Mail"; 
    private static final String EMAIL_TEXT = "This is a test e-mail."; 
    private static final String LOCALHOST = "localhost"; 
    private static final String USER_PASSWORD = "abcdef123"; 

@Before 
public void testSmtpInit() { 
    MockitoAnnotations.initMocks(this); 
    greenMail = new GreenMail(ServerSetupTest.SMTP); 
    greenMail.start(); 
    greenMail.setUser(EMAIL_USER_ADDRESS, USER_NAME, USER_PASSWORD); 
} 


@Test 
public void sendContactEmail() { 
    MessageTemplate mock_template=new MessageTemplate(); 
    mock_template.setId(2); 
    mock_template.setBody("Hi ${name} want to contact"); 
    mock_template.setSubject("Contact EMAIL"); 
    ContactDTO mock_Dto=new ContactDTO(); 
    mock_Dto.setFirstName("abc"); 
    mock_Dto.setLastName("xyz"); 
    Properties mock_props = System.getProperties(); 
    mock_props.put("mail.smtp.host", LOCALHOST); 
    mock_props.put("mail.smtp.auth", "true"); 
    mock_props.put("mail.smtp.port", ServerSetupTest.SMTP.getPort()); 
    mock_props.put("mail.debug", "true"); 

    when(messageTemplateService.getMessageTemplateById("2")).thenReturn(mock_template); 
    emailProperties.setAdminTo("[email protected]"); 
    when(emailProperties.getAdminTo()).thenReturn("[email protected]"); 
    when(emailProperties.getContactMsgKey()).thenReturn("2"); 
    when(emailProperties.getProps()).thenReturn(mock_props); 
    when(emailProperties.getSenderEmail()).thenReturn(EMAIL_USER_ADDRESS); 
    when(emailProperties.getSender()).thenReturn(USER_NAME); 
    when(emailProperties.getHost()).thenReturn(LOCALHOST); 
    when(emailProperties.getUserName()).thenReturn(EMAIL_USER_ADDRESS); 
    when(emailProperties.getPassword()).thenReturn(USER_PASSWORD); 


    emailServiceImpl.sendContactEmail(mock_Dto); 
    MimeMessage[] messages = greenMail.getReceivedMessages(); 
    assertNotNull(messages); 
    assertEquals(1, messages.length); 


} 
  • 当我调试它显示空我在做什么wronge
+0

我不太明白你要问什么。您希望mockito发送消息的问题是?或者是因为消息变量为空?如果是后者,那不是因为你没有模拟'greenMail.getReceivedMessages()'。 – 2015-04-05 13:43:06

+0

我的问题是我嘲笑emailProperties,但是当我调试时,我发现emailProperties不是调用when()返回vakue。 – 2015-04-05 14:16:01

+0

我试过了,但它确实返回了我之前设置的值。你如何调试它?你不是直接指向该领域的权利?你怎么试着检查这个函数'emailproperties.getUsername()'并且看看结果是什么。 – 2015-04-05 15:22:53

回答

0

是完成。更改为@spy @Mock private EmailProperties emailProperties;

  • 并设置值,因为它是。
  • 为期15天的努力。