【发布时间】:2019-09-28 06:16:31
【问题描述】:
我想使用 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)
然后我将 maven 插件中的 testMode 属性更改为 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(..). ..
顺便说一句,我在BaseTestClass 中也尝试过RestAssuredWebTestClient.standaloneSetup(new HelloWorldPortRESTAdapter());,但结果是一样的。
那么对于EXPLICIT 和WEBTESTCLIENT testModes,我应该如何实现BaseTestClass?
【问题讨论】:
标签: spring-webflux junit5 spring-cloud-contract