简体   繁体   English

使用MockWebServer模拟响应时,Mono.flatMap无法正常工作

[英]The Mono.flatMap is not working while mocking the response using MockWebServer

Making an HTTP call using WebClient, from the Mono output I want to do some logic, for which I'm using flatMap method. 我想从Mono输出中使用WebClient进行HTTP调用,我想做一些逻辑,为此我使用了flatMap方法。

        Mono<Response> authResponseMono = webClient.post().syncBody(requestBody)
            .retrieve()
            .bodyToMono(Response.class);

        return authResponseMono.flatMap(authResponse -> {
        //code removed for brevity
        });

Using OkHttp MockWebServer library to mock the response and unit test the functionalities. 使用OkHttp MockWebServer库模拟响应并对功能进行单元测试。 The flatMap method is not getting invoked while using MockWebServer, however, on the actual calls things are working fine. 使用MockWebServer时,不会调用flatMap方法,但是,在实际的调用中,一切正常。

Following is the snippet used to mock the HTTP response 以下是用于模拟HTTP响应的代码段

server.enqueue(mockResponse); //server is instance of MockWebServer
Optional<Response> optionalResponse = authClientService.verifyToken().block(); //verifyToken returns authResponseMono

I think the cause for this behavior is due to the way mocks are created internally by the MockWebServer. 我认为造成这种现象的原因是由于MockWebServer在内部创建了模拟程序。 What am I missing here? 我在这里想念什么?

I use MockWebServer and webClient and it seems to be fine. 我使用MockWebServer和webClient,这似乎很好。 Maybe you can post more detail. 也许您可以发布更多详细信息。

here is a snippet of my setup - note this is not a spring test 这是我的设置的摘要-请注意,这不是弹簧测试

@RunWith(MockitoJUnitRunner.class)
public class AccountDetailsRetrievalTest {

    private static final String ACCOUNT_DETAILS_PATH = "/AccountDetailsPath";
    private MockWebServer server;

    private WebClient webClient;

    private AccountDetailsRetrieval accountDetailsRetrieval;

    @Before
    public void setup() {
        this.server = new MockWebServer();
        this.webClient = WebClient.create(this.server.url(ACCOUNT_DETAILS_PATH).toString());
        accountDetailsRetrieval = new AccountDetailsRetrieval(this.webClient, ACCOUNT_DETAILS_PATH, 300);
    }

    @After
    public void shutdown() throws Exception {
        this.server.shutdown();
    }

@Test
public void nameHasBeenChanged() {
    String content = "{\"bar\":\"bar1\",\"foo\":\"foo1\"}";
    MockResponse response1 = new MockResponse()
            .setHeader("Content-Type", "application/json")
            .setBody(content);
    this.server.enqueue(response1);

    StepVerifier.create(accountDetailsRetrieval.getAccountData(new AccountDetails("001", "ABC")))
            .assertNext(response -> {
                assertThat(response).isNotNull();
                assertThat(response.getHttpStatus()).isEqualByComparingTo(HttpStatus.OK);
                assertThat(response.data().get("bar")).isEqualTo("bar1");
                assertThat(response.data().get("foo")).isEqualTo("foo1");
            })
            .verifyComplete();
}

Response here is just a wrapper for JsonNode 这里的响应只是JsonNode的包装

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM