I have the following Spock Unit Test example:
given:
ObjectMapper mapper = new ObjectMapper()
def eventNode = mapper.valueToTree([
person : [
name : year,
location. : location,
]
])
.
.
.
where:
name | location
'Tom' | null
It treats null
in the test case as a string "null"
and so it passes if-checks such as if(location != null)...
How can I make it not a string (Empty string ' ' is treated as "''")?
Most likely the location
is being parsed into a NullNode
. The toString()
value of a NullNode
is indeed the string "null"
but the textValue()
is a null
reference. Note that even a TextNode
produces different results for toString()
vs. textValue()
. You have to call the appropriate method for your test case.
def jacksonNull() {
given:
ObjectMapper mapper = new ObjectMapper()
ObjectNode eventNode = mapper.valueToTree([
person : [
name : name,
location : location,
]
])
JsonNode nameNode = eventNode.get('person').get('name')
JsonNode locationNode = eventNode.get('person').get('location')
expect:
nameNode.isTextual()
nameNode.textValue() == 'Tom'
nameNode.toString() == '"Tom"'
locationNode.isNull()
locationNode.textValue() == null
locationNode.toString() == 'null'
where:
name | location
'Tom' | null
}