简体   繁体   中英

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

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:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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