我是Mockito的新手,面对RestTemplate的空指针异常。有人可以帮忙,让我知道我的模拟服务有什么问题。
服务:
public List<String> getval(String id) {
String path = "/abc" + id;
ResponseEntity<tnclass> RE = restTemplate.getForEntity(id, tnClass.class);
if (RE != null && RE.getStatusCode() == HttpStatus.OK){
return RE.getBody().gettnNumbers();
}else{
return Collections.<String>emptyList();
}
}
MockService:
private RestTemplate res = mock(RestTemplate.class);
public List<String> getval(String id) {
ResponseEntity<tnClass> my = mock(ResponseEntity.class);
tnClass tn = mock(tnClass.class);
List<String> l = new ArrayList();
l.add(0, "t1");
l.add(1, "t2");
tn.setId(id);
tn.settnNumbers(l);
Mockito.when(restTemplate.getForEntity(path, tnClass.class, tn)).thenReturn(my);
List<String> finallist = my.getBody().gettnNumbers();
return finallist;
}