【问题标题】:Working of routing functions (reactive-webflux) with spring cloud contract使用 Spring Cloud 合约处理路由功能(reactive-webflux)
【发布时间】:2018-07-27 04:57:32
【问题描述】:

我正在构建一个 spring webflux 项目,其中我已经实现了路由功能作为具有 spring 客户驱动合同的控制器。 我在执行测试用例时遇到了问题,我在任何地方都没有找到任何关于此问题的在线解决方案。 我的要求是执行生成的测试并为 n 加载应用程序上下文。的测试。下面是示例代码:

==========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))  

        );
    }
}

【问题讨论】:

    标签: spring reactive-programming spring-webflux


    【解决方案1】:

    我们更新了快照文档以包含有关如何使用 Web Flux 的信息。你可以在这里查看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 的示例。为了您的方便,让我复制文档的一部分

    Spring Cloud Contract 需要在生成的测试中使用 EXPLICIT 模式才能使用 Web Flux。

    马文。

    <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>
    

    分级。

    contracts {
            testMode = 'EXPLICIT'
    }
    

    以下示例展示了如何为 Web Flux 设置基类和 Rest Assured:

    @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());
        }
    }
    
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 2019-12-04
    • 2021-08-04
    • 2021-11-08
    • 2020-12-02
    • 2020-08-12
    • 1970-01-01
    相关资源
    最近更新 更多