Skip to content

Commit

Permalink
chore: cleanup of test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Oct 10, 2024
1 parent a90595e commit 67d6784
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 132 deletions.
29 changes: 1 addition & 28 deletions wiremock-spring-boot-example/src/main/java/app/App.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,12 @@
package app;

import app.todoclient.TodoClient;
import app.userclient.UserClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

@SpringBootApplication
public class App {

public static void main(String[] args) {
public static void main(final String[] args) {
SpringApplication.run(App.class, args);
}

@Bean
UserClient userClient(WebClient.Builder builder, @Value("${user-client.url}") String url) {
WebClient webClient = builder.baseUrl(url).build();

HttpServiceProxyFactory httpServiceProxyFactory =
HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient)).build();
return httpServiceProxyFactory.createClient(UserClient.class);
}

@Bean
TodoClient todoClient(WebClient.Builder builder, @Value("${todo-client.url}") String url)
throws InterruptedException {
Thread.sleep(1000);
WebClient webClient = builder.baseUrl(url).build();

HttpServiceProxyFactory httpServiceProxyFactory =
HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient)).build();
return httpServiceProxyFactory.createClient(TodoClient.class);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
user-client.url=https://jsonplaceholder.typicode.com/users
todo-client.url=https://jsonplaceholder.typicode.com/todos

#logging.level.org.springframework.test.context.cache=DEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.wiremock.spring.EnableWireMock;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@SpringBootTest
@EnableWireMock
class DefaultInstanceTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import org.wiremock.spring.EnableWireMock;
import org.wiremock.spring.InjectWireMock;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@SpringBootTest
@EnableWireMock
class InjectNamedWireMockTest {
class InjectDefaultWireMockTest {

@InjectWireMock private WireMockServer wireMockServer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import org.wiremock.spring.EnableWireMock;
import org.wiremock.spring.InjectWireMock;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@SpringBootTest
@EnableWireMock(@ConfigureWireMock(name = "mywiremock"))
class InjectDefaultWireMockTest {
class InjectNamedWireMockTest {

@InjectWireMock("mywiremock")
private WireMockServer wireMockServer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static org.assertj.core.api.Assertions.assertThat;

import app.todoclient.TodoClient;
import com.github.tomakehurst.wiremock.client.WireMock;
import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.wiremock.spring.ConfigureWireMock;
import org.wiremock.spring.EnableWireMock;
Expand All @@ -21,7 +21,8 @@
})
class JavaStubbingTest {

@Autowired private TodoClient todoClient;
@Value("${todo-client.url}")
private String todoUrl;

@Test
void usesJavaStubbing() {
Expand All @@ -41,6 +42,23 @@ void usesJavaStubbing() {
{ "id": 2, "userId": 1, "title": "my todo2" }
]
""")));
assertThat(this.todoClient.findAll()).isNotNull().hasSize(2);

assertThat(
RestAssured.when().get(this.todoUrl).then().statusCode(200).extract().asPrettyString())
.isEqualToIgnoringWhitespace(
"""
[
{
"id": 1,
"userId": 1,
"title": "my todo"
},
{
"id": 2,
"userId": 1,
"title": "my todo2"
}
]
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static org.assertj.core.api.Assertions.assertThat;

import app.controller.TodoDTO;
import com.github.tomakehurst.wiremock.WireMockServer;
import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.wiremock.spring.ConfigureWireMock;
import org.wiremock.spring.EnableWireMock;
import org.wiremock.spring.InjectWireMock;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@SpringBootTest
@EnableWireMock({
@ConfigureWireMock(name = "user-client", baseUrlProperties = "user-client.url"),
@ConfigureWireMock(name = "todo-service", baseUrlProperties = "todo-client.url")
Expand All @@ -29,8 +25,6 @@ class MultipleWireMocksTest {
@InjectWireMock("user-client")
private WireMockServer userService;

@Autowired private TestRestTemplate restTemplate;

@Test
void returnsTodos() {
this.todoService.stubFor(
Expand Down Expand Up @@ -64,21 +58,57 @@ void returnsTodos() {
{ "id": 2, "name": "John" }
""")));

final ResponseEntity<TodoDTO[]> response = this.restTemplate.getForEntity("/", TodoDTO[].class);
assertThat(
RestAssured.when()
.get("http://localhost:" + this.todoService.port() + "/")
.then()
.statusCode(200)
.extract()
.asPrettyString())
.isEqualToIgnoringWhitespace(
"""
[
{
"id": 1,
"userId": 1,
"title": "my todo"
},
{
"id": 2,
"userId": 2,
"title": "my todo2"
}
]
""");

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody()).hasSize(2);
assertThat(response.getBody())
.hasSize(2)
.satisfies(
todos -> {
assertThat(todos[0].id()).isEqualTo(1);
assertThat(todos[0].title()).isEqualTo("my todo");
assertThat(todos[0].userName()).isEqualTo("Amy");
assertThat(
RestAssured.when()
.get("http://localhost:" + this.userService.port() + "/1")
.then()
.statusCode(200)
.extract()
.asPrettyString())
.isEqualToIgnoringWhitespace(
"""
{
"id": 1,
"name": "Amy"
}
""");

assertThat(todos[1].id()).isEqualTo(2);
assertThat(todos[1].title()).isEqualTo("my todo2");
assertThat(todos[1].userName()).isEqualTo("John");
});
assertThat(
RestAssured.when()
.get("http://localhost:" + this.userService.port() + "/2")
.then()
.statusCode(200)
.extract()
.asPrettyString())
.isEqualToIgnoringWhitespace(
"""
{
"id": 2,
"name": "John"
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static org.assertj.core.api.Assertions.assertThat;

import app.userclient.User;
import app.userclient.UserClient;
import com.github.tomakehurst.wiremock.WireMockServer;
import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.wiremock.spring.ConfigureWireMock;
import org.wiremock.spring.EnableWireMock;
Expand All @@ -18,12 +16,10 @@
@EnableWireMock({
@ConfigureWireMock(
name = "user-client",
filesUnderClasspath = {"wiremock"},
filesUnderClasspath = {"wiremock/user-client"},
baseUrlProperties = "user-client.url")
})
class SingleWireMockTest {

@Autowired private UserClient userClient;
class SingleNamedWireMockTest {

@InjectWireMock("user-client")
private WireMockServer wiremock;
Expand All @@ -38,17 +34,32 @@ void usesJavaStubbing() {
.withBody("""
{ "id": 2, "name": "Amy" }
""")));
final User user = this.userClient.findOne(2L);
assertThat(user).isNotNull();
assertThat(user.id()).isEqualTo(2L);
assertThat(user.name()).isEqualTo("Amy");

RestAssured.baseURI = "http://localhost:" + this.wiremock.port();
final String actual =
RestAssured.when().get("/2").then().statusCode(200).extract().asPrettyString();
assertThat(actual)
.isEqualToIgnoringWhitespace(
"""
{
"id": 2,
"name": "Amy"
}
""");
}

@Test
void usesStubFiles() {
final User user = this.userClient.findOne(1L);
assertThat(user).isNotNull();
assertThat(user.id()).isEqualTo(1L);
assertThat(user.name()).isEqualTo("Jenna");
RestAssured.baseURI = "http://localhost:" + this.wiremock.port();
final String actual =
RestAssured.when().get("/1").then().statusCode(200).extract().asPrettyString();
assertThat(actual)
.isEqualToIgnoringWhitespace(
"""
{
"name": "Jenna",
"id": 1
}
""");
}
}

0 comments on commit 67d6784

Please sign in to comment.