TDD for CashCard contract
This commit is contained in:
parent
5ff7115430
commit
64707ff810
5
src/main/java/djmil/cashcard/CashCard.java
Normal file
5
src/main/java/djmil/cashcard/CashCard.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package djmil.cashcard;
|
||||||
|
|
||||||
|
public record CashCard(Long id, Double amount) {
|
||||||
|
|
||||||
|
}
|
@ -1,13 +1,52 @@
|
|||||||
package djmil.cashcard;
|
package djmil.cashcard;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
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;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@JsonTest
|
||||||
public class CashCardJsonTest {
|
public class CashCardJsonTest {
|
||||||
|
|
||||||
@Test
|
@Autowired
|
||||||
|
private JacksonTester<CashCard> json;
|
||||||
|
|
||||||
|
@Test
|
||||||
public void myFirstTest() {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
4
src/test/resources/djmil/cashcard/expected.json
Normal file
4
src/test/resources/djmil/cashcard/expected.json
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"id": 99,
|
||||||
|
"amount": 123.45
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user