From abd46c4349f07b36e279b7fa6fa310ac1ea52a17 Mon Sep 17 00:00:00 2001 From: Amit Pandey Date: Mon, 23 Mar 2020 19:58:34 +0530 Subject: [PATCH] fix integration test cases (#8727) * fix integration test cases * fix integration test cases * used dynamic port in the integration test cases --- spring-5-reactive-client/pom.xml | 4 +- .../reactive/ReactiveIntegrationTest.java | 48 +++++++++++++++---- .../simultaneous/ClientIntegrationTest.java | 9 ++-- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/spring-5-reactive-client/pom.xml b/spring-5-reactive-client/pom.xml index e3c41f8b84..cc728398f3 100644 --- a/spring-5-reactive-client/pom.xml +++ b/spring-5-reactive-client/pom.xml @@ -92,8 +92,8 @@ com.github.tomakehurst - wiremock - 2.24.1 + wiremock-standalone + 2.26.0 test diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/ReactiveIntegrationTest.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/ReactiveIntegrationTest.java index 394ff42e5f..1d2197a381 100644 --- a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/ReactiveIntegrationTest.java +++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/ReactiveIntegrationTest.java @@ -1,37 +1,67 @@ package com.baeldung.reactive; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.reactive.function.client.ClientResponse; import org.springframework.web.reactive.function.client.WebClient; import com.baeldung.reactive.model.Foo; +import com.github.tomakehurst.wiremock.WireMockServer; import reactor.core.publisher.Mono; +@RunWith(SpringRunner.class) @SpringBootTest public class ReactiveIntegrationTest { private WebClient client; - - @BeforeEach - public void before() { - client = WebClient.create("http://localhost:8080"); + private int singleRequestTime = 1000; + private WireMockServer wireMockServer; + + @Before + public void setup() { + wireMockServer = new WireMockServer(wireMockConfig().dynamicPort()); + wireMockServer.start(); + configureFor("localhost", wireMockServer.port()); + client = WebClient.create("http://localhost:" + wireMockServer.port()); } - // + @After + public void tearDown() { + wireMockServer.stop(); + } @Test public void whenMonoReactiveEndpointIsConsumed_thenCorrectOutput() { - final Mono fooMono = client.get().uri("/foos/123").exchange().log(); + stubFor(get(urlEqualTo("/foo/123")).willReturn(aResponse().withFixedDelay(singleRequestTime) + .withStatus(200) + .withHeader("Content-Type", "application/json") + .withBody("{\"id\":123, \"name\":\"foo\"}"))); + + final Mono fooMono = client.get().uri("/foo/123").exchange().log(); System.out.println(fooMono.subscribe()); } @Test public void whenFluxReactiveEndpointIsConsumed_thenCorrectOutput() throws InterruptedException { - client.get().uri("/foos") + stubFor(get(urlEqualTo("/foo")).willReturn(aResponse().withFixedDelay(singleRequestTime) + .withStatus(200) + .withHeader("Content-Type", "application/json") + .withBody("{\"id\":1, \"name\":\"foo\"}"))); + + client.get().uri("/foo") .retrieve() .bodyToFlux(Foo.class).log() .subscribe(System.out::println); diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/webclient/simultaneous/ClientIntegrationTest.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/webclient/simultaneous/ClientIntegrationTest.java index d74a64d9c9..43454daf17 100644 --- a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/webclient/simultaneous/ClientIntegrationTest.java +++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/webclient/simultaneous/ClientIntegrationTest.java @@ -5,6 +5,7 @@ import org.junit.Before; import org.junit.After; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; import com.github.tomakehurst.wiremock.WireMockServer; @@ -19,15 +20,19 @@ import static org.junit.Assert.assertEquals; @RunWith(SpringRunner.class) @SpringBootTest +@DirtiesContext public class ClientIntegrationTest { private WireMockServer wireMockServer; + private Client client; + @Before public void setup() { - wireMockServer = new WireMockServer(wireMockConfig().port(8089)); + wireMockServer = new WireMockServer(wireMockConfig().dynamicPort()); wireMockServer.start(); configureFor("localhost", wireMockServer.port()); + client = new Client("http://localhost:" + wireMockServer.port()); } @After @@ -52,8 +57,6 @@ public class ClientIntegrationTest { .boxed() .collect(Collectors.toList()); - Client client = new Client("http://localhost:8089"); - // Act long start = System.currentTimeMillis(); List users = client.fetchUsers(userIds)