From 64707ff810cca0b92734c83a3de33123e9836126 Mon Sep 17 00:00:00 2001 From: djmil Date: Tue, 18 Jul 2023 19:23:49 +0200 Subject: [PATCH] TDD for CashCard contract --- src/main/java/djmil/cashcard/CashCard.java | 5 +++ .../java/djmil/cashcard/CashCardJsonTest.java | 43 ++++++++++++++++++- .../resources/djmil/cashcard/expected.json | 4 ++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 src/main/java/djmil/cashcard/CashCard.java create mode 100644 src/test/resources/djmil/cashcard/expected.json diff --git a/src/main/java/djmil/cashcard/CashCard.java b/src/main/java/djmil/cashcard/CashCard.java new file mode 100644 index 0000000..4bc5397 --- /dev/null +++ b/src/main/java/djmil/cashcard/CashCard.java @@ -0,0 +1,5 @@ +package djmil.cashcard; + +public record CashCard(Long id, Double amount) { + +} diff --git a/src/test/java/djmil/cashcard/CashCardJsonTest.java b/src/test/java/djmil/cashcard/CashCardJsonTest.java index bd2a2df..df620f2 100644 --- a/src/test/java/djmil/cashcard/CashCardJsonTest.java +++ b/src/test/java/djmil/cashcard/CashCardJsonTest.java @@ -1,13 +1,52 @@ package djmil.cashcard; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.json.JsonTest; +import org.springframework.boot.test.json.JacksonTester; + +import java.io.IOException; import static org.assertj.core.api.Assertions.assertThat; +@JsonTest public class CashCardJsonTest { -@Test + @Autowired + private JacksonTester json; + + @Test public void myFirstTest() { - assertThat(1).isEqualTo(42); + assertThat(1).isEqualTo(1); + } + + @Test + public void cashCardSerializationTest() throws IOException { + CashCard cashCard = new CashCard(99L, 123.45); + + assertThat(json.write(cashCard)).isStrictlyEqualToJson("expected.json"); + + assertThat(json.write(cashCard)).hasJsonPathNumberValue("@.id"); + assertThat(json.write(cashCard)).extractingJsonPathNumberValue("@.id") + .isEqualTo(99); + + assertThat(json.write(cashCard)).hasJsonPathNumberValue("@.amount"); + assertThat(json.write(cashCard)).extractingJsonPathNumberValue("@.amount") + .isEqualTo(123.45); + } + + @Test + public void cashCardDeserializationTest() throws IOException { + 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); } } diff --git a/src/test/resources/djmil/cashcard/expected.json b/src/test/resources/djmil/cashcard/expected.json new file mode 100644 index 0000000..82eacd0 --- /dev/null +++ b/src/test/resources/djmil/cashcard/expected.json @@ -0,0 +1,4 @@ +{ + "id": 99, + "amount": 123.45 +} \ No newline at end of file