![](/img/trans.png)
[英]Why Spring Cloud Contract with Reactive Webflux mandate EXPLICIT testmode
[英]Spring Cloud Contract EXPLICIT and WEBTESTCLIENT testMode
我想使用 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(..). ..
順便說一句,我試過RestAssuredWebTestClient.standaloneSetup(new HelloWorldPortRESTAdapter());
在我的BaseTestClass
也是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"))
;
}
}
請查看 spring 雲合約示例https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/master/producer_webflux_webtestclient
和junit5
這
plugin
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<version>${spring-cloud-contract.version}</version>
<extensions>true</extensions>
<configuration>
<packageWithBaseClasses>com.example</packageWithBaseClasses>
<testMode>WEBTESTCLIENT</testMode>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<failIfNoTests>true</failIfNoTests>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit-platform-surefire-provider.version}</version>
</dependency>
</dependencies>
</plugin>
和junit5的基類
公共抽象類 BeerRestBase {
@BeforeEach
public void setup() {
// remove::start[]
RestAssuredWebTestClient.standaloneSetup(new ProducerController(personToCheck -> personToCheck.age >= 20));
// remove::end[]
}
}
嘗試傳遞ApplicationContext
實例而不是WebApplicationContext
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.