spring-webflux - Spring Cloud Contract EXPLICIT 和 WEBTESTCLIENT 测试模式
问题描述
我想使用 Spring Cloud Contract 来生成我的合约并验证它们。我想使用 Spring WebFlux 和 Junit5。这是我的控制器:
@RestController
@Slf4j
public class HelloWorldPortRESTAdapter implements HelloWorldPort {
@GetMapping(value = "/hello-world", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Override
public Mono<String> helloWorld() {
return Mono.just("Hello World!");
}
}
这是云合约 Maven 插件配置:
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<basePackageForTests>com.example.feedproviderapi.contract</basePackageForTests>
<testFramework>JUNIT5</testFramework>
<testMode>EXPLICIT</testMode>
</configuration>
</plugin>
但我不知道基础测试类应该是什么样子。我试过这个:
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BaseTestClass {
@LocalServerPort
private int port;
@BeforeEach
void setup(){
RestAssured.baseURI = "http://localhost:" + this.port;
}
}
当我运行时mvn clean install
,它会返回java.net.ConnectException: Connection refused (Connection refused)
然后我将testMode
maven 插件中的属性更改为WEBTESTCLIENT
并更新BaseTestClass
如下:
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class BaseTestClass {
@Autowired
WebApplicationContext context;
@BeforeEach
void setup(){
RestAssuredWebTestClient.standaloneSetup(context);
}
}
当我mvn clean install
现在运行时,它再次返回:
You haven't configured a WebTestClient instance. You can do this statically
RestAssuredWebTestClient.mockMvc(..)
RestAssuredWebTestClient.standaloneSetup(..);
RestAssuredWebTestClient.webAppContextSetup(..);
or using the DSL:
given().
mockMvc(..). ..
顺便说一句,我也尝试过RestAssuredWebTestClient.standaloneSetup(new HelloWorldPortRESTAdapter());
,BaseTestClass
但结果是一样的。
那么我应该如何实现BaseTestClass
关于EXPLICIT
和WEBTESTCLIENT
testModes?
解决方案
我已经奋斗了 3 天,以使 RestAssuredWebTestClient 工作。
感谢 llooottt:https ://www.baeldung.com/spring-5-webclient
这就是我能做到的:
@WebFluxTest
public class AnimeControllerIntegrTest{
WebTestClient testClient;
@Test
public void get_RA() {
testClient = WebTestClient.bindToServer().baseUrl("http://localhost:8080/animes").build();
RestAssuredWebTestClient
.given()
.webTestClient(testClient)
.when()
.get()
.then()
.statusCode(OK.value())
.body("name" ,hasItem("paulo"))
;
}
}
推荐阅读
- ios - Firebase 存储:在模拟器上运行良好,但在设备上出错
- azure-devops - Azure DevOps 发布管道失败,密钥集不存在
- c# - 如何在 Blazor 中将一个组件引用传递给另一个组件?
- c++ - qDebug()< 输出中的 '*' 和 ')' 是什么意思
代码:
QByteArray receive=serialport.readAll(); qDebug()<<"receive="<<receive;
q调试输出:
receive= "\x02\x03*\x00\x00\x00\x00\x00\x
- java - Java 8 Map按orderid分组并按itemLeastDate排序
- javascript - 反应小部件中的无效挂钩调用
- aurelia - 创建自定义 aurelia 插件时,Aurelia 对话框不起作用
- python - 调试 CSV 文件
- python - 如何在 Python 中连接到 DB2 数据库?
- python-3.x - 如何使用 dask 和并行处理加载大 csv 文件,然后转换回 pandas