Update UnitTests

djmil 2023-07-19 14:27:53 +02:00
parent d3f06ce991
commit fb97794e5c

@ -1,119 +1,115 @@
---
gitea: none # My first unit test
include_toc: true
--- Let's start with the simplest thing you can imagine: a single test method with a single statement. Create [src/test/java/example/cashcard/CashCardJsonTest.java](http://192.168.8.55:3000/HQLAx/FamilyCashCard/src/commit/5ff71154302523ab5ebd0a291e3f5819aed8fdb9/src/test/java/djmil/cashcard/CashCardJsonTest.java):
# My first unit test ``` java
package djmil.cashcard;
Let's start with the simplest thing you can imagine: a single test method with a single statement. Create [src/test/java/example/cashcard/CashCardJsonTest.java](http://192.168.8.55:3000/HQLAx/FamilyCashCard/src/commit/5ff71154302523ab5ebd0a291e3f5819aed8fdb9/src/test/java/djmil/cashcard/CashCardJsonTest.java):
import org.junit.jupiter.api.Test;
``` java import static org.assertj.core.api.Assertions.assertThat;
package djmil.cashcard;
public class CashCardJsonTest {
import org.junit.jupiter.api.Test; @Test
import static org.assertj.core.api.Assertions.assertThat; public void myFirstTest() {
assertThat(1).isEqualTo(42);
public class CashCardJsonTest { }
@Test }
public void myFirstTest() { ```
assertThat(1).isEqualTo(42);
} The `@Test` annotation is part of the JUnit library, and the `assertThat` method is part of the AssertJ library. Both of these libraries are imported after the package statement.
}
``` A common convention (but not a requirement) is to always use the Test suffix for test classes. Weve done that here. The full class name CashCardJsonTest.java gives you a clue about the nature of the test we're about to write.
The `@Test` annotation is part of the JUnit library, and the `assertThat` method is part of the AssertJ library. Both of these libraries are imported after the package statement. In true Test-First fashion, we've written a failing test first. It's important to have a failing test first so you can have high confidence that whatever you did to fix the test actually worked.
A common convention (but not a requirement) is to always use the Test suffix for test classes. Weve done that here. The full class name CashCardJsonTest.java gives you a clue about the nature of the test we're about to write. Toggle terminal with `ctrl+tilda` and type
In true Test-First fashion, we've written a failing test first. It's important to have a failing test first so you can have high confidence that whatever you did to fix the test actually worked. ```bash
./gradlew test
Toggle terminal with `ctrl+tilda` and type ```
```bash # Testing the CashCard Data Contract
./gradlew test
``` ```java
import org.springframework.boot.test.json.JacksonTester;
# Testing the CashCard Data Contract import org.springframework.beans.factory.annotation.Autowired;
```java @JsonTest
import org.springframework.boot.test.json.JacksonTester; public class CashCardJsonTest {
import org.springframework.beans.factory.annotation.Autowired; @Autowired
private JacksonTester<CashCard> json;
@JsonTest ```
public class CashCardJsonTest {
@Autowired Marking CashCardJsonTest with `@JsonTest` annotation makes it a test class which uses the Jackson framework (which is included as part of Spring). This provides extensive JSON testing and parsing support. It also establishes all the related behavior to test JSON objects.
private JacksonTester<CashCard> json;
``` ### @Autowired
Marking CashCardJsonTest with `@JsonTest` annotation makes it a test class which uses the Jackson framework (which is included as part of Spring). This provides extensive JSON testing and parsing support. It also establishes all the related behavior to test JSON objects. `@Autowired` is an annotation that directs Spring to create an object of the requested type. `JacksonTester` is a convenience wrapper to the Jackson JSON parsing library. It handles serialization and deserialization of JSON objects.
### @Autowired To create a CashCard class and the constructor thats used in the `cashCardSerializationTest()` test, create the file `src/main/java/djmil/cashcard/CashCard.java` with the following contents (notice that this file is under in the `src/main` directory, not the `src/test` directory):
`@Autowired` is an annotation that directs Spring to create an object of the requested type. `JacksonTester` is a convenience wrapper to the Jackson JSON parsing library. It handles serialization and deserialization of JSON objects. ```java
package djmil.cashcard;
To create a CashCard class and the constructor thats used in the `cashCardSerializationTest()` test, create the file `src/main/java/djmil/cashcard/CashCard.java` with the following contents (notice that this file is under in the `src/main` directory, not the `src/test` directory):
public record CashCard(Long id, Double amount) {
```java }
package djmil.cashcard; ```
public record CashCard(Long id, Double amount) { ### The contract file
}
``` `src/test/resources/djmil/cashcard/expected.json`
### The contract file ```json
{
`src/test/resources/djmil/cashcard/expected.json` "id": 99,
"amount": 123.45
```json }
{ ```
"id": 99,
"amount": 123.45 **NOTE** Resources
} Pay attention to the path `djmil/cashcard/` is essentially a *package name*. It is shared between different aspects of the project:
``` - src/main/java - code
- src/tests/java - tests
**NOTE** Resources - src/tests/resources - static resources for testing.
Pay attention to the path `djmil/cashcard/` is essentially a *package name*. It is shared between different aspects of the project: Essentially `gradle` is responsible to map different parts of source code onto final package to be accessible for java via *classpath*.
- src/main/java - code
- src/tests/java - tests ### The test
- src/tests/resources - static resources for testing.
Essentially `gradle` is responsible to map different parts of source code onto final package to be accessible for java via *classpath*. ```java
@Test
### The test public void cashCardSerializationTest() throws IOException {
CashCard cashCard = new CashCard(99L, 123.45);
```java
@Test assertThat(json.write(cashCard)).isStrictlyEqualToJson("expected.json");
public void cashCardSerializationTest() throws IOException {
CashCard cashCard = new CashCard(99L, 123.45); assertThat(json.write(cashCard)).hasJsonPathNumberValue("@.id");
assertThat(json.write(cashCard)).extractingJsonPathNumberValue("@.id")
assertThat(json.write(cashCard)).isStrictlyEqualToJson("expected.json"); .isEqualTo(99);
assertThat(json.write(cashCard)).hasJsonPathNumberValue("@.id"); assertThat(json.write(cashCard)).hasJsonPathNumberValue("@.amount")
assertThat(json.write(cashCard)).extractingJsonPathNumberValue("@.id") assertThat(json.write(cashCard)).extractingJsonPathNumberValue("@.amount")
.isEqualTo(99); .isEqualTo(123.45);
}
assertThat(json.write(cashCard)).hasJsonPathNumberValue("@.amount") ```
assertThat(json.write(cashCard)).extractingJsonPathNumberValue("@.amount")
.isEqualTo(123.45); `.isStrictlyEqualToJson("expected.json");` will try to load static file from `FamilyCashCard/build/resources/test/djmil/cashcard` directory.
}
``` # Testing Deserialization
`.isStrictlyEqualToJson("expected.json");` will try to load static file from `FamilyCashCard/build/resources/test/djmil/cashcard` directory. ```java
@Test
# Testing Deserialization public void cashCardDeserializationTest() throws IOException {
String expected = """
```java {
@Test "id":1000,
public void cashCardDeserializationTest() throws IOException { "amount":67.89
String expected = """ }
{ """;
"id":1000,
"amount":67.89 assertThat(json.parse(expected)).isEqualTo(new CashCard(1000L, 67.89));
} assertThat(json.parseObject(expected).id()).isEqualTo(1000);
"""; assertThat(json.parseObject(expected).amount()).isEqualTo(67.89);
}
assertThat(json.parse(expected)).isEqualTo(new CashCard(1000L, 67.89)); ```
assertThat(json.parseObject(expected).id()).isEqualTo(1000);
assertThat(json.parseObject(expected).amount()).isEqualTo(67.89);
}
```