[JAVA-12361] Code clean up and migrate to JUnit 5 tests
This commit is contained in:
parent
220f50f2e2
commit
47dc8477ae
@ -19,18 +19,18 @@ import ch.qos.logback.classic.spi.IThrowableProxy;
|
|||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Hooks;
|
import reactor.core.publisher.Hooks;
|
||||||
|
|
||||||
public class ConsumerFooServiceIntegrationTest {
|
class ConsumerFooServiceIntegrationTest {
|
||||||
|
|
||||||
FooService service = new FooService();
|
FooService service = new FooService();
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void clearLogList() {
|
void clearLogList() {
|
||||||
Hooks.onOperatorDebug();
|
Hooks.onOperatorDebug();
|
||||||
ListAppender.clearEventList();
|
ListAppender.clearEventList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFooWithNullId_whenProcessFoo_thenLogsWithDebugTrace() {
|
void givenFooWithNullId_whenProcessFoo_thenLogsWithDebugTrace() {
|
||||||
Foo one = new Foo(1, "nameverylong", 8);
|
Foo one = new Foo(1, "nameverylong", 8);
|
||||||
Foo two = new Foo(null, "nameverylong", 4);
|
Foo two = new Foo(null, "nameverylong", 4);
|
||||||
Flux<Foo> flux = Flux.just(one, two);
|
Flux<Foo> flux = Flux.just(one, two);
|
||||||
|
@ -10,7 +10,7 @@ import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec;
|
|||||||
* - com.baeldung.reactive.debugging.server.ServerDebuggingApplication
|
* - com.baeldung.reactive.debugging.server.ServerDebuggingApplication
|
||||||
* - com.baeldung.reactive.debugging.consumer.ConsumerDebuggingApplication
|
* - com.baeldung.reactive.debugging.consumer.ConsumerDebuggingApplication
|
||||||
*/
|
*/
|
||||||
public class ConsumerFooServiceLiveTest {
|
class ConsumerFooServiceLiveTest {
|
||||||
|
|
||||||
private static final String BASE_URL = "http://localhost:8082";
|
private static final String BASE_URL = "http://localhost:8082";
|
||||||
private static final String DEBUG_HOOK_ON = BASE_URL + "/debug-hook-on";
|
private static final String DEBUG_HOOK_ON = BASE_URL + "/debug-hook-on";
|
||||||
@ -19,14 +19,14 @@ public class ConsumerFooServiceLiveTest {
|
|||||||
private static WebTestClient client;
|
private static WebTestClient client;
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setup() {
|
static void setup() {
|
||||||
client = WebTestClient.bindToServer()
|
client = WebTestClient.bindToServer()
|
||||||
.baseUrl(BASE_URL)
|
.baseUrl(BASE_URL)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenRequestingDebugHookOn_thenObtainExpectedMessage() {
|
void whenRequestingDebugHookOn_thenObtainExpectedMessage() {
|
||||||
ResponseSpec response = client.get()
|
ResponseSpec response = client.get()
|
||||||
.uri(DEBUG_HOOK_ON)
|
.uri(DEBUG_HOOK_ON)
|
||||||
.exchange();
|
.exchange();
|
||||||
@ -37,7 +37,7 @@ public class ConsumerFooServiceLiveTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenRequestingDebugHookOff_thenObtainExpectedMessage() {
|
void whenRequestingDebugHookOff_thenObtainExpectedMessage() {
|
||||||
ResponseSpec response = client.get()
|
ResponseSpec response = client.get()
|
||||||
.uri(DEBUG_HOOK_OFF)
|
.uri(DEBUG_HOOK_OFF)
|
||||||
.exchange();
|
.exchange();
|
||||||
|
@ -1,27 +1,24 @@
|
|||||||
package com.baeldung.reactive.errorhandling;
|
package com.baeldung.reactive.errorhandling;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
|
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
@AutoConfigureWebTestClient(timeout = "10000")
|
@AutoConfigureWebTestClient(timeout = "10000")
|
||||||
public class ErrorHandlingIntegrationTest {
|
class ErrorHandlingIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebTestClient webTestClient;
|
private WebTestClient webTestClient;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenErrorReturn_whenUsernamePresent_thenOk() {
|
void givenErrorReturn_whenUsernamePresent_thenOk() {
|
||||||
|
|
||||||
webTestClient.get()
|
webTestClient.get()
|
||||||
.uri("/api/endpoint1?name={username}", "Tony")
|
.uri("/api/endpoint1?name={username}", "Tony")
|
||||||
@ -31,7 +28,7 @@ public class ErrorHandlingIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenErrorReturn_whenNoUsername_thenOk() {
|
void givenErrorReturn_whenNoUsername_thenOk() {
|
||||||
|
|
||||||
webTestClient.get()
|
webTestClient.get()
|
||||||
.uri("/api/endpoint1")
|
.uri("/api/endpoint1")
|
||||||
@ -41,7 +38,7 @@ public class ErrorHandlingIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenResumeFallback_whenUsernamePresent_thenOk() {
|
void givenResumeFallback_whenUsernamePresent_thenOk() {
|
||||||
|
|
||||||
webTestClient.get()
|
webTestClient.get()
|
||||||
.uri("/api/endpoint2?name={username}", "Tony")
|
.uri("/api/endpoint2?name={username}", "Tony")
|
||||||
@ -51,7 +48,7 @@ public class ErrorHandlingIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenResumeFallback_whenNoUsername_thenOk() {
|
void givenResumeFallback_whenNoUsername_thenOk() {
|
||||||
|
|
||||||
webTestClient.get()
|
webTestClient.get()
|
||||||
.uri("/api/endpoint2")
|
.uri("/api/endpoint2")
|
||||||
@ -61,7 +58,7 @@ public class ErrorHandlingIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenResumeDynamicValue_whenUsernamePresent_thenOk() {
|
void givenResumeDynamicValue_whenUsernamePresent_thenOk() {
|
||||||
|
|
||||||
webTestClient.get()
|
webTestClient.get()
|
||||||
.uri("/api/endpoint3?name={username}", "Tony")
|
.uri("/api/endpoint3?name={username}", "Tony")
|
||||||
@ -71,7 +68,7 @@ public class ErrorHandlingIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenResumeDynamicValue_whenNoUsername_thenOk() {
|
void givenResumeDynamicValue_whenNoUsername_thenOk() {
|
||||||
|
|
||||||
webTestClient.get()
|
webTestClient.get()
|
||||||
.uri("/api/endpoint3")
|
.uri("/api/endpoint3")
|
||||||
@ -81,7 +78,7 @@ public class ErrorHandlingIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenResumeRethrow_whenUsernamePresent_thenOk() {
|
void givenResumeRethrow_whenUsernamePresent_thenOk() {
|
||||||
|
|
||||||
webTestClient.get()
|
webTestClient.get()
|
||||||
.uri("/api/endpoint4?name={username}", "Tony")
|
.uri("/api/endpoint4?name={username}", "Tony")
|
||||||
@ -91,7 +88,7 @@ public class ErrorHandlingIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenResumeRethrow_whenNoUsername_thenOk() {
|
void givenResumeRethrow_whenNoUsername_thenOk() {
|
||||||
|
|
||||||
webTestClient.get()
|
webTestClient.get()
|
||||||
.uri("/api/endpoint4")
|
.uri("/api/endpoint4")
|
||||||
@ -103,7 +100,7 @@ public class ErrorHandlingIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenGlobalErrorHandling_whenUsernamePresent_thenOk() {
|
void givenGlobalErrorHandling_whenUsernamePresent_thenOk() {
|
||||||
|
|
||||||
webTestClient.get()
|
webTestClient.get()
|
||||||
.uri("/api/endpoint5?name={username}", "Tony")
|
.uri("/api/endpoint5?name={username}", "Tony")
|
||||||
@ -113,7 +110,7 @@ public class ErrorHandlingIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenGlobalErrorHandling_whenNoUsername_thenOk() {
|
void givenGlobalErrorHandling_whenNoUsername_thenOk() {
|
||||||
|
|
||||||
webTestClient.get()
|
webTestClient.get()
|
||||||
.uri("/api/endpoint5")
|
.uri("/api/endpoint5")
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package com.baeldung.reactive.introduction;
|
package com.baeldung.reactive.introduction;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.reactivestreams.Subscriber;
|
import org.reactivestreams.Subscriber;
|
||||||
import org.reactivestreams.Subscription;
|
import org.reactivestreams.Subscription;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -14,12 +14,12 @@ import java.util.List;
|
|||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class ReactorIntegrationTest {
|
class ReactorIntegrationTest {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(ReactorIntegrationTest.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(ReactorIntegrationTest.class);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFlux_whenSubscribing_thenStream() {
|
void givenFlux_whenSubscribing_thenStream() {
|
||||||
|
|
||||||
List<Integer> elements = new ArrayList<>();
|
List<Integer> elements = new ArrayList<>();
|
||||||
|
|
||||||
@ -35,7 +35,7 @@ public class ReactorIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFlux_whenZipping_thenCombine() {
|
void givenFlux_whenZipping_thenCombine() {
|
||||||
List<String> elements = new ArrayList<>();
|
List<String> elements = new ArrayList<>();
|
||||||
|
|
||||||
Flux.just(1, 2, 3, 4)
|
Flux.just(1, 2, 3, 4)
|
||||||
@ -52,7 +52,7 @@ public class ReactorIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFlux_whenApplyingBackPressure_thenPushElementsInBatches() {
|
void givenFlux_whenApplyingBackPressure_thenPushElementsInBatches() {
|
||||||
|
|
||||||
List<Integer> elements = new ArrayList<>();
|
List<Integer> elements = new ArrayList<>();
|
||||||
|
|
||||||
@ -90,7 +90,7 @@ public class ReactorIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFlux_whenInParallel_thenSubscribeInDifferentThreads() throws InterruptedException {
|
void givenFlux_whenInParallel_thenSubscribeInDifferentThreads() throws InterruptedException {
|
||||||
List<String> threadNames = new ArrayList<>();
|
List<String> threadNames = new ArrayList<>();
|
||||||
|
|
||||||
Flux.just(1, 2, 3, 4)
|
Flux.just(1, 2, 3, 4)
|
||||||
@ -106,7 +106,7 @@ public class ReactorIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenConnectableFlux_whenConnected_thenShouldStream() {
|
void givenConnectableFlux_whenConnected_thenShouldStream() {
|
||||||
|
|
||||||
List<Integer> elements = new ArrayList<>();
|
List<Integer> elements = new ArrayList<>();
|
||||||
|
|
||||||
|
@ -2,17 +2,14 @@ package com.baeldung.reactive.security;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.security.test.context.support.WithMockUser;
|
import org.springframework.security.test.context.support.WithMockUser;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
|
|
||||||
@ExtendWith(SpringExtension.class)
|
@SpringBootTest(classes = SpringSecurity5Application.class)
|
||||||
@ContextConfiguration(classes = SpringSecurity5Application.class)
|
class SecurityIntegrationTest {
|
||||||
public class SecurityIntegrationTest {
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ApplicationContext context;
|
private ApplicationContext context;
|
||||||
@ -20,14 +17,14 @@ public class SecurityIntegrationTest {
|
|||||||
private WebTestClient webTestClient;
|
private WebTestClient webTestClient;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setup() {
|
void setup() {
|
||||||
webTestClient = WebTestClient.bindToApplicationContext(context)
|
webTestClient = WebTestClient.bindToApplicationContext(context)
|
||||||
.configureClient()
|
.configureClient()
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenNoCredentials_thenRedirectToLogin() {
|
void whenNoCredentials_thenRedirectToLogin() {
|
||||||
webTestClient.get()
|
webTestClient.get()
|
||||||
.uri("/")
|
.uri("/")
|
||||||
.exchange()
|
.exchange()
|
||||||
@ -36,7 +33,7 @@ public class SecurityIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@WithMockUser
|
@WithMockUser
|
||||||
public void whenHasCredentials_thenSeesGreeting() {
|
void whenHasCredentials_thenSeesGreeting() {
|
||||||
webTestClient.get()
|
webTestClient.get()
|
||||||
.uri("/")
|
.uri("/")
|
||||||
.exchange()
|
.exchange()
|
||||||
|
@ -4,9 +4,9 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
@SpringBootTest(classes = WebClientApplication.class)
|
@SpringBootTest(classes = WebClientApplication.class)
|
||||||
public class SpringContextTest {
|
class SpringContextTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ import java.util.concurrent.TimeUnit;
|
|||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@SpringBootTest(classes = WebClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
@SpringBootTest(classes = WebClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||||
public class WebClientIntegrationTest {
|
class WebClientIntegrationTest {
|
||||||
|
|
||||||
private static final String BODY_VALUE = "bodyValue";
|
private static final String BODY_VALUE = "bodyValue";
|
||||||
private static final ParameterizedTypeReference<Map<String, String>> MAP_RESPONSE_REF = new ParameterizedTypeReference<Map<String, String>>() {
|
private static final ParameterizedTypeReference<Map<String, String>> MAP_RESPONSE_REF = new ParameterizedTypeReference<Map<String, String>>() {
|
||||||
@ -53,7 +53,7 @@ public class WebClientIntegrationTest {
|
|||||||
private int port;
|
private int port;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDifferentWebClientCreationMethods_whenUsed_thenObtainExpectedResponse() {
|
void givenDifferentWebClientCreationMethods_whenUsed_thenObtainExpectedResponse() {
|
||||||
// WebClient creation
|
// WebClient creation
|
||||||
WebClient client1 = WebClient.create();
|
WebClient client1 = WebClient.create();
|
||||||
WebClient client2 = WebClient.create("http://localhost:" + port);
|
WebClient client2 = WebClient.create("http://localhost:" + port);
|
||||||
@ -83,7 +83,7 @@ public class WebClientIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDifferentMethodSpecifications_whenUsed_thenObtainExpectedResponse() {
|
void givenDifferentMethodSpecifications_whenUsed_thenObtainExpectedResponse() {
|
||||||
// request specification
|
// request specification
|
||||||
RequestBodyUriSpec uriSpecPost1 = createDefaultClient().method(HttpMethod.POST);
|
RequestBodyUriSpec uriSpecPost1 = createDefaultClient().method(HttpMethod.POST);
|
||||||
RequestBodyUriSpec uriSpecPost2 = createDefaultClient().post();
|
RequestBodyUriSpec uriSpecPost2 = createDefaultClient().post();
|
||||||
@ -103,7 +103,7 @@ public class WebClientIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDifferentUriSpecifications_whenUsed_thenObtainExpectedResponse() {
|
void givenDifferentUriSpecifications_whenUsed_thenObtainExpectedResponse() {
|
||||||
// uri specification
|
// uri specification
|
||||||
RequestBodySpec bodySpecUsingString = createDefaultPostRequest().uri("/resource");
|
RequestBodySpec bodySpecUsingString = createDefaultPostRequest().uri("/resource");
|
||||||
RequestBodySpec bodySpecUsingUriBuilder = createDefaultPostRequest().uri(
|
RequestBodySpec bodySpecUsingUriBuilder = createDefaultPostRequest().uri(
|
||||||
@ -140,7 +140,7 @@ public class WebClientIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDifferentBodySpecifications_whenUsed_thenObtainExpectedResponse() {
|
void givenDifferentBodySpecifications_whenUsed_thenObtainExpectedResponse() {
|
||||||
// request body specifications
|
// request body specifications
|
||||||
RequestHeadersSpec<?> headersSpecPost1 = createDefaultPostResourceRequest().body(
|
RequestHeadersSpec<?> headersSpecPost1 = createDefaultPostResourceRequest().body(
|
||||||
BodyInserters.fromPublisher(Mono.just(BODY_VALUE), String.class));
|
BodyInserters.fromPublisher(Mono.just(BODY_VALUE), String.class));
|
||||||
@ -194,7 +194,7 @@ public class WebClientIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPostSpecifications_whenHeadersAdded_thenObtainExpectedResponse() {
|
void givenPostSpecifications_whenHeadersAdded_thenObtainExpectedResponse() {
|
||||||
// request header specification
|
// request header specification
|
||||||
RequestHeadersSpec<?> headersSpecInserterStringWithHeaders = createDefaultPostResourceRequestResponse().header(
|
RequestHeadersSpec<?> headersSpecInserterStringWithHeaders = createDefaultPostResourceRequestResponse().header(
|
||||||
HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
|
HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
|
||||||
@ -210,7 +210,7 @@ public class WebClientIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDifferentResponseSpecifications_whenUsed_thenObtainExpectedResponse() {
|
void givenDifferentResponseSpecifications_whenUsed_thenObtainExpectedResponse() {
|
||||||
ResponseSpec responseSpecPostString = createDefaultPostResourceRequestResponse().retrieve();
|
ResponseSpec responseSpecPostString = createDefaultPostResourceRequestResponse().retrieve();
|
||||||
Mono<String> responsePostString = responseSpecPostString.bodyToMono(String.class);
|
Mono<String> responsePostString = responseSpecPostString.bodyToMono(String.class);
|
||||||
Mono<String> responsePostString2 = createDefaultPostResourceRequestResponse().exchangeToMono(response -> {
|
Mono<String> responsePostString2 = createDefaultPostResourceRequestResponse().exchangeToMono(response -> {
|
||||||
@ -255,7 +255,7 @@ public class WebClientIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenWebClientWithTimeoutConfigurations_whenRequestUsingWronglyConfiguredPublisher_thenObtainTimeout() {
|
void givenWebClientWithTimeoutConfigurations_whenRequestUsingWronglyConfiguredPublisher_thenObtainTimeout() {
|
||||||
HttpClient httpClient = HttpClient.create()
|
HttpClient httpClient = HttpClient.create()
|
||||||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000)
|
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000)
|
||||||
.responseTimeout(Duration.ofMillis(1000))
|
.responseTimeout(Duration.ofMillis(1000))
|
||||||
|
@ -13,7 +13,7 @@ import static org.springframework.test.annotation.DirtiesContext.ClassMode.BEFOR
|
|||||||
|
|
||||||
@DirtiesContext(classMode = BEFORE_CLASS)
|
@DirtiesContext(classMode = BEFORE_CLASS)
|
||||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = WebClientApplication.class)
|
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = WebClientApplication.class)
|
||||||
public class WebControllerIntegrationTest {
|
class WebControllerIntegrationTest {
|
||||||
|
|
||||||
@LocalServerPort
|
@LocalServerPort
|
||||||
private int randomServerPort;
|
private int randomServerPort;
|
||||||
|
@ -16,7 +16,7 @@ import org.springframework.web.server.WebHandler;
|
|||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@SpringBootTest(classes = WebClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
@SpringBootTest(classes = WebClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||||
public class WebTestClientIntegrationTest {
|
class WebTestClientIntegrationTest {
|
||||||
|
|
||||||
@LocalServerPort
|
@LocalServerPort
|
||||||
private int port;
|
private int port;
|
||||||
@ -28,7 +28,7 @@ public class WebTestClientIntegrationTest {
|
|||||||
private WebClientController controller;
|
private WebClientController controller;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenBindToWebHandler_thenRequestProcessed() {
|
void whenBindToWebHandler_thenRequestProcessed() {
|
||||||
WebHandler webHandler = exchange -> Mono.empty();
|
WebHandler webHandler = exchange -> Mono.empty();
|
||||||
|
|
||||||
WebTestClient.bindToWebHandler(webHandler)
|
WebTestClient.bindToWebHandler(webHandler)
|
||||||
@ -39,7 +39,7 @@ public class WebTestClientIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenBindToRouter_thenRequestProcessed() {
|
void whenBindToRouter_thenRequestProcessed() {
|
||||||
RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(
|
RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(
|
||||||
RequestPredicates.GET("/resource"),
|
RequestPredicates.GET("/resource"),
|
||||||
request -> ServerResponse.ok().build()
|
request -> ServerResponse.ok().build()
|
||||||
@ -55,7 +55,7 @@ public class WebTestClientIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@WithMockUser
|
@WithMockUser
|
||||||
public void whenBindToServer_thenRequestProcessed() {
|
void whenBindToServer_thenRequestProcessed() {
|
||||||
WebTestClient.bindToServer()
|
WebTestClient.bindToServer()
|
||||||
.baseUrl("http://localhost:" + port).build()
|
.baseUrl("http://localhost:" + port).build()
|
||||||
.get().uri("/resource")
|
.get().uri("/resource")
|
||||||
@ -66,7 +66,7 @@ public class WebTestClientIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@WithMockUser
|
@WithMockUser
|
||||||
public void whenBindToApplicationContext_thenRequestProcessed() {
|
void whenBindToApplicationContext_thenRequestProcessed() {
|
||||||
WebTestClient.bindToApplicationContext(context)
|
WebTestClient.bindToApplicationContext(context)
|
||||||
.build()
|
.build()
|
||||||
.get().uri("/resource")
|
.get().uri("/resource")
|
||||||
@ -76,7 +76,7 @@ public class WebTestClientIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenBindToController_thenRequestProcessed() {
|
void whenBindToController_thenRequestProcessed() {
|
||||||
WebTestClient.bindToController(controller)
|
WebTestClient.bindToController(controller)
|
||||||
.build()
|
.build()
|
||||||
.get().uri("/resource")
|
.get().uri("/resource")
|
||||||
|
@ -1,13 +1,11 @@
|
|||||||
package com.baeldung.reactive.webclientrequests;
|
package com.baeldung.reactive.webclientrequests;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.Captor;
|
import org.mockito.Captor;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
|
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
import org.springframework.web.reactive.function.client.ClientRequest;
|
import org.springframework.web.reactive.function.client.ClientRequest;
|
||||||
import org.springframework.web.reactive.function.client.ClientResponse;
|
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||||
import org.springframework.web.reactive.function.client.ExchangeFunction;
|
import org.springframework.web.reactive.function.client.ExchangeFunction;
|
||||||
@ -15,15 +13,14 @@ import org.springframework.web.reactive.function.client.WebClient;
|
|||||||
import org.springframework.web.util.DefaultUriBuilderFactory;
|
import org.springframework.web.util.DefaultUriBuilderFactory;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@WebFluxTest
|
@WebFluxTest
|
||||||
public class WebClientRequestsWithParametersUnitTest {
|
class WebClientRequestsWithParametersUnitTest {
|
||||||
|
|
||||||
private static final String BASE_URL = "https://example.com";
|
private static final String BASE_URL = "https://example.com";
|
||||||
|
|
||||||
@ -35,8 +32,8 @@ public class WebClientRequestsWithParametersUnitTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private ExchangeFunction exchangeFunction;
|
private ExchangeFunction exchangeFunction;
|
||||||
|
|
||||||
@Before
|
@BeforeEach
|
||||||
public void init() {
|
void init() {
|
||||||
ClientResponse mockResponse = mock(ClientResponse.class);
|
ClientResponse mockResponse = mock(ClientResponse.class);
|
||||||
when(mockResponse.bodyToMono(String.class)).thenReturn(Mono.just("test"));
|
when(mockResponse.bodyToMono(String.class)).thenReturn(Mono.just("test"));
|
||||||
when(exchangeFunction.exchange(argumentCaptor.capture())).thenReturn(Mono.just(mockResponse));
|
when(exchangeFunction.exchange(argumentCaptor.capture())).thenReturn(Mono.just(mockResponse));
|
||||||
@ -49,7 +46,7 @@ public class WebClientRequestsWithParametersUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCallSimpleURI_thenURIMatched() {
|
void whenCallSimpleURI_thenURIMatched() {
|
||||||
webClient.get()
|
webClient.get()
|
||||||
.uri("/products")
|
.uri("/products")
|
||||||
.retrieve()
|
.retrieve()
|
||||||
@ -60,7 +57,7 @@ public class WebClientRequestsWithParametersUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCallSinglePathSegmentUri_thenURIMatched() {
|
void whenCallSinglePathSegmentUri_thenURIMatched() {
|
||||||
webClient.get()
|
webClient.get()
|
||||||
.uri(uriBuilder -> uriBuilder
|
.uri(uriBuilder -> uriBuilder
|
||||||
.path("/products/{id}")
|
.path("/products/{id}")
|
||||||
@ -73,7 +70,7 @@ public class WebClientRequestsWithParametersUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCallMultiplePathSegmentsUri_thenURIMatched() {
|
void whenCallMultiplePathSegmentsUri_thenURIMatched() {
|
||||||
webClient.get()
|
webClient.get()
|
||||||
.uri(uriBuilder -> uriBuilder
|
.uri(uriBuilder -> uriBuilder
|
||||||
.path("/products/{id}/attributes/{attributeId}")
|
.path("/products/{id}/attributes/{attributeId}")
|
||||||
@ -86,7 +83,7 @@ public class WebClientRequestsWithParametersUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCallSingleQueryParams_thenURIMatched() {
|
void whenCallSingleQueryParams_thenURIMatched() {
|
||||||
webClient.get()
|
webClient.get()
|
||||||
.uri(uriBuilder -> uriBuilder
|
.uri(uriBuilder -> uriBuilder
|
||||||
.path("/products/")
|
.path("/products/")
|
||||||
@ -102,7 +99,7 @@ public class WebClientRequestsWithParametersUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCallSingleQueryParamsPlaceholders_thenURIMatched() {
|
void whenCallSingleQueryParamsPlaceholders_thenURIMatched() {
|
||||||
webClient.get()
|
webClient.get()
|
||||||
.uri(uriBuilder -> uriBuilder
|
.uri(uriBuilder -> uriBuilder
|
||||||
.path("/products/")
|
.path("/products/")
|
||||||
@ -118,7 +115,7 @@ public class WebClientRequestsWithParametersUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCallArrayQueryParamsBrackets_thenURIMatched() {
|
void whenCallArrayQueryParamsBrackets_thenURIMatched() {
|
||||||
webClient.get()
|
webClient.get()
|
||||||
.uri(uriBuilder -> uriBuilder
|
.uri(uriBuilder -> uriBuilder
|
||||||
.path("/products/")
|
.path("/products/")
|
||||||
@ -132,7 +129,7 @@ public class WebClientRequestsWithParametersUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCallArrayQueryParams_thenURIMatched() {
|
void whenCallArrayQueryParams_thenURIMatched() {
|
||||||
webClient.get()
|
webClient.get()
|
||||||
.uri(uriBuilder -> uriBuilder
|
.uri(uriBuilder -> uriBuilder
|
||||||
.path("/products/")
|
.path("/products/")
|
||||||
@ -146,7 +143,7 @@ public class WebClientRequestsWithParametersUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCallArrayQueryParamsComma_thenURIMatched() {
|
void whenCallArrayQueryParamsComma_thenURIMatched() {
|
||||||
webClient.get()
|
webClient.get()
|
||||||
.uri(uriBuilder -> uriBuilder
|
.uri(uriBuilder -> uriBuilder
|
||||||
.path("/products/")
|
.path("/products/")
|
||||||
@ -160,7 +157,7 @@ public class WebClientRequestsWithParametersUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUriComponentEncoding_thenQueryParamsNotEscaped() {
|
void whenUriComponentEncoding_thenQueryParamsNotEscaped() {
|
||||||
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(BASE_URL);
|
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(BASE_URL);
|
||||||
factory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.URI_COMPONENT);
|
factory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.URI_COMPONENT);
|
||||||
webClient = WebClient
|
webClient = WebClient
|
||||||
|
@ -2,13 +2,11 @@ package com.baeldung.reactive.webflux.annotation;
|
|||||||
|
|
||||||
import com.baeldung.reactive.webflux.Employee;
|
import com.baeldung.reactive.webflux.Employee;
|
||||||
import com.baeldung.reactive.webflux.EmployeeRepository;
|
import com.baeldung.reactive.webflux.EmployeeRepository;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.security.test.context.support.WithMockUser;
|
import org.springframework.security.test.context.support.WithMockUser;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
@ -21,9 +19,8 @@ import static org.mockito.Mockito.verify;
|
|||||||
import static org.mockito.Mockito.verifyNoInteractions;
|
import static org.mockito.Mockito.verifyNoInteractions;
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = EmployeeSpringApplication.class)
|
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = EmployeeSpringApplication.class)
|
||||||
public class EmployeeControllerIntegrationTest {
|
class EmployeeControllerIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebTestClient testClient;
|
private WebTestClient testClient;
|
||||||
@ -32,7 +29,7 @@ public class EmployeeControllerIntegrationTest {
|
|||||||
private EmployeeRepository employeeRepository;
|
private EmployeeRepository employeeRepository;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmployeeId_whenGetEmployeeById_thenCorrectEmployee() {
|
void givenEmployeeId_whenGetEmployeeById_thenCorrectEmployee() {
|
||||||
|
|
||||||
Employee employee = new Employee("1", "Employee 1 Name");
|
Employee employee = new Employee("1", "Employee 1 Name");
|
||||||
|
|
||||||
@ -46,7 +43,7 @@ public class EmployeeControllerIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenGetAllEmployees_thenCorrectEmployees() {
|
void whenGetAllEmployees_thenCorrectEmployees() {
|
||||||
List<Employee> employeeList = Arrays.asList(
|
List<Employee> employeeList = Arrays.asList(
|
||||||
new Employee("1", "Employee 1 Name"),
|
new Employee("1", "Employee 1 Name"),
|
||||||
new Employee("2", "Employee 2 Name"),
|
new Employee("2", "Employee 2 Name"),
|
||||||
@ -65,7 +62,7 @@ public class EmployeeControllerIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@WithMockUser(username = "admin", roles = { "ADMIN" })
|
@WithMockUser(username = "admin", roles = { "ADMIN" })
|
||||||
public void givenValidUser_whenUpdateEmployee_thenEmployeeUpdated() {
|
void givenValidUser_whenUpdateEmployee_thenEmployeeUpdated() {
|
||||||
Employee employee = new Employee("10", "Employee 10 Updated");
|
Employee employee = new Employee("10", "Employee 10 Updated");
|
||||||
|
|
||||||
given(employeeRepository.updateEmployee(employee)).willReturn(Mono.just(employee));
|
given(employeeRepository.updateEmployee(employee)).willReturn(Mono.just(employee));
|
||||||
@ -82,7 +79,7 @@ public class EmployeeControllerIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@WithMockUser
|
@WithMockUser
|
||||||
public void givenInvalidUser_whenUpdateEmployee_thenForbidden() {
|
void givenInvalidUser_whenUpdateEmployee_thenForbidden() {
|
||||||
Employee employee = new Employee("10", "Employee 10 Updated");
|
Employee employee = new Employee("10", "Employee 10 Updated");
|
||||||
|
|
||||||
testClient.post()
|
testClient.post()
|
||||||
|
@ -2,12 +2,10 @@ package com.baeldung.reactive.webflux.functional;
|
|||||||
|
|
||||||
import com.baeldung.reactive.webflux.Employee;
|
import com.baeldung.reactive.webflux.Employee;
|
||||||
import com.baeldung.reactive.webflux.EmployeeRepository;
|
import com.baeldung.reactive.webflux.EmployeeRepository;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
@ -19,9 +17,8 @@ import static org.mockito.BDDMockito.given;
|
|||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = EmployeeSpringFunctionalApplication.class)
|
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = EmployeeSpringFunctionalApplication.class)
|
||||||
public class EmployeeSpringFunctionalIntegrationTest {
|
class EmployeeSpringFunctionalIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private EmployeeFunctionalConfig config;
|
private EmployeeFunctionalConfig config;
|
||||||
@ -30,7 +27,7 @@ public class EmployeeSpringFunctionalIntegrationTest {
|
|||||||
private EmployeeRepository employeeRepository;
|
private EmployeeRepository employeeRepository;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmployeeId_whenGetEmployeeById_thenCorrectEmployee() {
|
void givenEmployeeId_whenGetEmployeeById_thenCorrectEmployee() {
|
||||||
WebTestClient client = WebTestClient.bindToRouterFunction(config.getEmployeeByIdRoute())
|
WebTestClient client = WebTestClient.bindToRouterFunction(config.getEmployeeByIdRoute())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
@ -48,7 +45,7 @@ public class EmployeeSpringFunctionalIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenGetAllEmployees_thenCorrectEmployees() {
|
void whenGetAllEmployees_thenCorrectEmployees() {
|
||||||
WebTestClient client = WebTestClient.bindToRouterFunction(config.getAllEmployeesRoute())
|
WebTestClient client = WebTestClient.bindToRouterFunction(config.getAllEmployeesRoute())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
@ -67,7 +64,7 @@ public class EmployeeSpringFunctionalIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUpdateEmployee_thenEmployeeUpdated() {
|
void whenUpdateEmployee_thenEmployeeUpdated() {
|
||||||
WebTestClient client = WebTestClient.bindToRouterFunction(config.updateEmployeeRoute())
|
WebTestClient client = WebTestClient.bindToRouterFunction(config.updateEmployeeRoute())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user