我有测试用例
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@AutoConfigureWebTestClient
public class PaymentsITest {
@Autowired
private WebTestClient client1;
@Test
public void getPayment() {
client1.get().uri("/v1/payments/123" )
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectBody()
.jsonPath("$.group_header.identification").exists()
.jsonPath("$.group_header.date_time").exists()
.jsonPath("$.response").exists()
;
}
}
我需要检查 response
对象内的属性。
有没有办法将方法 expectBody()
返回的 WebTestClient.BodyContentSpec
转换为 JSON 对象或 JSON 字符串?
最佳答案
找到了
使用 expectBody().returnResult().getResponseBody()
它将返回 byte[]
形式的响应,然后将 byte[]
转换为字符串
String s=new String(client1.get().uri("/v1/payments/123" )
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectBody().returnResult().getResponseBody());
System.out.println(s);
关于java - 将 WebTestClient.BodyContentSpec 转换为 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57907689/