In a Spring Boot integration test, I'm trying to test if a JSON property is null
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class IntegrationTests {
@Autowired
private WebTestClient webClient;
@Test
void saveExtension() {
webClient.get()
.uri("/api/endpoint")
.exchange()
.expectStatus()
.is2xxSuccessful();
.expectBody()
.jsonPath("$.data[0].extended").isEqualTo(null)
}
}
The test passes, but I get a SpotBugs error (and an IntelliJ warning) because the parameter of isEqualTo
is annotated @NotNull
.
Surely there's a more elegant way of testing whether a JSON property is null? I've tried both of the following alternatives provided by org.springframework.test.web.reactive.server.JsonPathAssertions
:
.jsonPath("$.data[0].extended").doesNotExist()
.jsonPath("$.data[0].extended").isEmpty()
But the test fails in both cases. Is there a better way of testing if a JSON property is null?
I also tried the following:
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
// ...
.jsonPath("$.data[?(@.bidderId == '%s')].extended", BIDDER_ID_2).value(is(nullValue()));
But this fails with the error:
Expected: is null but: was <[null]>
This error message suggests that extended: [null]
was returned but I've checked the response and it's extended: null