(에러코드)
IllegalArgumentException: Name for argument of type [java.lang.Long] not specified, and parameter name information not found in class file either.
(구글번역)
IllegalArgumentException: [java.lang.Long] 유형의 인수 이름이 지정되지 않았고 매개변수 이름 정보도 클래스 파일에서 찾을 수 없습니다.
간략하게 설명하면 Long타입의 파라미터 이름에 대한 정보를 찾지 못하는 문제이다.
postman으로 테스트했을 때는 문제가 없었으나 테스트 코드를 작성하니 위의 에러가 발생했다.
(1. Controller)
@PutMapping("/{cardId}")
public ResponseEntity<UpdateCardResponseDto> updateCard(
@PathVariable Long cardId,
@RequestBody CardRequestDto requestDto,
@AuthenticationPrincipal UserDetailsImpl userDetails) {
UpdateCardResponseDto updateCardResponseDto =
cardService.updateCard(cardId, requestDto,userDetails.getUser());
return ResponseEntity.ok().body(updateCardResponseDto);
}
(2. Test)
@Test
@DisplayName("할일카드 수정 요청")
void updateCard() throws Exception {
User kim = User.builder().username("kim12345").password("12345678").build();
Long cardId = 1L;
CardRequestDto requestDto = new CardRequestDto("나는 제목수정","나는 내용수정");
Card card = Card.builder()
.title(requestDto.getTitle())
.content(requestDto.getContent())
.complete(DEFAULT_COMPLETE)
.user(kim)
.build();
UpdateCardResponseDto responseDto = new UpdateCardResponseDto(card);
String json = objectMapper.writeValueAsString(responseDto);
given(cardService.updateCard(anyLong(),any(CardRequestDto.class),any(User.class)))
.willReturn(responseDto);
mvc.perform(put("/api/cards/{cardId}",cardId)
.content(json)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.principal(mockPrincipal))
.andExpect(status().isOk())
.andDo(print());
}
(1. Controller)
@PutMapping("/{cardId}")
public ResponseEntity<UpdateCardResponseDto> updateCard(
@PathVariable Long cardId,
@RequestBody CardRequestDto requestDto,
@AuthenticationPrincipal UserDetailsImpl userDetails) {
...
}
(2. Test)
mvc.perform(put("/api/cards/{cardId}",cardId)
...;
이렇게 문제되는 부분을 추려볼수 있는데
여기서 cardId가 특정되지 않아 발생하는 에러인듯 해서
(1. Controller)
@PutMapping("/{cardId}")
public ResponseEntity<UpdateCardResponseDto> updateCard(
@PathVariable(name = "cardId") Long cardId,
@RequestBody CardRequestDto requestDto,
@AuthenticationPrincipal UserDetailsImpl userDetails) {
...
}
이렇게 @PathVariable Long cardId 에
(name = "cardId") 이 코드를 특정지어 주니 에러 없이 코드가 잘 실행 되었다