I have a Message
object with MessageHeaders
field. The MessageHeaders
class implements a Map<String, Object>
. I want to assert that I have specific headers set. I'm having trouble getting the MapAssert
methods to come up.
Here's what I want to accomplish:
assertThat(actual)
.extracting(Message::getHeaders) // This returns AbstractObjectAssert though
.containsKeys("some key"); // Not available
Here's the Message
and MessageHeaders
class to be clear:
public class Message {
private MessageHeaders headers;
// getter
}
public class MessageHeaders implements Map<String, Object>, Serializable {
// methods
}
In order to use MapAssert
you need to extract directly the MessageHeaders
field and cast the extraction with asInstanceOf
:
assertThat(actual)
.extracting("headers")
.asInstanceOf(InstanceOfAssertFactories.MAP)
.containsKey("some key");
AssertJ Core 3.14.0 provides a new extracting()
to support direct casting, so you can write:
assertThat(actual)
.extracting(Message::getHeaders, as(InstanceOfAssertFactories.MAP))
.containsKey("some key");
Note that as()
is an optional syntax sugar to improve readability.
The solution / workaround I came up with was to assert the map itself instead of using extracting.
assertThat(actual.getHeaders())
.containsKey("some key");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.