春云合同显式与WEBTESTCLIENT测试模式 [英] Spring Cloud Contract EXPLICIT and WEBTESTCLIENT testMode

查看:24
本文介绍了春云合同显式与WEBTESTCLIENT测试模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Spring Cloud Contact来生成我的合同并进行验证。我想使用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 plugin中的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());,但结果是相同的。

那么我应该如何实现关于EXPLICITWEBTESTCLIENTBaseTestClass测试模式?

推荐答案

我已经挣扎了3天,才使RestAssuredWebTestClient正常工作。

感谢您: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"))
        ;
    }

}

这篇关于春云合同显式与WEBTESTCLIENT测试模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆