类 ContentResultMatchers
- java.lang.Object
- org.springframework.test.web.servlet.result.ContentResultMatchers
public class ContentResultMatchers extends Object
Factory for response content assertions.An instance of this class is typically accessed via
MockMvcResultMatchers.content()
.- 从以下版本开始:
- 3.2
- 作者:
- Rossen Stoyanchev
构造器概要
构造器 限定符 构造器 说明 protected
ContentResultMatchers()
Protected constructor.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 ResultMatcher
bytes(byte[] expectedContent)
Assert the response body content as a byte array.ResultMatcher
contentType(String contentType)
Assert the ServletResponse content type.ResultMatcher
contentType(MediaType contentType)
Assert the ServletResponse content type after parsing it as a MediaType.ResultMatcher
contentTypeCompatibleWith(String contentType)
Assert the ServletResponse content type is compatible with the given content type as defined byMediaType.isCompatibleWith(MediaType)
.ResultMatcher
contentTypeCompatibleWith(MediaType contentType)
Assert the ServletResponse content type is compatible with the given content type as defined byMediaType.isCompatibleWith(MediaType)
.ResultMatcher
encoding(String characterEncoding)
Assert the character encoding in the ServletResponse.ResultMatcher
json(String jsonContent)
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).ResultMatcher
json(String jsonContent, boolean strict)
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.ResultMatcher
node(Matcher<? super Node> matcher)
ResultMatcher
source(Matcher<? super Source> matcher)
ResultMatcher
string(String expectedContent)
Assert the response body content as a String.ResultMatcher
string(Matcher<? super String> matcher)
Assert the response body content with a HamcrestMatcher
.ResultMatcher
xml(String xmlContent)
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.
构造器详细资料
ContentResultMatchers
protected ContentResultMatchers()
Protected constructor. UseMockMvcResultMatchers.content()
.
方法详细资料
contentType
public ResultMatcher contentType(String contentType)
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 seecontentTypeCompatibleWith(String)
.
contentType
public ResultMatcher contentType(MediaType contentType)
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 seecontentTypeCompatibleWith(MediaType)
.
contentTypeCompatibleWith
public ResultMatcher contentTypeCompatibleWith(String contentType)
Assert the ServletResponse content type is compatible with the given content type as defined byMediaType.isCompatibleWith(MediaType)
.
contentTypeCompatibleWith
public ResultMatcher contentTypeCompatibleWith(MediaType contentType)
Assert the ServletResponse content type is compatible with the given content type as defined byMediaType.isCompatibleWith(MediaType)
.
encoding
public ResultMatcher encoding(String characterEncoding)
Assert the character encoding in the ServletResponse.
string
public ResultMatcher string(Matcher<? super String> matcher)
Assert the response body content with a HamcrestMatcher
.mockMvc.perform(get("/path")) .andExpect(content().string(containsString("text")));
string
public ResultMatcher string(String expectedContent)
Assert the response body content as a String.
bytes
public ResultMatcher bytes(byte[] expectedContent)
Assert the response body content as a byte array.
xml
public ResultMatcher xml(String xmlContent)
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.
- 参数:
xmlContent
- the expected XML content- 另请参阅:
MockMvcResultMatchers.xpath(String, Object...)
,MockMvcResultMatchers.xpath(String, Map, Object...)
node
public ResultMatcher node(Matcher<? super Node> matcher)
source
public ResultMatcher source(Matcher<? super Source> matcher)
- 另请参阅:
- xml-matchers
json
public ResultMatcher json(String jsonContent)
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).- 参数:
jsonContent
- the expected JSON content- 从以下版本开始:
- 4.1
json
public ResultMatcher json(String jsonContent, boolean strict)
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.
- 参数:
jsonContent
- the expected JSON contentstrict
- enables strict checking- 从以下版本开始:
- 4.2