Defining Expectations
断言的运作方式与任何 AssertJ 断言相同。该支持为 MvcTestResult
的各个部分提供了专用的断言对象,如下例所示:
Assertions work the same way as any AssertJ assertions. The support provides dedicated
assert objects for the various pieces of the MvcTestResult
, as shown in the following
example:
include-code::./HotelControllerTests[tag=get,indent=0]
如果请求失败,则该交换不会抛出异常。相反,你可以断言交换的结果失败:
If a request fails, the exchange does not throw the exception. Rather, you can assert that the result of the exchange has failed: include-code::./HotelControllerTests[tag=failure,indent=0]
该请求也有可能意外失败,即处理程序抛出的异常未得到处理并按原样抛出。你仍然可以使用 .hasFailed()
和 .failure()
,但任何尝试访问结果的一部分都会抛出一个例外,因为交换尚未完成。
The request could also fail unexpectedly, that is the exception thrown by the handler
has not been handled and is thrown as is. You can still use .hasFailed()
and
.failure()
but any attempt to access part of the result will throw an exception as
the exchange hasn’t completed.
JSON Support
MvcTestResult
的 AssertJ 支持通过 bodyJson()
提供 JSON 支持。
The AssertJ support for MvcTestResult
provides JSON support via bodyJson()
.
如果提供 JSONPath, 您可以针对 JSON 文档应用表达式。返回值提供了方便的方法来针对各种受支持的 JSON 数据类型返回一个专门的 Assert 对象:
If JSONPath is available, you can apply an expression on the JSON document. The returned value provides convenient methods to return a dedicated assert object for the various supported JSON data types:
-
Java
-
Kotlin
assertThat(mockMvc.get().uri("/family")).bodyJson()
.extractingPath("$.members[0]")
.asMap()
.contains(entry("name", "Homer"));
assertThat(mockMvc.get().uri("/family")).bodyJson()
.extractingPath("$.members[0]")
.asMap()
.contains(entry("name", "Homer"))
只要正确地配置了消息转换器,你还可以将原始内容转换为任何数据类型:
You can also convert the raw content to any of your data types as long as the message converter is configured properly:
-
Java
-
Kotlin
assertThat(mockMvc.get().uri("/family")).bodyJson()
.extractingPath("$.members[0]")
.convertTo(Member.class)
.satisfies(member -> assertThat(member.name).isEqualTo("Homer"));
assertThat(mockMvc.get().uri("/family")).bodyJson()
.extractingPath("$.members[0]")
.convertTo(Member::class.java)
.satisfies(ThrowingConsumer { member: Member ->
assertThat(member.name).isEqualTo("Homer")
})
转换为目标 Class
提供了一个通用断言对象。对于更复杂类型,你可能想要使用 AssertFactory
,如果可能,它将返回一个专用的断言类型:
Converting to a target Class
provides a generic assert object. For more complex types,
you may want to use AssertFactory
instead that returns a dedicated assert type, if
possible:
-
Java
-
Kotlin
assertThat(mockMvc.get().uri("/family")).bodyJson()
.extractingPath("$.members")
.convertTo(InstanceOfAssertFactories.list(Member.class))
.hasSize(5)
.element(0).satisfies(member -> assertThat(member.name).isEqualTo("Homer"));
assertThat(mockMvc.get().uri("/family")).bodyJson()
.extractingPath("$.members")
.convertTo(InstanceOfAssertFactories.list(Member::class.java))
.hasSize(5)
.element(0).satisfies(ThrowingConsumer { member: Member ->
assertThat(member.name).isEqualTo("Homer")
})
还支持 JSONAssert。可以将响应的主体与 `Resource`或一段内容进行匹配。如果内容以 `.json ` 结尾,我们将在类路径上查找与该名称匹配的文件:
JSONAssert is also supported. The body of the
response can be matched against a Resource
or a content. If the content ends with
`.json ` we look for a file matching that name on the classpath:
-
Java
-
Kotlin
assertThat(mockMvc.get().uri("/family")).bodyJson()
.isStrictlyEqualTo("sample/simpsons.json");
assertThat(mockMvc.get().uri("/family")).bodyJson()
.isStrictlyEqualTo("sample/simpsons.json")
如果您希望使用其他库,可以提供https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/json/JsonComparator.html[JsonComparator
] 的一个实现。
If you prefer to use another library, you can provide an implementation of
JsonComparator
.