簡體   English   中英

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

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

我正在構建一個Spring Webflux項目,在該項目中,我已實現路由功能作為具有Spring客戶驅動合同的控制器。 在執行測試用例的過程中,我遇到了問題,但是我在任何地方都找不到關於此的任何解決方案。 我的要求是執行生成的測試並為n no加載應用程序上下文。 測試示例。下面是示例代碼:

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

        );
    }
}

我們更新了快照文檔,以包含有關如何使用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一起使用。

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>

搖籃。

contracts {
        testMode = 'EXPLICIT'
}

以下示例顯示如何為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