我正在尝试对该方法进行测试,它给了我org.springframework.web.util.NestedServletException:请求处理失败;嵌套的异常是java.lang.NullPointerException,我不知道为什么,请有人帮我,这是我试图实现的测试
public void updateProcedure() throws Exception {
Procedure procedure = new Procedure();
procedure.setId(1);
procedure.setContent("oumaima");
procedure.setDescription("test");
Media medias = new Media();
medias.setId(1);
medias.setType("image/png");
medias.setUrl("C:\\files\\actualite\\32769\\adobexd.png");
List<Media> allMedias = new ArrayList<Media>();
allMedias.add(medias);
procedure.setMedias(allMedias);
User user = new User();
user.setId(1);
user.setNom("oumaima");
procedure.setUser(user);
long id = 0L;
long id1 = 1L;
Mockito.when(procedureService.getProcedureById(Mockito.<Long>any())).thenReturn(new Procedure());
Mockito.when(procedureService.addOrEditProcedure(Mockito.any(Procedure .class))).thenReturn(procedure);
mockMvc.perform(put("/procedure/update/{id}",procedure.getId()).param("idDeleted",Long.toString(id))
.param("idDeleted",Long.toString(id1))
.param("content",procedure.getContent())
.param("description",procedure.getDescription())
)
.andExpect(status().isOk());
}
这是我的方法
@PutMapping("/procedure/update/{id}")
public Map<String,Object> editActualite(@PathVariable Long id, @Valid @RequestParam ArrayList<Long> idDeleted,
@Valid @RequestParam String content, @Valid @RequestParam String description){
Procedure proc = this.procService.getProcedureById(id);
if(content.length() !=0) {
proc.setContent(content);
}
if(description.length() !=0){
proc.setDescription(description);
}
if(idDeleted.size() != 0) {
for (int i = 0; i < idDeleted.size(); i++) {
this.mediaService.deleteMedia(idDeleted.get(i));
}
}
this.procService.addOrEditProcedure(proc);
return getProcedureWeb(proc);
}
public Map<String, Object> getProcedureWeb(Procedure procedure) {
List<Media> medias = procedure.getMedias();
List<MediaToByte> mediaToBytes = new ArrayList<MediaToByte>(); //here where I get the error
for (int i = 0; i < medias.size(); i++) {
MediaToByte mediaToByte = new MediaToByte(medias.get(i));
mediaToBytes.add(mediaToByte);
}
final Map<String, Object> act = new HashMap<String, Object>();
act.put("id", procedure.getId());
act.put("description", procedure.getDescription());
act.put("content", procedure.getContent());
act.put("medias", mediaToBytes);
act.put("user", procedure.getUser());
act.put("date", procedure.getDate());
return act;
}