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?
java spring spring-webflux reactor mockwebserver
add a comment |
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?
java spring spring-webflux reactor mockwebserver
add a comment |
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?
java spring spring-webflux reactor 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<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
java spring spring-webflux reactor mockwebserver
edited Nov 20 at 5:35
asked Nov 19 at 12:15
Pranav
493412
493412
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 20 at 23:58
Kevin Hussey
495210
495210
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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