Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Inspiration #263

Open
FanJups opened this issue Dec 6, 2023 · 0 comments
Open

Test Inspiration #263

FanJups opened this issue Dec 6, 2023 · 0 comments
Labels
enhancement New feature or request Experiment There is no urgency, it's all about things to try if possible then see the outcome good first issue Good for newcomers information good to know jsgenerator Related to the whole Project jsgenerator-test test module todo Ticket that has to be done

Comments

@FanJups
Copy link
Member

FanJups commented Dec 6, 2023

This class from Spring Academy course Lab: Returning a list with GET contains librairies that may be interesting to refactor tests

package example.cashcard;


import org.assertj.core.util.Arrays;
import org.junit.jupiter.api.BeforeEach;
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
class CashCardJsonTest {

    @Autowired
    private JacksonTester<CashCard> json;

    @Autowired
    private JacksonTester<CashCard[]> jsonList;

    private CashCard[] cashCards;

    @BeforeEach
    void setUp() {
        cashCards = Arrays.array(
                new CashCard(99L, 123.45),
                new CashCard(100L, 100.00),
                new CashCard(101L, 150.00));
    }

    @Test
    void cashCardSerializationTest() throws IOException {
        CashCard cashCard = cashCards[0];
        assertThat(json.write(cashCard)).isStrictlyEqualToJson("single.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
    void cashCardDeserializationTest() throws IOException {
        String expected = """
                {
                    "id": 99,
                    "amount": 123.45
                }
                """;
        assertThat(json.parse(expected))
                .isEqualTo(new CashCard(99L, 123.45));
        assertThat(json.parseObject(expected).id()).isEqualTo(99);
        assertThat(json.parseObject(expected).amount()).isEqualTo(123.45);
    }
}
@FanJups FanJups added jsgenerator-test test module jsgenerator Related to the whole Project enhancement New feature or request Experiment There is no urgency, it's all about things to try if possible then see the outcome todo Ticket that has to be done information good to know labels Dec 6, 2023
@FanJups FanJups added the good first issue Good for newcomers label Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Experiment There is no urgency, it's all about things to try if possible then see the outcome good first issue Good for newcomers information good to know jsgenerator Related to the whole Project jsgenerator-test test module todo Ticket that has to be done
Projects
None yet
Development

No branches or pull requests

1 participant