我想使用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
和EXPLICIT
testModes的WEBTESTCLIENT
?
答案 0 :(得分:0)
还有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[]
}
}
答案 1 :(得分:0)
尝试传递ApplicationContext
实例而不是WebApplicationContext
。
答案 2 :(得分:0)
为了使RestAssuredWebTestClient正常工作,我努力了3天。
感谢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"))
;
}
}