class StudentTest {
@Mock
Course courseMock1;
@Test
void student_getTeacherNames_should_return_list_of_full_names() {
when(courseMock1.getEAP()).thenReturn(1);
}
public class Course {
public Course(String courseName, String name, LocalDate startDate, LocalDate endDate, Integer EAP, Teacher teacher) {
this.courseName = courseName;
this.name = name;
this.startDate = startDate;
this.endDate = endDate;
this.EAP = EAP;
this.teacher = teacher;
}
public Integer getEAP() {
return EAP;
}
}
我尝试过:
@RunWith(MockitoJUnitRunner.class)
class StudentTest{...
-
@Before
public void setup(){
MockitoAnnotations.initMocks(this);
}
-
@Rule public Mocks mocks = new Mocks(this);
都没有解决NPE。
也尝试过(使用模拟的Teacher对象作为参数之一)
@Mock
Course courseMock1 = new Course(params..);
产生:Java认为我的模拟课程类对象在when()。thenReturn()行处为null。 class StudentTest {@模拟课程courseMock1; @Test void ...MissingMethodInvocationException:when()需要一个参数,该参数必须是“模拟的方法调用”
StudentTest
注释@RunWith(MockitoJUnitRunner.class)
类。