spring-framework / org.springframework.test.web.servlet.result / ContentResultMatchers

ContentResultMatchers

open class ContentResultMatchers

Factory for response content assertions.

An instance of this class is typically accessed via MockMvcResultMatchers#content.

Author
Rossen Stoyanchev

Since
3.2

Functions

bytes

open fun bytes(expectedContent: ByteArray): ResultMatcher

Assert the response body content as a byte array.

contentType

open fun contentType(contentType: String): ResultMatcher

Assert the ServletResponse content type. The given content type must fully match including type, sub-type, and parameters. For checking only the type and sub-type see #contentTypeCompatibleWith(String).

open fun contentType(contentType: MediaType): ResultMatcher

Assert the ServletResponse content type after parsing it as a MediaType. The given content type must fully match including type, sub-type, and parameters. For checking only the type and sub-type see #contentTypeCompatibleWith(MediaType).

contentTypeCompatibleWith

open fun contentTypeCompatibleWith(contentType: String): ResultMatcher
open fun contentTypeCompatibleWith(contentType: MediaType): ResultMatcher

Assert the ServletResponse content type is compatible with the given content type as defined by MediaType#isCompatibleWith(MediaType).

encoding

open fun encoding(characterEncoding: String): ResultMatcher

Assert the character encoding in the ServletResponse.

json

open fun json(jsonContent: String): ResultMatcher

Parse the expected and actual strings as JSON and assert the two are "similar" - i.e. they contain the same attribute-value pairs regardless of formatting with a lenient checking (extensible, and non-strict array ordering).

open fun json(jsonContent: String, strict: Boolean): ResultMatcher

Parse the response content and the given string as JSON and assert the two are "similar" - i.e. they contain the same attribute-value pairs regardless of formatting.

Can compare in two modes, depending on strict parameter value:

  • true: strict checking. Not extensible, and strict array ordering.
  • false: lenient checking. Extensible, and non-strict array ordering.

Use of this matcher requires the JSONassert library.

node

open fun node(matcher: Matcher<in Node>): ResultMatcher

Parse the response content as Node and apply the given Hamcrest Matcher.

source

open fun source(matcher: Matcher<in Source>): ResultMatcher

Parse the response content as DOMSource and apply the given Hamcrest Matcher.

string

open fun string(matcher: Matcher<in String>): ResultMatcher

Assert the response body content with a Hamcrest Matcher.

 mockMvc.perform(get("/path")) .andExpect(content().string(containsString("text"))); 

open fun string(expectedContent: String): ResultMatcher

Assert the response body content as a String.

xml

open fun xml(xmlContent: String): ResultMatcher

Parse the response content and the given string as XML and assert the two are "similar" - i.e. they contain the same elements and attributes regardless of order.

Use of this matcher requires the XMLUnit library.