Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #16 from ralf-ueberfuhr-ars/feature/test-fix
Browse files Browse the repository at this point in the history
Replace @nested test classes by separate outer classes
  • Loading branch information
ralf-ueberfuhr-ars authored Jun 20, 2024
2 parents 76ddc4d + cbc7980 commit c112f4b
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 188 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}

This file was deleted.

0 comments on commit c112f4b

Please sign in to comment.