此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10spring-doc.cadn.net.cn

RestTest客户端

RestTestClient是专为测试服务器应用程序而设计的 HTTP 客户端。它包裹着 Spring的RestClient并使用它来执行请求, 但暴露了用于验证响应的测试外观。RestTestClient可用于 执行端到端 HTTP 测试。它也可以用来测试 Spring MVC 通过 MockMvc 没有正在运行的服务器的应用程序。spring-doc.cadn.net.cn

设置

要设置RestTestClient您需要选择要绑定到的服务器设置。这可以是一个 多个 MockMvc 设置选项,或与实时服务器的连接。spring-doc.cadn.net.cn

绑定到控制器

此设置允许您通过模拟请求和响应对象测试特定控制器, 没有正在运行的服务器。spring-doc.cadn.net.cn

RestTestClient client =
		RestTestClient.bindToController(new TestController()).build();
val client = RestTestClient.bindToController(TestController()).build()

绑定到ApplicationContext

此设置允许您使用 Spring MVC 加载 Spring 配置 基础设施和控制器声明,并使用它通过模拟请求处理请求 和响应对象,无需运行服务器。spring-doc.cadn.net.cn

@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

绑定到路由器功能

此设置允许您通过 模拟请求和响应对象,无需运行服务器。spring-doc.cadn.net.cn

RouterFunction<?> route = ...
client = RestTestClient.bindToRouterFunction(route).build();
val route: RouterFunction<*> = ...
val client = RestTestClient.bindToRouterFunction(route).build()

绑定到服务器

此设置连接到正在运行的服务器以执行完整的端到端 HTTP 测试:spring-doc.cadn.net.cn

client = RestTestClient.bindToServer().baseUrl("http://localhost:8080").build();
client = RestTestClient.bindToServer().baseUrl("http://localhost:8080").build()

客户端配置

除了前面描述的服务器设置选项外,您还可以配置客户端 选项,包括基本 URL、默认标头、客户端过滤器等。这些选项 在初始bindTo调用,如下所示:spring-doc.cadn.net.cn

client = RestTestClient.bindToController(new TestController())
		.baseUrl("/test")
		.build();
client = RestTestClient.bindToController(TestController())
		.baseUrl("/test")
		.build()

编写测试

RestTestClient提供与RestClient直到使用exchange().spring-doc.cadn.net.cn

调用exchange(),RestTestClientRestClient和 而是继续使用工作流来验证响应。spring-doc.cadn.net.cn

若要断言响应状态和标头,请使用以下命令:spring-doc.cadn.net.cn

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 木星。spring-doc.cadn.net.cn

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) }
	)

然后,您可以选择通过以下方法之一解码响应正文:spring-doc.cadn.net.cn

如果内置断言不足,可以改用对象和 执行任何其他断言:spring-doc.cadn.net.cn

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:spring-doc.cadn.net.cn

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>.

暂无内容

如果响应不需要包含内容,您可以按如下方式断言:spring-doc.cadn.net.cn

client.post().uri("/persons")
		.body(person)
		.exchange()
		.expectStatus().isCreated()
		.expectBody().isEmpty();
client.post().uri("/persons")
		.body(person)
		.exchange()
		.expectStatus().isCreated()
		.expectBody().isEmpty()

如果要忽略响应内容,以下将释放内容,不带任何断言:spring-doc.cadn.net.cn

client.get().uri("/persons/123")
		.exchange()
		.expectStatus().isNotFound()
		.expectBody(Void.class);
client.get().uri("/persons/123")
		.exchange()
		.expectStatus().isNotFound
		.expectBody<Unit>()

JSON 内容

您可以使用expectBody()没有目标类型来对原始 内容,而不是通过更高级别的对象。spring-doc.cadn.net.cn

要使用 JSONAssert 验证完整的 JSON 内容,请执行以下作:spring-doc.cadn.net.cn

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 内容,请执行以下作:spring-doc.cadn.net.cn

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")