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












0















Making an HTTP call using WebClient, from the Mono output I want to do some logic, for which I'm using flatMap method.



        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. The flatMap method is not getting invoked while using MockWebServer, however, on the actual calls things are working fine.



Following is the snippet used to mock the HTTP response



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. What am I missing here?










share|improve this question





























    0















    Making an HTTP call using WebClient, from the Mono output I want to do some logic, for which I'm using flatMap method.



            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. The flatMap method is not getting invoked while using MockWebServer, however, on the actual calls things are working fine.



    Following is the snippet used to mock the HTTP response



    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. What am I missing here?










    share|improve this question



























      0












      0








      0








      Making an HTTP call using WebClient, from the Mono output I want to do some logic, for which I'm using flatMap method.



              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. The flatMap method is not getting invoked while using MockWebServer, however, on the actual calls things are working fine.



      Following is the snippet used to mock the HTTP response



      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. What am I missing here?










      share|improve this question
















      Making an HTTP call using WebClient, from the Mono output I want to do some logic, for which I'm using flatMap method.



              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. The flatMap method is not getting invoked while using MockWebServer, however, on the actual calls things are working fine.



      Following is the snippet used to mock the HTTP response



      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. What am I missing here?







      java spring spring-webflux reactor mockwebserver






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 5:35







      Pranav

















      asked Nov 19 '18 at 12:15









      PranavPranav

      523413




      523413
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I use MockWebServer and webClient and it seems to be fine. 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






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374433%2fthe-mono-flatmap-is-not-working-while-mocking-the-response-using-mockwebserver%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I use MockWebServer and webClient and it seems to be fine. 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






            share|improve this answer




























              0














              I use MockWebServer and webClient and it seems to be fine. 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






              share|improve this answer


























                0












                0








                0







                I use MockWebServer and webClient and it seems to be fine. 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






                share|improve this answer













                I use MockWebServer and webClient and it seems to be fine. 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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 '18 at 23:58









                Kevin HusseyKevin Hussey

                515211




                515211






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374433%2fthe-mono-flatmap-is-not-working-while-mocking-the-response-using-mockwebserver%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)