【问题标题】:Spring Cloud Contract EXPLICIT and WEBTESTCLIENT testModeSpring Cloud Contract EXPLICIT 和 WEBTESTCLIENT 测试模式
【发布时间】:2019-09-28 06:16:31
【问题描述】:

我想使用 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(..). ..

顺便说一句,我在BaseTestClass 中也尝试过RestAssuredWebTestClient.standaloneSetup(new HelloWorldPortRESTAdapter());,但结果是一样的。

那么对于EXPLICITWEBTESTCLIENT testModes,我应该如何实现BaseTestClass

【问题讨论】:

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


    【解决方案1】:

    请查看spring cloud合约样本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[]
    }
    

    }

    【讨论】:

    • 嗨@Marcin,我在问这个问题之前检查了这些样本。我试图实现一个非常简单的例子,但我做不到。你能告诉我我的BaseTestClass 中缺少什么吗?
    • 您的测试框架必须与 redt 保证设置保持一致。例如。您有明确的但使用 webtestclient。将其设置为 webtestclient 应该可以工作
    • 是的,我做到了。正如我在问题中所说,我将其更改为 webtestclient 并更新了 BaseTestClass 与它对齐。但当时我得到了不同的错误(我在我的问题中也写了这个)。
    • 你能在某处发布样本吗?
    【解决方案2】:

    尝试传递 ApplicationContext 实例而不是 WebApplicationContext

    【讨论】:

      【解决方案3】:

      我已经努力了 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"))
              ;
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-29
        • 1970-01-01
        • 1970-01-01
        • 2018-10-08
        • 1970-01-01
        相关资源
        最近更新 更多