java 未找到伪单元测试响应

6tr1vspr  于 2022-12-17  发布在  Java
关注(0)|答案(6)|浏览(171)

我正在尝试实现一个涉及FeignClient调用的单元测试,该调用应返回404 Not Found。
由于Feign正在为404抛出FeignException,实现此测试用例的正确方法是什么?
我在尝试这样的东西...

when(mockedApiClient.userDataDelete(anyString()))
                .thenThrow( ... );

我该扔什么?

erhoui1w

erhoui1w1#

Feign已经提供了NotFound等内部类和其他典型的HTTP响应代码类型。下面是一个示例。

Request request = Request.create(Request.HttpMethod.GET, "url",
                    new HashMap<>(), null, new RequestTemplate());
            throw new FeignException.NotFound("", request, null);

只要根据你的需要修改以上内容!需要注意的关键点是Request对象是强制性的。截至2021年,像Request.create这样的重载已经过时了。注意你使用的东西!
希望有帮助!快乐编码!

k10s72fa

k10s72fa2#

好吧,对我来说工作如下(我使用EasyRandom作为一些字段的生成器):

private EasyRandom easyRandom = new EasyRandom();
 Map<String, Collection<String>> headersError = easyRandom.nextObject(HashMap.class);
 byte[] bodyError = easyRandom.nextObject(byte[].class);

 when(mockedApiClient.userDataDelete(anyString())
                .thenThrow(FeignException.errorStatus(
                        "userDataDelete",
                        Response.builder()
                                .status(404)
                                .reason("message error")
                                .request(Request.create(
                                        Request.HttpMethod.POST,
                                        "foo/foo/bar/v1/delete-data-user",
                                        headersError, //this field is required for construtor//
                                        null,
                                        null,
                                        null))
                                .body(bodyError)//this field is required for construtor
                                .build())
                );
ecr0jaav

ecr0jaav3#

要在模拟服务时返回404伪异常,您可以尝试以下操作:
1.创建您自己的异常类,它将扩展伪异常。
1.使用HttpStatus.NOT_FOUND值注解响应状态。
1.如下所示重写构造函数:

import feign.FeignException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class SampleFeignClientException extends FeignException {

    public SampleFeignClientException (String message, Throwable cause) {
        super(message, cause);
    }

    public SampleFeignClientException (String message, Throwable cause, byte[] content) {
        super(message, cause, content);
    }

    public SampleFeignClientException (String message) {
        super(message);
    }

    public SampleFeignClientException (int status, String message, byte[] content) {
        super(status, message, content);
    }
}

1.在Junit测试用例中,抛出创建的sampleFeignException类。

@Test
    public void sampleTestMethod() {
        SampleFeignClientException sampleFeignClientException = new sampleFeignClientException(404, "NOT FOUND", new byte[1]);

         doThrow(sampleFeignClientException).when(mockService).method();

 }

我希望这能帮助你解决这个问题。

pcww981p

pcww981p4#

除非提供了其他更好的解决方案,否则这就是我克服这一点的方法...

when(mockedApiClient.userDataDelete(anyString()))
                .thenThrow(FeignException.errorStatus(
                        "userDataDelete",
                        Response.builder()
                                .status(404)
                                .headers(new HashMap<>())
                                .reason("Not found").build()));
8xiog9wr

8xiog9wr5#

Request request = mock(Request.class);

 when(someClient.someGetRequest(List.of(doc.getId())))
            .thenThrow(new FeignException.NotFound("message", request, null, null));

您可以避免创建请求,而只是模拟它。

flvlnr44

flvlnr446#

嘲笑您的FeignException

var ex = Mockito.mock(FeignException.class);
Mockito.when(ex.status()).thenReturn(404);
Mockito.when(mockedApiClient.userDataDelete(anyString()))
       .thenThrow(ex);

相关问题