模拟对象返回NPE
@SpringBootTest
@RunWith(MockitoJUnitRunner.class)
public class EmpCompSTest {
@Mock
private CompCl compCl;
@Test
public void testGetEmpCompS() {
EmpRequest request = new EmpRequest();
Map<Long, List<CompCl.CompeSc>> comp = new HashMap<>();
CompCl.CompSc compSc1
= new CompCl.CompSc();
comp.put(1L, Arrays.asList(compSc1));
when(compCl.getUsrSc(request.getComp().keySet())).thenReturn(comp);
}
}
调试时,when()
所在行弹出 NullPointerException,且类 compCL
为 null
。
@SpringBootTest
@RunWith(MockitoJUnitRunner.class)
public class EmpCompSTest {
@Mock
private CompCl compCl;
@Test
public void testGetEmpCompS() {
EmpRequest request = new EmpRequest();
Map<Long, List<CompCl.CompeSc>> comp = new HashMap<>();
CompCl.CompSc compSc1
= new CompCl.CompSc();
comp.put(1L, Arrays.asList(compSc1));
when(compCl.getUsrSc(request.getComp().keySet())).thenReturn(comp);
}
}
When debugging, an NullPointerException pops up on the line with when()
and the class compCL
is null
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

这是Junit5(木星)吗?在这种情况下,请使用
@ExtendWith(MockitoExtension.class)
而不是@RunWith(MockitoJUnitRunner.class)
。Is this Junit5 (Jupiter)? In that case use
@ExtendWith(MockitoExtension.class)
instead of@RunWith(MockitoJUnitRunner.class)
.