I'm trying to use spring cloud contract with a rest service using spring 5 routing but it doesn't work. I'm in the client side and i'm trying to use a stub runner within a junit test. If i use i classic @RestController and flux it works fine but if i try to change the controller using a RouterFunction it doesn't work and i obtain a 404. This is my sample code.
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
...
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Routing.java
@Configuration
@EnableWebFlux
public class Routing {
@Autowired
private TestLoginController loginController;
@Bean
public HttpHandler routerFunction() {
return WebHttpHandlerBuilder
.webHandler(RouterFunctions.toWebHandler(createRouterFunction()))
.build();
}
private RouterFunction<ServerResponse> createRouterFunction() {
return route(POST("/testlogin"), loginController::testLogin);
}
}
TestLoginController.java
@Component
public class TestLoginController {
@Autowired
private TestLoginService testLoginService;
public Mono<ServerResponse> testLogin(ServerRequest request) {
return Mono.just(request)
.flatMap(req -> ServerResponse.ok()
.body(testLoginService.testLogin(request.bodyToMono(LoginRequest.class)), LoginResponse.class)
);
}
}
DemoApplicationTest.java
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureStubRunner(ids = {"groupId:artifactId:+:stubs:8090"},
stubsMode = StubRunnerProperties.StubsMode.LOCAL)
public class DemoApplicationTests {
@LocalServerPort
private int port;
@Test
public void contextLoads() throws Exception {
LoginRequest request = new LoginRequest();
WebTestClient
.bindToServer()
.baseUrl("http://localhost:" + port)
.build()
.post()
.uri("testlogin").accept(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromObject(request))
.exchange()
.expectStatus().isOk()
.expectBody()
....
}
}
I have the same problem even if i remove the @AutoConfigureStubRunner annotation. If i only add the stub runner dependency i figure this behavior i find this problem. I also tried to use the latest versio of spring boot and spring cloud contract but i have the same issue. anyone can help me?
Spring Cloud Contract Stub Runner just starts a WireMock server on a given (or random port). Nothing related to WebTestClient
takes place with Stub Runner. In other words, most likely you've misconfigured WebTestClient
.
Let's try to ensure that you're not misusing the project. If you have service A calling service B via WebClient, then service B should have contracts defined, from which tests and spans would be created. Then on the service A side you will use Spring Cloud Contract Stub Runner to start the stubs of service B. Whatever you use (RestTemplate, WebClient, whatever) you will still send an HTTP call to a WireMock server that we start for you.
Example of how to use Spring Cloud Contract Stub Runner with WebTestClient (taken from: https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/master/consumer/src/test/java/com/example/BeerControllerWebClientTest.java )
package com.example;
import java.util.Objects;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner;
import org.springframework.cloud.contract.stubrunner.spring.StubRunnerPort;
import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
/**
* @author Marcin Grzejszczak
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
@AutoConfigureMockMvc
@AutoConfigureJsonTesters
//remove::start[]
@AutoConfigureStubRunner(stubsMode = StubRunnerProperties.StubsMode.LOCAL, ids = "com.example:beer-api-producer-webflux")
//remove::end[]
@DirtiesContext
//@org.junit.Ignore
public class BeerControllerWebClientTest extends AbstractTest {
//remove::start[]
@StubRunnerPort("beer-api-producer-webflux") int producerPort;
//remove::end[]
@Test public void should_give_me_a_beer_when_im_old_enough() throws Exception {
//remove::start[]
WebTestClient.bindToServer()
.build()
.post()
.uri("http://localhost:" + producerPort + "/check")
.syncBody(new WebClientPerson("marcin", 22))
.header("Content-Type", "application/json")
.exchange()
.expectStatus().is2xxSuccessful()
.expectBody(WebClientResponse.class)
.isEqualTo(new WebClientResponse(WebClientResponseStatus.OK));
//remove::end[]
}
@Test public void should_reject_a_beer_when_im_too_young() throws Exception {
//remove::start[]
WebTestClient.bindToServer()
.build()
.post()
.uri("http://localhost:" + producerPort + "/check")
.syncBody(new WebClientPerson("marcin", 17))
.header("Content-Type", "application/json")
.exchange()
.expectStatus().is2xxSuccessful()
.expectBody(WebClientResponse.class)
.isEqualTo(new WebClientResponse(WebClientResponseStatus.NOT_OK));
//remove::end[]
}
}
class WebClientPerson {
public String name;
public int age;
public WebClientPerson(String name, int age) {
this.name = name;
this.age = age;
}
public WebClientPerson() {
}
}
class WebClientResponse {
public WebClientResponseStatus status;
WebClientResponse(WebClientResponseStatus status) {
this.status = status;
}
WebClientResponse() {
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WebClientResponse that = (WebClientResponse) o;
return status == that.status;
}
@Override
public int hashCode() {
return Objects.hash(status);
}
}
enum WebClientResponseStatus {
OK, NOT_OK
}
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.