如何模拟这门课?

时间:2017-10-02 15:14:41

标签: java testing mocking mockito powermockito

我是一名真正需要帮助的学生,如何模拟这门课程或其任何部分。我已经尝试了一般方法,但它不起作用。我认为我缺乏知识,我需要任何知道如何嘲笑这些以帮助我的人的帮助。
我不知道该怎么做

package IT_chatbot;

import java.util.Properties;
import java.util.Scanner;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class request_remark {
private static Scanner scan = new Scanner(System.in);
public static String remark(String studentID){

    String courseCode="";

    System.out.println("Please enter course Code to be remarked:");
    courseCode = scan.nextLine();
    System.out.println("The message is sending...");


    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");     
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
   Session session = Session.getInstance(props,new javax.mail.Authenticator()
   {
      protected PasswordAuthentication getPasswordAuthentication() 
      {
         return new PasswordAuthentication("kmitl@gmail.com","2016mmm");
      }
  });

  try
  {   
   MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress("kmitl@gmail.com"));
      message.addRecipient(Message.RecipientType.TO,new InternetAddress("wn1apc@gmail.com"));
      message.setSubject("Request a remark for a recent past of"+studentID);
      StringBuffer emailMessage = new StringBuffer("Dear Hades");
       emailMessage.append("\r\n");
       emailMessage.append("We've recieved the remark request from student number " + studentID+ " subject " + courseCode);
       emailMessage.append("\r\n");
       emailMessage.append("\r\n");
       emailMessage.append("\r\n");
       emailMessage.append("Best regard,");
       emailMessage.append("\r\n");
       emailMessage.append("\r\n");
      message.setText(emailMessage.toString());
      Transport.send(message);


   }
   catch(Exception e)
   {
     e.printStackTrace();
   }
return courseCode;
}
}

1 个答案:

答案 0 :(得分:0)

首先,您的类名应该类似于RequestRemark(遵循约定)。

如果你想嘲笑你的课程,你只需要去你的考试班,做一些像:

private RequestRemark mockRequestRemark = mock(RequestRemark.class);

但如果你想要的是测试你的方法,你必须创建你的类的对象:

private RequestRemark requestRemark = new (RequestRemark);

然后为您班级的每个方法进行测试。

例如:

@Test
public void whenRemark_givenStudentID_thenWhatYouWantToAssert(){
  String id = "";
  requestMark.remark(id);

  assertThat(yourAssertion);
}

pd:我认为您在代码中留下了电子邮件的密码......