From cbc7980eba7d9d137a944088f921cc9b64f8f5dd Mon Sep 17 00:00:00 2001 From: Ralf Ueberfuhr Date: Thu, 20 Jun 2024 13:38:25 +0200 Subject: [PATCH] Replace @Nested test classes by separate outer classes --- .../CustomerApiGetCustomersTests.java | 52 +++++ .../CustomerApiGetSingleCustomerTests.java | 63 ++++++ .../CustomerApiPostCustomersTests.java | 90 +++++++++ .../quarkus/boundary/CustomerApiTests.java | 188 ------------------ 4 files changed, 205 insertions(+), 188 deletions(-) create mode 100644 customer-api-provider/src/test/java/de/schulung/sample/quarkus/boundary/CustomerApiGetCustomersTests.java create mode 100644 customer-api-provider/src/test/java/de/schulung/sample/quarkus/boundary/CustomerApiGetSingleCustomerTests.java create mode 100644 customer-api-provider/src/test/java/de/schulung/sample/quarkus/boundary/CustomerApiPostCustomersTests.java delete mode 100644 customer-api-provider/src/test/java/de/schulung/sample/quarkus/boundary/CustomerApiTests.java diff --git a/customer-api-provider/src/test/java/de/schulung/sample/quarkus/boundary/CustomerApiGetCustomersTests.java b/customer-api-provider/src/test/java/de/schulung/sample/quarkus/boundary/CustomerApiGetCustomersTests.java new file mode 100644 index 0000000..f1ed2b2 --- /dev/null +++ b/customer-api-provider/src/test/java/de/schulung/sample/quarkus/boundary/CustomerApiGetCustomersTests.java @@ -0,0 +1,52 @@ +package de.schulung.sample.quarkus.boundary; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.http.ContentType; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.startsWith; + +/* + * We test the app as a black box. + * (Integration Test) + */ +@QuarkusTest +@DisplayName("API-Tests for: GET /customers") +public class CustomerApiGetCustomersTests { + + /* + * GET /customers, Accept: application/json + * -> 200, Content-Type: application/json + */ + @Test + void shouldReturn200WhenGetCustomers() { + given() + .when() + .accept(ContentType.JSON) + .get("/customers") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body(startsWith("[")) + .body(endsWith("]")); + // we could also test for JSON schema: https://medium.com/@dhadiprasetyo/asserting-json-schema-for-api-testing-with-java-and-restassured-79b4a851f282 + } + + /* + * GET /customers, Accept: application/xml + * -> 406 + */ + @Test + void shouldReturn406WhenGetCustomersWithInvalidContentType() { + given() + .when() + .accept(ContentType.XML) + .get("/customers") + .then() + .statusCode(406); + } + +} diff --git a/customer-api-provider/src/test/java/de/schulung/sample/quarkus/boundary/CustomerApiGetSingleCustomerTests.java b/customer-api-provider/src/test/java/de/schulung/sample/quarkus/boundary/CustomerApiGetSingleCustomerTests.java new file mode 100644 index 0000000..31ce444 --- /dev/null +++ b/customer-api-provider/src/test/java/de/schulung/sample/quarkus/boundary/CustomerApiGetSingleCustomerTests.java @@ -0,0 +1,63 @@ +package de.schulung.sample.quarkus.boundary; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.http.ContentType; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; + +/* + * We test the app as a black box. + * (Integration Test) + */ +@QuarkusTest +@DisplayName("API-Tests for: GET /customers/{uuid}") +public class CustomerApiGetSingleCustomerTests { + + String customerLocation; + + @BeforeEach + // we need to create a customer to be sure that one exist + void setup() { + customerLocation = given() + .when() + .contentType(ContentType.JSON) + .body(""" + { + "name": "Tom", + "birth_date": "2000-10-04", + "state": "active" + } + """) + .accept(ContentType.JSON) + .post("/customers") + .then() + .statusCode(201) + .header("Location", is(notNullValue())) + .extract() + .header("Location"); + } + + private void ensureCustomerIsDeleted() { + given() + .when() + .delete(customerLocation) + .then() + .statusCode(204); + } + + @Test + void shouldReturn404WhenNotFound() { + ensureCustomerIsDeleted(); + given() + .when() + .accept(ContentType.JSON) + .get(customerLocation) + .then() + .statusCode(404); + } +} \ No newline at end of file diff --git a/customer-api-provider/src/test/java/de/schulung/sample/quarkus/boundary/CustomerApiPostCustomersTests.java b/customer-api-provider/src/test/java/de/schulung/sample/quarkus/boundary/CustomerApiPostCustomersTests.java new file mode 100644 index 0000000..6511b83 --- /dev/null +++ b/customer-api-provider/src/test/java/de/schulung/sample/quarkus/boundary/CustomerApiPostCustomersTests.java @@ -0,0 +1,90 @@ +package de.schulung.sample.quarkus.boundary; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.http.ContentType; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.*; + +/* + * We test the app as a black box. + * (Integration Test) + */ +@QuarkusTest +@DisplayName("API-Tests for: POST /customers") +public class CustomerApiPostCustomersTests { + + @Test + void shouldCreateCustomer() { + var location = given() + .when() + .contentType(ContentType.JSON) + .body(""" + { + "name": "Tom", + "birth_date": "2000-10-04", + "state": "active" + } + """) + .accept(ContentType.JSON) + .post("/customers") + .then() + .statusCode(201) + .header("Location", is(notNullValue())) + .header("Location", startsWith("http")) + .contentType(ContentType.JSON) + .body("name", is(equalTo("Tom"))) + .body("birth_date", is(equalTo("2000-10-04"))) + .body("uuid", is(notNullValue())) + .extract() + .header("Location"); + + given() + .when() + .accept(ContentType.JSON) + .get(location) + .then() + .statusCode(200) + .body("name", is(equalTo("Tom"))) + .body("birth_date", is(equalTo("2000-10-04"))); + } + + @Test + void shouldNotCreateCustomerWithInvalidName() { + given() + .when() + .contentType(ContentType.JSON) + .body(""" + { + "name": "T", + "birth_date": "2000-10-04", + "state": "active" + } + """) + .accept(ContentType.JSON) + .post("/customers") + .then() + .statusCode(400); + } + + @Test + void shouldNotCreateCustomerWithInvalidState() { + given() + .when() + .contentType(ContentType.JSON) + .body(""" + { + "name": "Tom Mayer", + "birth_date": "2000-10-04", + "state": "gelbekatze" + } + """) + .accept(ContentType.JSON) + .post("/customers") + .then() + .statusCode(400); + } + +} diff --git a/customer-api-provider/src/test/java/de/schulung/sample/quarkus/boundary/CustomerApiTests.java b/customer-api-provider/src/test/java/de/schulung/sample/quarkus/boundary/CustomerApiTests.java deleted file mode 100644 index 55889ce..0000000 --- a/customer-api-provider/src/test/java/de/schulung/sample/quarkus/boundary/CustomerApiTests.java +++ /dev/null @@ -1,188 +0,0 @@ -package de.schulung.sample.quarkus.boundary; - -import io.quarkus.test.junit.QuarkusTest; -import io.restassured.http.ContentType; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; - -import static io.restassured.RestAssured.given; -import static org.hamcrest.Matchers.*; - -/* - * We test the app as a black box. - * (Integration Test) - */ -@QuarkusTest -class CustomerApiTests { - - @Nested - @DisplayName("GET /customers") - class GetCustomersTests { - /* - * GET /customers, Accept: application/json - * -> 200, Content-Type: application/json - */ - @Test - void shouldReturn200WhenGetCustomers() { - given() - .when() - .accept(ContentType.JSON) - .get("/customers") - .then() - .statusCode(200) - .contentType(ContentType.JSON) - .body(startsWith("[")) - .body(endsWith("]")); - // we could also test for JSON schema: https://medium.com/@dhadiprasetyo/asserting-json-schema-for-api-testing-with-java-and-restassured-79b4a851f282 - } - - /* - * GET /customers, Accept: application/xml - * -> 406 - */ - @Test - void shouldReturn406WhenGetCustomersWithInvalidContentType() { - given() - .when() - .accept(ContentType.XML) - .get("/customers") - .then() - .statusCode(406); - } - } - - @Nested - @DisplayName("POST /customers") - class CreateCustomerTests { - - @Test - void shouldCreateCustomer() { - var location = given() - .when() - .contentType(ContentType.JSON) - .body(""" - { - "name": "Tom", - "birth_date": "2000-10-04", - "state": "active" - } - """) - .accept(ContentType.JSON) - .post("/customers") - .then() - .statusCode(201) - .header("Location", is(notNullValue())) - .header("Location", startsWith("http")) - .contentType(ContentType.JSON) - .body("name", is(equalTo("Tom"))) - .body("birth_date", is(equalTo("2000-10-04"))) - .body("uuid", is(notNullValue())) - .extract() - .header("Location"); - - given() - .when() - .accept(ContentType.JSON) - .get(location) - .then() - .statusCode(200) - .body("name", is(equalTo("Tom"))) - .body("birth_date", is(equalTo("2000-10-04"))); - } - - @Test - void shouldNotCreateCustomerWithInvalidName() { - given() - .when() - .contentType(ContentType.JSON) - .body(""" - { - "name": "T", - "birth_date": "2000-10-04", - "state": "active" - } - """) - .accept(ContentType.JSON) - .post("/customers") - .then() - .statusCode(400); - } - - @Test - void shouldNotCreateCustomerWithInvalidState() { - given() - .when() - .contentType(ContentType.JSON) - .body(""" - { - "name": "Tom Mayer", - "birth_date": "2000-10-04", - "state": "gelbekatze" - } - """) - .accept(ContentType.JSON) - .post("/customers") - .then() - .statusCode(400); - } - - } - - @Nested - @DisplayName("GET /customers/uuid") - class GetSingleCustomerTests { - - String customerLocation; - - @BeforeEach - // we need to create a customer to be sure that one exist - void setup() { - customerLocation = given() - .when() - .contentType(ContentType.JSON) - .body(""" - { - "name": "Tom", - "birth_date": "2000-10-04", - "state": "active" - } - """) - .accept(ContentType.JSON) - .post("/customers") - .then() - .statusCode(201) - .header("Location", is(notNullValue())) - .extract() - .header("Location"); - } - - @Nested - @DisplayName("with customer that does NOT exist") - class DeletedCustomerTests { - - @BeforeEach - void setup() { - given() - .when() - .delete(customerLocation) - .then() - .statusCode(204); - } - - @Test - void shouldReturn404WhenNotFound() { - given() - .when() - .accept(ContentType.JSON) - .get(customerLocation) - .then() - .statusCode(404); - } - - } - - } - -} \ No newline at end of file