Replace RestAssured.when with Spring WebTestClient

Fixes gh-221
This commit is contained in:
Georgios Andrianakis
2018-09-25 11:13:28 +03:00
committed by Spencer Gibb
parent cdd3a62ad8
commit 8be84431bc
8 changed files with 73 additions and 116 deletions

View File

@@ -71,7 +71,6 @@
<fabric8.maven.plugin.version>3.5.37</fabric8.maven.plugin.version>
<gmavenplus-plugin.version>1.6</gmavenplus-plugin.version>
<groovy.version>2.4.12</groovy.version>
<restassured.version>3.0.2</restassured.version>
<spock-spring.version>1.1-groovy-2.4</spock-spring.version>
</properties>
@@ -123,13 +122,6 @@
<version>${groovy.version}</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>${restassured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-spring</artifactId>

View File

@@ -162,8 +162,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<scope>test</scope>
</dependency>
<dependency>

View File

@@ -17,28 +17,23 @@
package org.springframework.cloud.kubernetes.config;
import java.util.HashMap;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import io.restassured.RestAssured;
import org.junit.Before;
import java.util.HashMap;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.config.example.App;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import static io.restassured.RestAssured.when;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
/**
@@ -48,6 +43,7 @@ import static org.junit.Assert.assertEquals;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, properties = {
"spring.application.name=configmap-example",
"spring.cloud.kubernetes.reload.enabled=false" })
@AutoConfigureWebTestClient
public class ConfigMapsSpringBootTest {
@ClassRule
@@ -56,12 +52,12 @@ public class ConfigMapsSpringBootTest {
private static KubernetesClient mockClient;
@Autowired(required = false)
Config config;
private Config config;
private static final String APPLICATION_NAME = "configmap-example";
@Value("${local.server.port}")
private int port;
@Autowired
private WebTestClient webClient;
@BeforeClass
public static void setUpBeforeClass() {
@@ -84,23 +80,12 @@ public class ConfigMapsSpringBootTest {
.always();
}
@Before
public void setUp() {
RestAssured.baseURI = String.format("http://localhost:%d/api/greeting", port);
}
@Test
public void testConfig() {
assertEquals(config.getMasterUrl(), mockClient.getConfiguration().getMasterUrl());
assertEquals(config.getNamespace(), mockClient.getNamespace());
}
@Test
public void testGreetingEndpoint() {
when().get().then().statusCode(200).body("content",
is("Hello ConfigMap, World!"));
}
@Test
public void testConfigMap() {
ConfigMap configmap = mockClient.configMaps().inNamespace("test")
@@ -109,4 +94,10 @@ public class ConfigMapsSpringBootTest {
assertEquals(keys.get("bean.greeting"), "Hello ConfigMap, %s!");
}
@Test
public void testGreetingEndpoint() {
this.webClient.get().uri("/api/greeting").exchange().expectStatus().isOk()
.expectBody().jsonPath("content").isEqualTo("Hello ConfigMap, World!");
}
}

View File

@@ -17,26 +17,22 @@
package org.springframework.cloud.kubernetes.config;
import java.util.HashMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import io.restassured.RestAssured;
import java.util.HashMap;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.config.example.App;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import static io.restassured.RestAssured.when;
import static org.hamcrest.core.Is.is;
import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.readResourceFile;
/**
@@ -46,6 +42,7 @@ import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.read
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, properties = {
"spring.application.name=configmap-with-profile-no-active-profiles-example",
"spring.cloud.kubernetes.reload.enabled=false" })
@AutoConfigureWebTestClient
public class ConfigMapsWithProfilesNoActiveProfileSpringBootTest {
@ClassRule
@@ -53,13 +50,10 @@ public class ConfigMapsWithProfilesNoActiveProfileSpringBootTest {
private static KubernetesClient mockClient;
@Autowired(required = false)
Config config;
private static final String APPLICATION_NAME = "configmap-with-profile-no-active-profiles-example";
@Value("${local.server.port}")
private int port;
@Autowired
private WebTestClient webClient;
@BeforeClass
public static void setUpBeforeClass() {
@@ -84,16 +78,15 @@ public class ConfigMapsWithProfilesNoActiveProfileSpringBootTest {
@Test
public void testGreetingEndpoint() {
RestAssured.baseURI = String.format("http://localhost:%d/api/greeting", port);
when().get().then().statusCode(200).body("content",
is("Hello ConfigMap default, World!"));
this.webClient.get().uri("/api/greeting").exchange().expectStatus().isOk()
.expectBody().jsonPath("content").isEqualTo("Hello ConfigMap default, World!");
}
@Test
public void testFarewellEndpoint() {
RestAssured.baseURI = String.format("http://localhost:%d/api/farewell", port);
when().get().then().statusCode(200).body("content",
is("Goodbye ConfigMap default, World!"));
this.webClient.get().uri("/api/farewell").exchange().expectStatus().isOk()
.expectBody().jsonPath("content")
.isEqualTo("Goodbye ConfigMap default, World!");
}
}

View File

@@ -17,27 +17,23 @@
package org.springframework.cloud.kubernetes.config;
import java.util.HashMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import io.restassured.RestAssured;
import java.util.HashMap;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.config.example.App;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import static io.restassured.RestAssured.when;
import static org.hamcrest.core.Is.is;
import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.readResourceFile;
/**
@@ -48,6 +44,7 @@ import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.read
"spring.application.name=configmap-with-profile-example",
"spring.cloud.kubernetes.reload.enabled=false" })
@ActiveProfiles("development")
@AutoConfigureWebTestClient
public class ConfigMapsWithProfilesSpringBootTest {
@ClassRule
@@ -60,8 +57,8 @@ public class ConfigMapsWithProfilesSpringBootTest {
private static final String APPLICATION_NAME = "configmap-with-profile-example";
@Value("${local.server.port}")
private int port;
@Autowired
private WebTestClient webClient;
@BeforeClass
public static void setUpBeforeClass() {
@@ -86,15 +83,15 @@ public class ConfigMapsWithProfilesSpringBootTest {
@Test
public void testGreetingEndpoint() {
RestAssured.baseURI = String.format("http://localhost:%d/api/greeting", port);
when().get().then().statusCode(200).body("content",
is("Hello ConfigMap dev, World!"));
this.webClient.get().uri("/api/greeting").exchange().expectStatus().isOk()
.expectBody().jsonPath("content")
.isEqualTo("Hello ConfigMap dev, World!");
}
@Test
public void testFarewellEndpoint() {
RestAssured.baseURI = String.format("http://localhost:%d/api/farewell", port);
when().get().then().statusCode(200).body("content",
is("Goodbye ConfigMap default, World!"));
this.webClient.get().uri("/api/farewell").exchange().expectStatus().isOk()
.expectBody().jsonPath("content")
.isEqualTo("Goodbye ConfigMap default, World!");
}
}

View File

@@ -17,26 +17,24 @@
package org.springframework.cloud.kubernetes.config;
import static io.restassured.RestAssured.when;
import static org.hamcrest.core.Is.is;
import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.readResourceFile;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import io.restassured.RestAssured;
import java.util.HashMap;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.config.example.App;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.readResourceFile;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
@@ -45,6 +43,7 @@ import org.springframework.test.context.junit4.SpringRunner;
"spring.cloud.kubernetes.reload.enabled=false"}
)
@ActiveProfiles("development")
@AutoConfigureWebTestClient
public class ConfigMapsWithoutProfilesSpringBootTest {
@ClassRule
@@ -52,13 +51,10 @@ public class ConfigMapsWithoutProfilesSpringBootTest {
private static KubernetesClient mockClient;
@Autowired(required = false)
Config config;
private static final String APPLICATION_NAME = "configmap-without-profile-example";
@Value("${local.server.port}")
private int port;
@Autowired
private WebTestClient webClient;
@BeforeClass
public static void setUpBeforeClass() {
@@ -82,19 +78,14 @@ public class ConfigMapsWithoutProfilesSpringBootTest {
@Test
public void testGreetingEndpoint() {
RestAssured.baseURI = String.format("http://localhost:%d/api/greeting", port);
when().get()
.then()
.statusCode(200)
.body("content", is("Hello ConfigMap, World!"));
this.webClient.get().uri("/api/greeting").exchange().expectStatus().isOk()
.expectBody().jsonPath("content").isEqualTo("Hello ConfigMap, World!");
}
@Test
public void testFarewellEndpoint() {
RestAssured.baseURI = String.format("http://localhost:%d/api/farewell", port);
when().get()
.then()
.statusCode(200)
.body("content", is("Goodbye ConfigMap, World!"));
this.webClient.get().uri("/api/farewell").exchange().expectStatus().isOk()
.expectBody().jsonPath("content")
.isEqualTo("Goodbye ConfigMap, World!");
}
}

View File

@@ -1,20 +1,22 @@
package org.springframework.cloud.kubernetes.config;
import static io.restassured.RestAssured.given;
import static org.hamcrest.core.StringContains.containsString;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.server.mock.KubernetesServer;
import io.restassured.RestAssured;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.config.example.App;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import static org.hamcrest.Matchers.containsString;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class,
@@ -31,6 +33,8 @@ public class HealthIndicatorTest {
@Value("${local.server.port}")
private int port;
@Autowired
private WebTestClient webClient;
@BeforeClass
public static void setUpBeforeClass() {
@@ -46,13 +50,12 @@ public class HealthIndicatorTest {
@Test
public void healthEndpointShouldContainKubernetes() {
RestAssured.baseURI = String.format("http://localhost:%d/actuator/health", port);
given()
.contentType("application/json")
.get()
.then()
.statusCode(200)
.body(containsString("kubernetes"));
webClient.get()
.uri("http://localhost:{port}/actuator/health", port)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk()
.expectBody(String.class).value(containsString("kubernetes"));
}

View File

@@ -17,25 +17,22 @@
package org.springframework.cloud.kubernetes.config;
import static io.restassured.RestAssured.when;
import static org.hamcrest.core.Is.is;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import io.restassured.RestAssured;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.config.example2.ExampleApp;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
/**
* @author Charles Moulliard
@@ -43,6 +40,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = ExampleApp.class,
properties = {"spring.cloud.bootstrap.name=multiplecms"})
@AutoConfigureWebTestClient
public class MultipleConfigMapsSpringBootTest {
@ClassRule
@@ -50,9 +48,8 @@ public class MultipleConfigMapsSpringBootTest {
private static KubernetesClient mockClient;
@Value("${local.server.port}")
private int port;
@Autowired
private WebTestClient webClient;
@BeforeClass
public static void setUpBeforeClass() {
@@ -109,12 +106,6 @@ public class MultipleConfigMapsSpringBootTest {
.always();
}
@Before
public void setUp() {
RestAssured.baseURI = String.format("http://localhost:%d/", port);
}
//the last confimap defined in 'multiplecms.yml' has the highest priority, so
//the common property defined in all configmaps is taken from the last one defined
@Test
@@ -138,10 +129,9 @@ public class MultipleConfigMapsSpringBootTest {
}
private void assertResponse(String path, String expectedMessage) {
when().get(path)
.then()
.statusCode(200)
.body("message", is(expectedMessage));
this.webClient.get().uri(path).exchange().expectStatus().isOk()
.expectBody().jsonPath("message")
.isEqualTo(expectedMessage);
}
}