【发布时间】:2018-08-24 11:48:09
【问题描述】:
我的 SpringBootTest 设置是这样的
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ControllerTest {
@Autowired
private WebTestClient webTestClient;
@Test
public void postTest() {
// Setup data
Form form = new Form();
form.setName("Test1");
form.setDescription("Description1");
// Run method
webTestClient.post()
.uri("/api/v1/data")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.syncBody(form)
.exchange()
.expectStatus()
.isOk()
.expectBody()
.jsonPath("data.id").isNotEmpty()
.jsonPath("data.name").isEqualTo(form.getName())
.jsonPath("data.description").isEqualTo(form.getDescription())
.jsonPath("data.status").isEqualTo("A");
}
}
我无法找到让WebTestClient 注销所有 HTTP 请求和响应的方法。
我想将WebTestClient 保留为@Autowired。可能我可以创建某种应用程序配置或配置类来执行此操作,但不幸的是我还没有找到方法。
【问题讨论】:
-
@thomas77 问题是他们手动实例化
WebTestClient而我想将@Autowired保留在
标签: java spring spring-mvc spring-test spring-webflux