简体   繁体   English

使用Spring Cloud Contract的路由功能(reactive-webflux)

[英]Working of routing functions (reactive-webflux) with spring cloud contract

I am building a spring webflux project in which I have implemented Routing function as controllers with spring customer driven contract. 我正在构建一个Spring Webflux项目,在该项目中,我已实现路由功能作为具有Spring客户驱动合同的控制器。 I am facing issue while the tests cases are getting executed and I haven't found any solution regarding this online anywhere. 在执行测试用例的过程中,我遇到了问题,但是我在任何地方都找不到关于此的任何解决方案。 My requirement is to execute the generated tests and loading the application context for n no. 我的要求是执行生成的测试并为n no加载应用程序上下文。 of tests.Below is the sample code: 测试示例。下面是示例代码:

==========Base class===========

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class GetBase extends SampleParentBase{



    protected MockMvc mvc ;


      @MockBean
      private PersonService service;

      @MockBean
      private ServerRequest request;

      @Before
      public void setup() throws Exception {
          mvc = MockMvcBuilders
                    .webAppContextSetup(context).build();

}



}



============groovy file==================


Contract.make {
    description "."
    request {
        method GET()
        urlPath('/person/1')
        headers {
            contentType(applicationJson())
            header('''Accept''', applicationJson())
                }
            }
            response {
                headers {
                    contentType(applicationJson())
                }
                status 200
                bodyMatchers {

                    jsonPath('$.name', byType())
                    jsonPath('$.age', byType())
                    jsonPath('$.pId', byType())

                }
                body ('''{

                    "name":"string",
                    "age":20,
                    "pId":"string"
}''')
            }
        }


=======================Router Configuration====================

@Configuration
public class RoutesConfiguration {

    @Autowired
    PersonRespository personRespository;

    @Bean
    RouterFunction<?> routes() {


        return nest(path("/person"),

          route(RequestPredicates.GET("/{id}"),
            request -> ok().body(personRespository.findById(request.pathVariable("id")), Person.class))         
            .andRoute(method(HttpMethod.GET),
                  request -> ok().body(personRespository.findAll(), Person.class))  

        );
    }
}

We've updated the snapshot documentation to contain information on how to work with Web Flux. 我们更新了快照文档,以包含有关如何使用Web Flux的信息。 You can check it out here https://cloud.spring.io/spring-cloud-contract/2.0.x/single/spring-cloud-contract.html#_working_with_web_flux and here you have a sample with Web Flux https://github.com/spring-cloud-samples/spring-cloud-contract-samples/tree/master/producer_webflux . 您可以在这里https://cloud.spring.io/spring-cloud-contract/2.0.x/single/spring-cloud-contract.html#_working_with_web_flux进行检查,这里还有一个Web Flux示例https:// github.com/spring-cloud-samples/spring-cloud-contract-samples/tree/master/producer_webflux Let me copy the part of the documentation for your convenience 为了方便起见,让我复制文档的一部分

Spring Cloud Contract requires the usage of EXPLICIT mode in your generated tests to work with Web Flux. Spring Cloud Contract需要在生成的测试中使用EXPLICIT模式才能与Web Flux一起使用。

Maven. Maven的。

<plugin>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-contract-maven-plugin</artifactId>
    <version>${spring-cloud-contract.version}</version>
    <extensions>true</extensions>
    <configuration>
        <testMode>EXPLICIT</testMode>
    </configuration>
</plugin>

Gradle. 摇篮。

contracts {
        testMode = 'EXPLICIT'
}

The following example shows how to set up a base class and Rest Assured for Web Flux: 以下示例显示如何为Web Flux设置基类和“确保放心”:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = BeerRestBase.Config.class,
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
        properties = "server.port=0")
public abstract class BeerRestBase {

    // your tests go here

    // in this config class you define all controllers and mocked services
@Configuration
@EnableAutoConfiguration
static class Config {

    @Bean
    PersonCheckingService personCheckingService()  {
        return personToCheck -> personToCheck.age >= 20;
    }

    @Bean
    ProducerController producerController() {
        return new ProducerController(personCheckingService());
    }
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM