RestTestClient
RestTestClient 是一个专为测试服务器应用程序设计的 HTTP 客户端。它封装了
Spring 的 RestClient 并使用它来执行请求,
但提供了一个测试门面用于验证响应。RestTestClient 可用于执行端到端 HTTP 测试。它也可以通过 MockMvc 在没有运行服务器的情况下测试 Spring MVC 应用程序。
设置
要设置 RestTestClient,您需要选择一个要绑定的服务器设置。这可以是几种 MockMvc 设置选项之一,也可以是与实时服务器的连接。
绑定到控制器
此设置允许您通过模拟请求和响应对象测试特定的控制器,而无需运行服务器。
-
Java
-
Kotlin
RestTestClient client =
RestTestClient.bindToController(new TestController()).build();
val client = RestTestClient.bindToController(TestController()).build()
绑定到 ApplicationContext
此设置允许您加载包含 Spring MVC 基础设施和控制器声明的 Spring 配置,并使用它通过模拟请求和响应对象处理请求,而无需运行服务器。
- Java
-
@SpringJUnitConfig(WebConfig.class) [id="CO1-1"][id="CO1-1"][id="CO1-1"](1) class MyTests { RestTestClient client; @BeforeEach void setUp(ApplicationContext context) { [id="CO1-2"][id="CO1-2"][id="CO1-2"](2) client = RestTestClient.bindToApplicationContext(context).build(); [id="CO1-3"][id="CO1-3"][id="CO1-3"](3) } }
<1> 指定要加载的配置 <1> 注入配置 <1> 创建 `RestTestClient`
- Kotlin
-
@SpringJUnitConfig(WebConfig::class) [id="CO2-1"][id="CO1-4"][id="CO2-1"](1) class MyTests { lateinit var client: RestTestClient @BeforeEach fun setUp(context: ApplicationContext) { [id="CO2-2"][id="CO1-5"][id="CO2-2"](2) client = RestTestClient.bindToApplicationContext(context).build() [id="CO2-3"][id="CO1-6"][id="CO2-3"](3) } }
<1> 指定要加载的配置 <1> 注入配置 <1> 创建 `RestTestClient`
绑定到路由器函数
此设置允许您通过模拟请求和响应对象测试 功能性端点,而无需运行服务器。
-
Java
-
Kotlin
RouterFunction<?> route = ...
client = RestTestClient.bindToRouterFunction(route).build();
val route: RouterFunction<*> = ...
val client = RestTestClient.bindToRouterFunction(route).build()
编写测试
RestTestClient 提供的 API 与 RestClient 完全相同,直到通过 exchange() 执行请求。
在调用 exchange() 之后,RestTestClient 与 RestClient 不同,而是继续执行验证响应的工作流。
要断言响应状态和头,请使用以下内容:
-
Java
-
Kotlin
client.get().uri("/persons/1")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON);
client.get().uri("/persons/1")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON)
如果您希望即使其中一个断言失败,所有断言也都能被断言,您可以使用 expectAll(..) 而不是多个链式 expect*(..) 调用。此功能类似于 AssertJ 中的_软断言_支持和 JUnit Jupiter 中的 assertAll() 支持。
-
Java
-
Kotlin
client.get().uri("/persons/1")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectAll(
spec -> spec.expectStatus().isOk(),
spec -> spec.expectHeader().contentType(MediaType.APPLICATION_JSON)
);
client.get().uri("/persons/1")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectAll(
{ spec -> spec.expectStatus().isOk() },
{ spec -> spec.expectHeader().contentType(MediaType.APPLICATION_JSON) }
)
然后,您可以通过以下方法之一解码响应体:
-
expectBody(Class<T>):解码为单个对象。 -
expectBody():解码为byte[]用于 JSON 内容 或空体。
如果内置断言不足,您可以转而消费对象并执行任何其他断言:
-
Java
-
Kotlin
client.get().uri("/persons/1")
.exchange()
.expectStatus().isOk()
.expectBody(Person.class)
.consumeWith(result -> {
// custom assertions (for example, AssertJ)...
});
client.get().uri("/persons/1")
.exchange()
.expectStatus().isOk()
.expectBody<Person>()
.consumeWith {
// custom assertions (for example, AssertJ)...
}
或者您可以退出工作流并获取 EntityExchangeResult:
-
Java
-
Kotlin
EntityExchangeResult<Person> result = client.get().uri("/persons/1")
.exchange()
.expectStatus().isOk()
.expectBody(Person.class)
.returnResult();
val result = client.get().uri("/persons/1")
.exchange()
.expectStatus().isOk
.expectBody<Person>()
.returnResult()
|
当您需要解码为带有泛型的目标类型时,请查找接受 |
无内容
如果响应预期没有内容,您可以如下断言:
-
Java
-
Kotlin
client.post().uri("/persons")
.body(person)
.exchange()
.expectStatus().isCreated()
.expectBody().isEmpty();
client.post().uri("/persons")
.body(person)
.exchange()
.expectStatus().isCreated()
.expectBody().isEmpty()
如果您想忽略响应内容,以下操作会在不进行任何断言的情况下释放内容:
-
Java
-
Kotlin
client.get().uri("/persons/123")
.exchange()
.expectStatus().isNotFound()
.expectBody(Void.class);
client.get().uri("/persons/123")
.exchange()
.expectStatus().isNotFound
.expectBody<Unit>()
JSON 内容
您可以在不指定目标类型的情况下使用 expectBody() 来对原始内容而不是通过更高级别的对象执行断言。
要使用 JSONAssert 验证完整的 JSON 内容:
-
Java
-
Kotlin
client.get().uri("/persons/1")
.exchange()
.expectStatus().isOk()
.expectBody()
.json("{\"name\":\"Jane\"}")
client.get().uri("/persons/1")
.exchange()
.expectStatus().isOk()
.expectBody()
.json("{\"name\":\"Jane\"}")
要使用 JSONPath 验证 JSON 内容:
-
Java
-
Kotlin
client.get().uri("/persons")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$[0].name").isEqualTo("Jane")
.jsonPath("$[1].name").isEqualTo("Jason");
client.get().uri("/persons")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$[0].name").isEqualTo("Jane")
.jsonPath("$[1].name").isEqualTo("Jason")