Java mockito在模拟对象上引发空指针异常
我是莫基托的新手。我做了很多调试,但仍然找不到解决方案。我的测试是为其中一个模拟对象方法调用抛出空指针异常,因为它正在为另一个模拟对象传递。下面是我的代码Java mockito在模拟对象上引发空指针异常,java,spring,testing,mockito,testng,Java,Spring,Testing,Mockito,Testng,我是莫基托的新手。我做了很多调试,但仍然找不到解决方案。我的测试是为其中一个模拟对象方法调用抛出空指针异常,因为它正在为另一个模拟对象传递。下面是我的代码 public class POServiceImpl implements POService { private final PODao poDao; @Inject public POServiceImpl(PODao poDao){ this.poDao = poDao;
public class POServiceImpl implements POService {
private final PODao poDao;
@Inject
public POServiceImpl(PODao poDao){
this.poDao = poDao;
}
@Autowired
CCPOService ccPOService ;
@Override
public Set<String> getPO(UUID a, UUID b) {
Set<String> po= poDao.getPO(a, b);
Set<String> ccpo= ccPOService.getPO(a, b);
return Stream.concat(ccpo.stream(), po.stream()).collect(Collectors.toSet());
}
}
public class CCPOServiceImpl implements CCPOService {
private final CCPODao ccpoDao;
@Inject
public POServiceImpl(CCPODao ccpoDao){
this.ccpoDao = ccpoDao;
}
@Override
public Set<String> getPO(UUID a, UUID b) {
return ccpoDao.getPO(a, b);
}
}
公共类POServiceImpl实现posService{
私人决赛波多波多;
@注入
公共POServiceImpl(博道博道){
this.poDao=poDao;
}
@自动连线
CCPOService CCPOService;
@凌驾
公共集getPO(UUID a、UUID b){
设置po=poDao.getPO(a,b);
设置ccpo=ccPOService.getPO(a,b);
return Stream.concat(ccpo.Stream(),po.Stream()).collect(Collectors.toSet());
}
}
公共类CCPOServiceImpl实现CCPOService{
私人最终CCPODao CCPODao;
@注入
公共POServiceImpl(CCPODao CCPODao){
this.ccpoDao=ccpoDao;
}
@凌驾
公共集getPO(UUID a、UUID b){
返回ccpoDao.getPO(a,b);
}
}
我的测试课程如下
@Test
public class POServiceImplTest {
@Mock
PODao poDao;
@Mock
CCPOService ccpoService;
@BeforeMethod(alwaysRun = true)
public void beforeMethod() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testPO() {
final POService porderService = new POServiceImpl(poDao);
Set<String> res = new HashSet<String>();
res.add("PO");
Set<String> res1 = new HashSet<String>();
res1.add("CCPO");
when(poDao.getPO(UUID.fromString("6af27ba7-b668-4571-b673-4a79fc203429"), UUID.fromString("6af27ba7-b668-4571-b673-4a79fc203429"))).thenReturn(res);
when(ccpoService.getpo(UUID.fromString("6af27ba7-b668-4571-b673-4a79fc203429"), UUID.fromString("6af27ba7-b668-4571-b673-4a79fc203429"))).thenReturn(res1);
Set<String> result = porderService.getPO(UUID.fromString("6af27ba7-b668-4571-b673-4a79fc203429"), UUID.fromString("6af27ba7-b668-4571-b673-4a79fc203429"));
Assert.assertEquals(res.contains("PO"), result.contains("PO"));
Assert.assertEquals(res.contains("CCPO"), result.contains("CCPO"));
}
}
@测试
公共类POServiceImplTest{
@嘲弄
坡道坡道;
@嘲弄
CCPOService CCPOService;
@BeforeMethod(alwaysRun=true)
方法()之前的公共无效{
initMocks(this);
}
@试验
公开作废测试PO(){
最终POService porderService=新的POServiceImpl(poDao);
Set res=新的HashSet();
决议增补(“PO”);
Set res1=新的HashSet();
res1.添加(“CCPO”);
当(poDao.getPO(UUID.fromString(“6af27ba7-b668-4571-b673-4a79fc203429”)、UUID.fromString(“6af27ba7-b668-4571-b673-4a79fc203429”)。然后返回(res);
当(ccpoService.getpo(UUID.fromString(“6af27ba7-b668-4571-b673-4a79fc203429”)、UUID.fromString(“6af27ba7-b668-4571-b673-4a79fc203429”)。然后返回(res1);
Set result=porderService.getPO(UUID.fromString(“6af27ba7-b668-4571-b673-4a79fc203429”)、UUID.fromString(“6af27ba7-b668-4571-b673-4a79fc203429”);
Assert.assertEquals(res.contains(“PO”)、result.contains(“PO”);
Assert.assertEquals(res.contains(“CCPO”)、result.contains(“CCPO”);
}
}
测试用例为setpo=poDao.getPO(a,b)提供了正确的模拟Set对象代码>,但它正在为行Set ccpo=ccPOService.getPO(a,b)抛出空指针异常代码>
我相信@autowired ccPOService对象是空的。
任何需要的帮助当然,它会抛出一个NullPointerException
您永远不会为ccPOService
字段分配任何内容,因此它将保持null
。相反,将POService porderService
设置为一个字段,并使用@InjectMocks
进行注释,这样它将自动创建一个实例,并将模拟对象分配给其字段和构造函数(或者通过构造函数将两者都输入,这样更好!)。@InjectMocks帮助。谢谢