使用 spring 项目测试 JUnit 5 中的删除方法得到 "Name for argument of type [long] not specified" 错误
Testing delete method in JUnit 5 with spring project get "Name for argument of type [long] not specified" error
我对 spring 和 JUnit 很陌生。现在我尝试构建一个测试用例。在我 运行 这个
@Test
public void shouldBeOK_Found() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.delete("/scores/{id}", 1L)
).andExpect(status().isOk());
}
我得到这个错误跟踪。
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Name for argument of type [long] not specified, and parameter name information not found in class file either.
这是我的控制器
//Delete Score
//DELETE /score/{id}
@DeleteMapping("/scores/{id}")
public ResponseEntity<Score> deleteScore(@PathVariable(required = true) long id) {
Score result = null;
try{
Optional<ScoreEntity> s = scoreRepository.findById(id);
if (s.isPresent()){
result = new Score(s.get());
scoreRepository.delete(s.get());
return ResponseEntity.status(HttpStatus.OK).body(result);
}
}catch (Exception e){
System.out.println(e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
请帮忙。提前谢谢你。
如果您的代码没有使用 -parameter for Java 8 doc.
编译,您需要在注释中定义名称
You can explicitly name URI variables (for example, @PathVariable("customId")), but you can leave that detail out if the names are the same and your code is compiled with debugging information or with the -parameters compiler flag on Java 8.
@PathVariable(required = true, name="id") long id
我对 spring 和 JUnit 很陌生。现在我尝试构建一个测试用例。在我 运行 这个
@Test
public void shouldBeOK_Found() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.delete("/scores/{id}", 1L)
).andExpect(status().isOk());
}
我得到这个错误跟踪。
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Name for argument of type [long] not specified, and parameter name information not found in class file either.
这是我的控制器
//Delete Score
//DELETE /score/{id}
@DeleteMapping("/scores/{id}")
public ResponseEntity<Score> deleteScore(@PathVariable(required = true) long id) {
Score result = null;
try{
Optional<ScoreEntity> s = scoreRepository.findById(id);
if (s.isPresent()){
result = new Score(s.get());
scoreRepository.delete(s.get());
return ResponseEntity.status(HttpStatus.OK).body(result);
}
}catch (Exception e){
System.out.println(e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
请帮忙。提前谢谢你。
如果您的代码没有使用 -parameter for Java 8 doc.
编译,您需要在注释中定义名称You can explicitly name URI variables (for example, @PathVariable("customId")), but you can leave that detail out if the names are the same and your code is compiled with debugging information or with the -parameters compiler flag on Java 8.
@PathVariable(required = true, name="id") long id