-
I have this thin http client wrapper over a REST service: @Singleton
@Slf4j
public class TransversalApiLowLevelClient {
private final HttpClient httpClient;
public TransversalApiLowLevelClient(@Client(id = "transversal-api") HttpClient httpClient, TransversalApiConfiguration configuration) {
this.httpClient = httpClient;
// other initializations
}
public Mono<List<Auto>> getAutoContractsAsync(String enterpriseId) {
//fetch the data from somewhere..
String endpointUrl // = ... ;
MutableHttpRequest<?> req = HttpRequest.GET(endpointUrl).accept(MediaType.APPLICATION_JSON_TYPE);
Publisher<List<Auto>> publisher = httpClient.retrieve(req, Argument.listOf(Auto.class));
return Mono.from(publisher)
.defaultIfEmpty(Collections.emptyList());
} Then in the test I want to test the case where the service returns "no data" : @Test
@SneakyThrows
void getAutoContractsAsyncWhenEmpty() {
wmExtension.stubFor(get(urlMatching("/some-path-here/auto[?\\w%&=_,]*$"))
.willReturn(okForJson("[]")));
Mono<List<Auto>> autoContractsAsync = transversalApiLowLevelClient.getAutoContractsAsync("0200068636");
List<Auto> autoList = autoContractsAsync.defaultIfEmpty(Collections.emptyList()).block();
assertThat(autoList.size(), CoreMatchers.is(0));
}
@RegisterExtension
static WireMockExtension wmExtension = WireMockExtension.newInstance()
.options(wireMockConfig().port(4444).dynamicHttpsPort())
.build(); Problem is, running the above test throws an error:
The project uses Micronaut v4.4.3 with Jackson Databind library (v4.4.10) For the sake of completeness, the following test completes normally (when the service returns some records): @Test
@SneakyThrows
void getAutoContractsAsync() {
String jsonBody = IO.loadFileResource("auto_contracts_0200068636.json"); // loads a canned response from a local file
wmExtension.stubFor(get(urlMatching("/some-path-here/auto[?\\w%&=_,]*$"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody(jsonBody)));
Mono<List<Auto>> autoContractsAsync = transversalApiLowLevelClient.getAutoContractsAsync("0200068636");
List<Auto> autoList = autoContractsAsync.block();
assertThat(autoList.size(), CoreMatchers.is(7));
} So how do I fix the "no-data" case? AFAIK, in general a service can either
when there is no data available. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Could it be related with this issue? |
Beta Was this translation helpful? Give feedback.
-
Are you sure that |
Beta Was this translation helpful? Give feedback.
Are you sure that
okForJson("[]")
returns[]
and not"[]"