当有请求正文时模拟 WebClient 帖子 [英] Mocking a WebClient post when there's a request body

查看:35
本文介绍了当有请求正文时模拟 WebClient 帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几个关于模拟 WebClient 对象的问题提供了有用的答案.但是我在做一个带有身体的帖子时仍然有问题.我只是使用 Mockito 而不是 mockwebserver.

There are several questions with helpful answers about mocking a WebClient object. But I still have problems when doing a post with a body. I'm just using Mockito not mockwebserver.

这是我正在测试的方法:

This is the method I'm testing:

public class RestClient extends BaseRestClient {
 ...
 public <T,G> Mono<T> post(String url, G req, Class<T> resp) throws IOException {
        Mono<T> response = null;

        response = this.getWebClient().post()
                    .uri(url)
                    .header(HttpHeaders.CONTENT_TYPE,JSON_CONTENT_TYPE)
                    .accept(MediaType.APPLICATION_JSON)
                    //.body(BodyInserters.fromObject(req))
                    .header(HttpHeaders.AUTHORIZATION, BEARER + token)
                    .retrieve()
                    .bodyToMono(resp).log();

        return response.map(resp::cast);
    }
 ...

注意注释掉的正文行.

这是与上面的代码一起工作的测试 - 再次注意测试中注释掉的行:

And this is the test which works fine with the code above- again notice the commented out line in the test:

@Mock
WebClient webClient;

@Mock
WebClient.RequestBodyUriSpec requestBodyUriSpec;

@Mock
WebClient.RequestHeadersSpec requestHeadersSpec;

@Mock
WebClient.RequestBodySpec requestBodySpec;

@Mock
WebClient.ResponseSpec responseSpec;

@InjectMocks
RestClient restClient;

@Test
    public void postTest() throws IOException {
        when(webClient.post()).thenReturn(requestBodyUriSpec);
        when(requestBodyUriSpec.uri(anyString())).thenReturn(requestBodySpec);
        when(requestBodySpec.header(any(),any())).thenReturn(requestBodySpec);
        when(requestBodySpec.accept(any())).thenReturn(requestBodySpec);
        when(responseSpec.bodyToMono(ArgumentMatchers.<Class<String>>notNull()))
                .thenReturn(Mono.just("resp"));

        //when(requestBodySpec.body(any())).thenReturn(requestHeadersSpec);
        when(requestBodySpec.retrieve()).thenReturn(responseSpec);

        restClient.post("http://sampleurl",Object.class, Object.class);
    }

再次,一切正常.但是,如果我将注释掉的行放回代码中,这意味着这篇文章有正文,并通过将注释掉的行放回测试来模拟正文,那么我将得到 NullPointerException.retrieve() 在代码中.就像我缺少一个可以模拟的对象一样.

Again, everything works fine. But if I put the commented out line back in the code, meaning there's a body to this post, and mock the body by putting the commented out line back in the test, then I'll get NullPointerException on .retrieve() in the code. Just like I'm missing an object to mock.

我什至嘲笑了 requestHeadersSpecrequestBodyUriSpec 的 .retrieve() :

I even mocked .retrieve() for requestHeadersSpec and requestBodyUriSpec:

when(requestHeadersSpec.retrieve()).thenReturn(responseSpec);
when(requestBodyUriSpec.retrieve()).thenReturn(responseSpec);

仍然没有成功.知道有什么问题吗?

And still no success. Any ideas what's wrong there?

推荐答案

我错过了对 requestHeadersSpec 的header"方法的嘲讽:

I missed mocking the "header" method of requestHeadersSpec:

when(requestHeadersSpec.header(any(),any())).thenReturn(requestHeadersSpec);

所以,现在可以正常工作了:

So, this works fine now:

@Test
    public void postTest() throws IOException {
        when(webClient.post()).thenReturn(requestBodyUriSpec);
        when(requestBodyUriSpec.uri(anyString())).thenReturn(requestBodySpec);
        when(requestBodySpec.header(any(),any())).thenReturn(requestBodySpec);

        when(requestHeadersSpec.header(any(),any())).thenReturn(requestHeadersSpec);

        when(requestBodySpec.accept(any())).thenReturn(requestBodySpec);
        when(requestBodySpec.body(any())).thenReturn(requestHeadersSpec);
        when(requestHeadersSpec.retrieve()).thenReturn(responseSpec);
        when(responseSpec.bodyToMono(ArgumentMatchers.<Class<String>>notNull()))
                .thenReturn(Mono.just("resp"));

        Assert.assertNotNull(restClient.post("http://sampleurl",Object.class, Object.class));
    }

这篇关于当有请求正文时模拟 WebClient 帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆