簡體   English   中英

Spring Cloud Contract EXPLICIT 和 WEBTESTCLIENT 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但結果是一樣的。

所以,我應該如何落實BaseTestClassEXPLICITWEBTESTCLIENT 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

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[]
}

}

嘗試傳遞ApplicationContext實例而不是WebApplicationContext

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM