Java @PutMapping抛出InvocationTargetException的JUnit RestControllerTest
我正在用Spring Boot构建一个微服务。我用GET、POST、PUT、DELETE方法编写了一个API,运行应用程序并使用Postman进行了测试-一切正常 但是测试PUT方法失败了 java.lang.AssertionError:状态应为:但为:Java @PutMapping抛出InvocationTargetException的JUnit RestControllerTest,java,spring,spring-boot,mockito,invocationtargetexception,Java,Spring,Spring Boot,Mockito,Invocationtargetexception,我正在用Spring Boot构建一个微服务。我用GET、POST、PUT、DELETE方法编写了一个API,运行应用程序并使用Postman进行了测试-一切正常 但是测试PUT方法失败了 java.lang.AssertionError:状态应为:但为: @PutMapping(value = "/{id}") public ResponseEntity updateSongById(@PathVariable("id") Integer id, @Re
@PutMapping(value = "/{id}")
public ResponseEntity updateSongById(@PathVariable("id") Integer id, @RequestBody @Validated
SongDto songDto) {
// TODO Add authorization
SongDto song = songService.getSongById(id);
if (song == null)
return new ResponseEntity(HttpStatus.BAD_REQUEST);
return new ResponseEntity(songService.updateSong(id, songDto), HttpStatus.NO_CONTENT);
}
@Override
public SongDto getSongById(Integer id) {
return songMapper.songToSongDto(songRepository.findById(id)
.orElseThrow(NotFoundException::new));
}
在调试模式下运行测试并单步抛出抛出InvocationTargetException:
我的RestController方法如下所示:
@PutMapping(value = "/{id}")
public ResponseEntity updateSongById(@PathVariable("id") Integer id, @RequestBody @Validated
SongDto songDto) {
// TODO Add authorization
SongDto song = songService.getSongById(id);
if (song == null)
return new ResponseEntity(HttpStatus.BAD_REQUEST);
return new ResponseEntity(songService.updateSong(id, songDto), HttpStatus.NO_CONTENT);
}
@Override
public SongDto getSongById(Integer id) {
return songMapper.songToSongDto(songRepository.findById(id)
.orElseThrow(NotFoundException::new));
}
songService.getSongById(id):
@PutMapping(value = "/{id}")
public ResponseEntity updateSongById(@PathVariable("id") Integer id, @RequestBody @Validated
SongDto songDto) {
// TODO Add authorization
SongDto song = songService.getSongById(id);
if (song == null)
return new ResponseEntity(HttpStatus.BAD_REQUEST);
return new ResponseEntity(songService.updateSong(id, songDto), HttpStatus.NO_CONTENT);
}
@Override
public SongDto getSongById(Integer id) {
return songMapper.songToSongDto(songRepository.findById(id)
.orElseThrow(NotFoundException::new));
}
SongRepository只是一个扩展JpaRepository的简单接口
我的失败测试如下所示:
@Test
void updateSongById_success() throws Exception {
when(songService.updateSong(anyInt(), any(SongDto.class))).thenReturn(getValidSongDto());
String songDtoJson = objectMapper.writeValueAsString(getValidSongDto());
mockMvc.perform(put("/rest/v1/songs/1")
.content(songDtoJson)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
}
getValidSongDto()只提供了一个在我的测试中使用的Dto:
private SongDto getValidSongDto() {
return SongDto.builder()
.id(1)
.title("TestSongValid")
.label("TestLabelValid")
.genre("TestGenreValid")
.artist("TestArtistValid")
.released(1000)
.build();
}
我现在真的不明白,我做错了什么使这次考试不及格,到目前为止,在互联网上也找不到任何帮助我解决这个问题的东西。因此,如果有人能告诉我这里出了什么问题以及如何解决这个问题,我将非常感激
非常感谢 您需要返回
songService.getSongById
的值,如下所示
@Test
void updateSongById_success() throws Exception {
when(songService.getSongById(Mockito.any())).thenReturn(getValidSongDto());
when(songService.updateSong(anyInt(), any(SongDto.class))).thenReturn(getValidSongDto());
String songDtoJson = objectMapper.writeValueAsString(getValidSongDto());
mockMvc.perform(put("/rest/v1/songs/1")
.content(songDtoJson)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
}
您没有为
songService.getSongById
定义任何内容,这将导致null
响应并导致400错误。实际上,这永远不会发生,因为会抛出异常。也就是说,你的回答也很奇怪,你给出的状态没有内容,但是你包含了内容。非常感谢。这很好——我的考试现在很快乐,我也是。@Mahakala108,你可以接受答案,这样其他人就会知道答案被接受了