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











up vote
0
down vote

favorite












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




























    up vote
    0
    down vote

    favorite












    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


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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 at 5:35

























      asked Nov 19 at 12:15









      Pranav

      493412




      493412
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          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',
            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








            up vote
            0
            down vote













            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

























              up vote
              0
              down vote













              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























                up vote
                0
                down vote










                up vote
                0
                down vote









                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 at 23:58









                Kevin Hussey

                495210




                495210






























                     

                    draft saved


                    draft discarded



















































                     


                    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

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    Refactoring coordinates for Minecraft Pi buildings written in Python