此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10! |
RestTest客户端
RestTestClient
是专为测试服务器应用程序而设计的 HTTP 客户端。它包裹着
Spring的RestClient
并使用它来执行请求,
但暴露了用于验证响应的测试外观。RestTestClient
可用于
执行端到端 HTTP 测试。它也可以用来测试 Spring MVC
通过 MockMvc 没有正在运行的服务器的应用程序。
设置
要设置RestTestClient
您需要选择要绑定到的服务器设置。这可以是一个
多个 MockMvc 设置选项,或与实时服务器的连接。
绑定到控制器
此设置允许您通过模拟请求和响应对象测试特定控制器, 没有正在运行的服务器。
-
Java
-
Kotlin
RestTestClient client =
RestTestClient.bindToController(new TestController()).build();
val client = RestTestClient.bindToController(TestController()).build()
绑定到ApplicationContext
此设置允许您使用 Spring MVC 加载 Spring 配置 基础设施和控制器声明,并使用它通过模拟请求处理请求 和响应对象,无需运行服务器。
-
Java
-
Kotlin
@SpringJUnitConfig(WebConfig.class) (1)
class MyTests {
RestTestClient client;
@BeforeEach
void setUp(ApplicationContext context) { (2)
client = RestTestClient.bindToApplicationContext(context).build(); (3)
}
}
1 | 指定要加载的配置 |
2 | 注入配置 |
3 | 创建RestTestClient |
@SpringJUnitConfig(WebConfig::class) (1)
class MyTests {
lateinit var client: RestTestClient
@BeforeEach
fun setUp(context: ApplicationContext) { (2)
client = RestTestClient.bindToApplicationContext(context).build() (3)
}
}
1 | 指定要加载的配置 |
2 | 注入配置 |
3 | 创建RestTestClient |
绑定到路由器功能
此设置允许您通过 模拟请求和响应对象,无需运行服务器。
-
Java
-
Kotlin
RouterFunction<?> route = ...
client = RestTestClient.bindToRouterFunction(route).build();
val route: RouterFunction<*> = ...
val client = RestTestClient.bindToRouterFunction(route).build()
编写测试
RestTestClient
提供与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 中的软断言支持和assertAll()
支持
JUnit 木星。
-
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()
当需要解码为具有泛型的目标类型时,请查找重载的方法
接受ParameterizedTypeReference 而不是Class<T> . |
暂无内容
如果响应不需要包含内容,您可以按如下方式断言:
-
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")