Class ContentResultMatchers
java.lang.Object
org.springframework.test.web.servlet.result.ContentResultMatchers
Factory for response content assertions.
 
An instance of this class is typically accessed via
 MockMvcResultMatchers.content().
- Since:
- 3.2
- Author:
- Rossen Stoyanchev, Sam Brannen
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionbytes(byte[] expectedContent) Assert the response body content as a byte array.contentType(String contentType) Assert the ServletResponse content type.contentType(MediaType contentType) Assert the ServletResponse content type after parsing it as a MediaType.contentTypeCompatibleWith(String contentType) Assert the ServletResponse content type is compatible with the given content type as defined byMediaType.isCompatibleWith(MediaType).contentTypeCompatibleWith(MediaType contentType) Assert the ServletResponse content type is compatible with the given content type as defined byMediaType.isCompatibleWith(MediaType).Assert the character encoding in the ServletResponse.Assert the character encoding in the ServletResponse.Parse the expected and actual strings as JSON and assert the two are "similar" - i.e.Deprecated.json(String jsonContent, JsonComparator comparator) Parse the response content and the given string as JSON and assert the two using the givenJsonComparator.json(String jsonContent, JsonCompareMode compareMode) Parse the response content and the given string as JSON and assert the two using the given mode.Assert the response body content as a String.Assert the response body content with a HamcrestMatcher.Parse the response content and the given string as XML and assert the two are "similar" - i.e.
- 
Constructor Details- 
ContentResultMatchersprotected ContentResultMatchers()Protected constructor. UseMockMvcResultMatchers.content().
 
- 
- 
Method Details- 
contentTypeAssert the ServletResponse content type. The given content type must fully match including type, subtype, and parameters. For checking only the type and subtype seecontentTypeCompatibleWith(String).
- 
contentTypeAssert the ServletResponse content type after parsing it as a MediaType. The given content type must fully match including type, subtype, and parameters. For checking only the type and subtype seecontentTypeCompatibleWith(MediaType).
- 
contentTypeCompatibleWithAssert the ServletResponse content type is compatible with the given content type as defined byMediaType.isCompatibleWith(MediaType).
- 
contentTypeCompatibleWithAssert the ServletResponse content type is compatible with the given content type as defined byMediaType.isCompatibleWith(MediaType).
- 
encodingAssert the character encoding in the ServletResponse.- Since:
- 5.3.10
- See Also:
 
- 
encodingAssert the character encoding in the ServletResponse.- See Also:
 
- 
stringAssert the response body content with a HamcrestMatcher.mockMvc.perform(get("/path")) .andExpect(content().string(containsString("text")));
- 
stringAssert the response body content as a String.
- 
bytesAssert the response body content as a byte array.
- 
xmlParse 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. - Parameters:
- xmlContent- the expected XML content
- See Also:
 
- 
node
- 
source- See Also:
 
- 
jsonParse 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).Use of this matcher requires the JSONassert library. - Parameters:
- jsonContent- the expected JSON content
- Since:
- 4.1
- See Also:
 
- 
jsonDeprecated.in favor ofjson(String, JsonCompareMode)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 strictparameter 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. - Parameters:
- jsonContent- the expected JSON content
- strict- enables strict checking
- Since:
- 4.2
 
- 
jsonParse the response content and the given string as JSON and assert the two using the given mode. If the comparison failed, throws anAssertionErrorwith the message of theJsonComparison.Use of this matcher requires the JSONassert library. - Parameters:
- jsonContent- the expected JSON content
- compareMode- the compare mode
- Since:
- 6.2
 
- 
jsonParse the response content and the given string as JSON and assert the two using the givenJsonComparator. If the comparison failed, throws anAssertionErrorwith the message of theJsonComparison.Use this matcher if you require a custom JSONAssert configuration or if you desire to use another assertion library. - Parameters:
- jsonContent- the expected JSON content
- comparator- the comparator to use
- Since:
- 6.2
 
 
- 
json(String, JsonCompareMode)