Reputation: 2607
This is my mock response, where I need to assert a particular field (type) as null. It always throws me exception as
java.lang.AssertionError: expected null, but was:<[null]>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotNull(Assert.java:755)
at org.junit.Assert.assertNull(Assert.java:737)
at org.junit.Assert.assertNull(Assert.java:747)
Mock response
{
"locations": [
{
"type": null,
"statusDt": "2018-08-15",
}
]
}
I'm doing assertion in the followig way
assertNull(locationResponse.getLocations().stream().map(location -> location.getType()).collect(Collectors.toList()));
Upvotes: 0
Views: 2211
Reputation: 24498
Consider an assert on each type, such as (working example here):
locationResponse.getLocations()
.stream()
.map(location -> location.getType())
.forEach(t -> assertNull(t));
Upvotes: 0
Reputation: 396
The message of the assertion says that null
is expected but [null]
is observed: [null]
is an array that contains a single null
element.
I think that collect(Collectors.toList()))
cannot return a null
object.
You should write an assertion like this one to check that all the types are null
:
assertTrue(locationResponse.getLocations().stream().map(location -> location.getType()).allMatch(type -> type == null));
Upvotes: 4
Reputation: 8758
Seems like as a result of collect()
operation you get a List that contains only one element null
.
Please try to get first element from that list
assertNull(locationResponse.getLocations().stream().map(location -> location.getType()).collect(Collectors.toList()).get(0));
Upvotes: 2