https://kotlinlang.org logo
#spring
Title
d

Dariusz Kuc

01/13/2020, 5:40 PM
hello, any idea how to verify JSON response from WebTestClient to check if fields are null? e.g.
Copy code
testClient.get()
    .uri("whatever")
    .accept(APPLICATION_JSON)
    .exchange()
    .expectStatus().isOk
    .expectBody()
    .jsonPath("$.whatever").isEqualTo(null) // NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS
If I enable
jsr305=strict
compiler settings the above fails complaining it expects non-null
Any
using • SpringBoot 2.2.1.RELEASE • Kotlin 1.3.50
using Hamcrest
IsNull.nullValue
I'm getting
expected:<null> but was:<null>
weird part is that
isEqualTo
ends up calling
Copy code
public void assertValue(String content, @Nullable Object expectedValue)
which allows nullable value
any ideas?
if anyone is also looking for workaround this worked for me
Copy code
jsonPath("$.whatever").value<String?> {
  assertNull(it)
}
b

Ben

01/13/2020, 9:48 PM
Are you returning
null
or are you just asserting that the field isn't there?
d

Dariusz Kuc

01/13/2020, 9:50 PM
response has explicit null
i.e.
Copy code
{
  "whatever": null
}
b

Ben

01/13/2020, 10:45 PM
You could probably use :
Copy code
.json("""{"whatever":null}""")
8 Views