So let us say we have something like:
public class SomeService {
...
public Flux<String> getStringsFromWebServer() {
return webClient.get()
.uri(this::generateSomeUrl)
.retrieve()
.bodyToMono(SomePojo.class)
.map(SomePojo::getStringList)
.flatMapMany(Flux::fromIterable);
}
Does it make sense to write tests that look like this:
void getStringsFromWebServer_shouldParseInOrderOfReceivingStrings() {
// given
// I have mocked up a WebClient, that is wired up to a Mocked Web Server
// I am preloading the Mocked Web Server with this JSON
String jsonStrings = "{'stringList': ['hello1', 'hello2', 'hello3']}"
mockWebServer.enqueue(new MockResponse().setResponseCode(200))
.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.setBody(jsonStrings);
// when
Flux<String> result = someService.getStringsFromWebServer();
// then
StepVerifier.FirstStep<String> fluxStep = StepVerifier.create(result);
for (int i = 1; i < 4; i ) {
String expectedInput = String.format("hello%d", i);
fluxStep.assertNext(someString -> assertEquals(expectedInput, someString));
}
fluxStep.verifyComplete();
}
Is this how you are to programmatically assert the order that comes back out from Flux?
Am I doing something bad with the assertNext flux method? I mean in this sense, I am always providing ordered data so I am assuming that fromIterable will consuming from that list in the order that it is received by the spring boot application.
It feels like I am violating some sort of principle here...I mean it works...
CodePudding user response:
Meh sorted it out.
So there is the expectNext
method:
https://www.baeldung.com/flux-sequences-reactor
Where you can pregenerate your list and then assert something like this:
List<String> expectedStrings = Arrays.asList(
"hello1", "hello2", "hello3"
);
...
StepVerifier.create(result)
.expectNextSequence(expectedStrings)
.verifyComplete();
EDIT: apparently I have to wait a few days before I can mark my own question as answered?