Mockito模拟NullPointerException,不知道还有什么可以尝试的]] << [

问题描述 投票:0回答:1
Java认为我的模拟课程类对象在when()。thenReturn()行处为null。

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..);

产生:

MissingMethodInvocationException:when()需要一个参数,该参数必须是“模拟的方法调用”

Java认为我的模拟课程类对象在when()。thenReturn()行处为null。 class StudentTest {@模拟课程courseMock1; @Test void ...
java nullpointerexception mocking mockito
1个回答
0
投票
StudentTest注释@RunWith(MockitoJUnitRunner.class)类。
© www.soinside.com 2019 - 2024. All rights reserved.