Skip to content

[테스트] FeignException.class 기반 willThrow 사용 시 InstantiationError 발생 문제 수정 필요 #16

Description

@coderabbitai

개요

OrderCommandServiceImplTest의 배송 실패 테스트 케이스에서 willThrow(FeignException.class) 방식을 사용하고 있으나, OpenFeign 13.x의 FeignException은 무인자 생성자가 없어 Mockito가 리플렉션으로 인스턴스 생성 시 InstantiationError가 발생합니다.

관련 파일

  • src/test/java/com/fhsh/daitda/application/OrderCommandServiceImplTest.java (101~102번째 줄)

문제 상세

Mockito의 willThrow(Class) / thenThrow(Class) 형식은 내부적으로 리플렉션을 이용해 무인자 생성자를 호출하여 예외 인스턴스를 생성합니다.
그러나 FeignException은 최소한 int statusString message를 요구하는 생성자만 제공하므로, stubbing 시점에 InstantiationError가 발생하여 테스트가 실패합니다.

수정 방안

FeignException.errorStatus(...) 정적 팩토리 메서드를 이용해 실제 인스턴스를 생성하고 willThrow(instance)로 교체해야 합니다.

import java.nio.charset.StandardCharsets;
import feign.Request;
import feign.Response;

Request req = Request.create(
    Request.HttpMethod.POST,
    "/deliveries",
    Map.of(),
    null,
    StandardCharsets.UTF_8,
    null
);
Response res = Response.builder()
    .status(500)
    .reason("Internal Server Error")
    .request(req)
    .headers(Map.of())
    .build();
given(deliveryClient.createDelivery(any(), any(), any()))
    .willThrow(FeignException.errorStatus("DeliveryClient#createDelivery", res));

참고

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions