TDD for CashCard contract

This commit is contained in:
djmil 2023-07-18 19:23:49 +02:00
parent 5ff7115430
commit 64707ff810
3 changed files with 50 additions and 2 deletions

View File

@ -0,0 +1,5 @@
package djmil.cashcard;
public record CashCard(Long id, Double amount) {
}

View File

@ -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<CashCard> 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);
}
}

View File

@ -0,0 +1,4 @@
{
"id": 99,
"amount": 123.45
}