所以讓我們說我們有這樣的事情:
public class SomeService {
...
public Flux<String> getStringsFromWebServer() {
return webClient.get()
.uri(this::generateSomeUrl)
.retrieve()
.bodyToMono(SomePojo.class)
.map(SomePojo::getStringList)
.flatMapMany(Flux::fromIterable);
}
撰寫如下所示的測驗是否有意義:
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();
}
這是您以編程方式斷言從 Flux 回傳的順序的方式嗎?
我對 assertNext 通量方法做了什么壞事嗎?我的意思是在這個意義上,我總是提供有序的資料,所以我假設 fromIterable 將按照 Spring Boot 應用程式接收到的順序從該串列中使用。
感覺就像我在這里違反了某種原則......我的意思是它有效......
uj5u.com熱心網友回復:
嗯整理了一下。
于是就有了expectNext方法:
https://www.baeldung.com/flux-sequences-reactor
您可以在哪里預先生成您的串列,然后像這樣斷言:
List<String> expectedStrings = Arrays.asList(
"hello1", "hello2", "hello3"
);
...
StepVerifier.create(result)
.expectNextSequence(expectedStrings)
.verifyComplete();
編輯:顯然我必須等待幾天才能將我自己的問題標記為已回答?
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/385179.html
