fix integration test cases (#8727)

* fix integration test cases

* fix integration test cases

* used dynamic port in the integration test cases
This commit is contained in:
Amit Pandey 2020-03-23 19:58:34 +05:30 committed by GitHub
parent 185a3a12c2
commit abd46c4349
3 changed files with 47 additions and 14 deletions

View File

@ -92,8 +92,8 @@
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.24.1</version>
<artifactId>wiremock-standalone</artifactId>
<version>2.26.0</version>
<scope>test</scope>
</dependency>

View File

@ -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<ClientResponse> 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<ClientResponse> 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);

View File

@ -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<User> users = client.fetchUsers(userIds)