我对Mockito有一个关于单元测试的问题。我创建了MVC架构模式,应该测试我的控制器,该控制器可与输入表单控制台一起使用。在我的测试中,我模仿了从控制台输入的内容,但是老师说我不应该在那里使用某些类。
我有Student.class这是我的模型。此类具有-> setId,setName,setSubjects,getId,getName和getSubjects之类的方法,以及toString()方法。
这是我的观点:
public class View {
private Student student;
private DiplomaStudent diplomaStudent;
public View() {
this.student = new Student();
this.diplomaStudent = new DiplomaStudent();
}
public void inoutStudent() {
System.out.println("Enter id: ");
Scanner input = new Scanner(System.in);
Integer id = input.nextInt();
student.setId(id);
System.out.println("Enter name: ");
Scanner inputName = new Scanner(System.in);
String name = inputName.nextLine();
student.setStudentName(name);
System.out.println("Enter course");
Scanner inputCourse = new Scanner(System.in);
Integer course = inputCourse.nextInt();
student.setCourse(course);
System.out.println(student.getId() + " " + student.getStudentName() + " " + student.getCourse());
}
public String inputing() {
System.out.println("Enter name: ");
Scanner inputName = new Scanner(System.in);
String name = inputName.nextLine();
return name;
}
}
这是我的控制器:
public class Controller {
private Student student;
private DiplomaStudent diplomaStudent;
private View view;
public Controller() {
this.student = new Student();
this.diplomaStudent = new DiplomaStudent();
this.view = new View();
}
public void startStud() {
view.inoutStudent();
}
public String inpt() {
return view.inputing();
}
}
在测试中,我使用View.class进行了类似的模仿
@Test
@DisplayName("test immitation from console input")
public void testView(){
View view = new View();
String inputName = "Jhon";
InputStream in = new ByteArrayInputStream(inputName.getBytes());
System.setIn(in);
assertEquals("Jhon", view.inputing());
}
但是我应该为控制器写同样的东西
@Test
public void testController() {
Controller controller = mock(Controller.class);
/*String inputName = "Jhon";
InputStream in = new ByteArrayInputStream(inputName.getBytes());
System.setIn(in);*/
//what i should write here
assertEquals("Jhon", controller.inpt());
}
所以,我应该为控制器测试写些什么,请帮帮我。