简体   繁体   中英

How to mock this class?

I'm a student who really needs help with how to mock this class or any part of it. I've tried the general way but it's not working. I think I'm lacking the knowledge and I need help from anyone who knows how to mock these in any way to help me.
I have no idea how to do it

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;
}
}

first of all, your class name should be something like RequestRemark (to follow the conventions).

If you want to mock your class you just have to go to your test class and do something like:

private RequestRemark mockRequestRemark = mock(RequestRemark.class);

but if what you want is to test your methods, you have to create an object of your class:

private RequestRemark requestRemark = new (RequestRemark);

and then make tests for each method of your class.

For example:

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

  assertThat(yourAssertion);
}

pd: I think you left your email's password in your code...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM