Spring Cloud Contract EXPLICIT和WEBTESTCLIENT testMode

时间:2019-05-10 10:36:12

标签: spring-webflux junit5 spring-cloud-contract

我想使用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,但是结果是一样的。

那么我应该如何实现关于BaseTestClassEXPLICIT testModes的WEBTESTCLIENT

3 个答案:

答案 0 :(得分:0)

请检查春季云合同样本https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/master/producer_webflux_webtestclient

还有junit5

https://github.com/spring-cloud-samples/spring-cloud-contract-samples/tree/master/producer_with_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"))
        ;
    }

}