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:
parent
185a3a12c2
commit
abd46c4349
@ -92,8 +92,8 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.tomakehurst</groupId>
|
<groupId>com.github.tomakehurst</groupId>
|
||||||
<artifactId>wiremock</artifactId>
|
<artifactId>wiremock-standalone</artifactId>
|
||||||
<version>2.24.1</version>
|
<version>2.26.0</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
@ -1,37 +1,67 @@
|
|||||||
package com.baeldung.reactive;
|
package com.baeldung.reactive;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||||
import org.junit.jupiter.api.Test;
|
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.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.ClientResponse;
|
||||||
import org.springframework.web.reactive.function.client.WebClient;
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
|
||||||
import com.baeldung.reactive.model.Foo;
|
import com.baeldung.reactive.model.Foo;
|
||||||
|
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||||
|
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
public class ReactiveIntegrationTest {
|
public class ReactiveIntegrationTest {
|
||||||
|
|
||||||
private WebClient client;
|
private WebClient client;
|
||||||
|
private int singleRequestTime = 1000;
|
||||||
|
private WireMockServer wireMockServer;
|
||||||
|
|
||||||
@BeforeEach
|
@Before
|
||||||
public void before() {
|
public void setup() {
|
||||||
client = WebClient.create("http://localhost:8080");
|
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
|
@Test
|
||||||
public void whenMonoReactiveEndpointIsConsumed_thenCorrectOutput() {
|
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());
|
System.out.println(fooMono.subscribe());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenFluxReactiveEndpointIsConsumed_thenCorrectOutput() throws InterruptedException {
|
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()
|
.retrieve()
|
||||||
.bodyToFlux(Foo.class).log()
|
.bodyToFlux(Foo.class).log()
|
||||||
.subscribe(System.out::println);
|
.subscribe(System.out::println);
|
||||||
|
@ -5,6 +5,7 @@ import org.junit.Before;
|
|||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||||
|
|
||||||
@ -19,15 +20,19 @@ import static org.junit.Assert.assertEquals;
|
|||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
|
@DirtiesContext
|
||||||
public class ClientIntegrationTest {
|
public class ClientIntegrationTest {
|
||||||
|
|
||||||
private WireMockServer wireMockServer;
|
private WireMockServer wireMockServer;
|
||||||
|
|
||||||
|
private Client client;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
wireMockServer = new WireMockServer(wireMockConfig().port(8089));
|
wireMockServer = new WireMockServer(wireMockConfig().dynamicPort());
|
||||||
wireMockServer.start();
|
wireMockServer.start();
|
||||||
configureFor("localhost", wireMockServer.port());
|
configureFor("localhost", wireMockServer.port());
|
||||||
|
client = new Client("http://localhost:" + wireMockServer.port());
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
@ -52,8 +57,6 @@ public class ClientIntegrationTest {
|
|||||||
.boxed()
|
.boxed()
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
Client client = new Client("http://localhost:8089");
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
List<User> users = client.fetchUsers(userIds)
|
List<User> users = client.fetchUsers(userIds)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user