Skip to content

Commit

Permalink
Add tests for WebClient
Browse files Browse the repository at this point in the history
  • Loading branch information
izeye committed Sep 8, 2020
1 parent e0264cc commit e0dba09
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Expand Up @@ -36,6 +36,7 @@ repositories {

dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.springframework.boot:spring-boot-starter-actuator")

developmentOnly("org.springframework.boot:spring-boot-devtools")
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/izeye/throwaway/TestController.java
Expand Up @@ -2,11 +2,14 @@

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import lombok.extern.slf4j.Slf4j;

/**
* {@link Controller} for testing.
Expand All @@ -15,8 +18,11 @@
*/
@Controller
@RequestMapping(path = "/test")
@Slf4j
public class TestController {

private final AtomicBoolean first = new AtomicBoolean(true);

@GetMapping("/redirect/parameter")
public String redirectForTestingParameters() {
return "redirect:/httpServletRequest?name=테스트";
Expand Down Expand Up @@ -49,4 +55,15 @@ public String path(@PathVariable String value) {
return "value: " + value;
}

@GetMapping("/first_delay")
@ResponseBody
public String firstDelay() throws InterruptedException {
log.info("firstDelay() has been invoked.");

if (this.first.getAndSet(false)) {
TimeUnit.SECONDS.sleep(1);
}
return "Completed.";
}

}
52 changes: 52 additions & 0 deletions src/test/java/com/izeye/throwaway/TestControllerTests.java
@@ -0,0 +1,52 @@
package com.izeye.throwaway;

import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutException;
import io.netty.handler.timeout.ReadTimeoutHandler;
import org.junit.jupiter.api.Test;
import reactor.netty.http.client.HttpClient;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* Tests for {@link TestController}.
*
* @author Johnny Lim
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class TestControllerTests {

private final WebClient webClient;
private final int port;

TestControllerTests(@Autowired WebClient.Builder webClientBuilder, @LocalServerPort int port) {
HttpClient httpClient = HttpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 500)
.doOnConnected((connection) -> connection.addHandlerLast(
new ReadTimeoutHandler(500, TimeUnit.MILLISECONDS)));
this.webClient = webClientBuilder.clientConnector(new ReactorClientHttpConnector(httpClient)).build();
this.port = port;
}

@Test
void firstDelay() {
String uri = "http://localhost:" + this.port + "/test/first_delay";
assertThatThrownBy(() -> this.webClient.get().uri(uri).retrieve().bodyToMono(String.class).block())
.isInstanceOf(ReadTimeoutException.class);
}

@Test
void firstDelayWithRetry() {
String uri = "http://localhost:" + this.port + "/test/first_delay";
String response = this.webClient.get().uri(uri).retrieve().bodyToMono(String.class).retry().block();
assertThat(response).isEqualTo("Completed.");
}

}

0 comments on commit e0dba09

Please sign in to comment.