Mel*_*ius 0 java spring unit-testing spring-test
我有测试用例
@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()
;
}
}
Run Code Online (Sandbox Code Playgroud)
我需要检查对象内部的属性response。有没有办法将WebTestClient.BodyContentSpec方法返回的内容转换expectBody()为 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3900 次 |
| 最近记录: |