Test API often used in AssertJ

Fraction

――Assertion of JUnit5 is excellent, but I dare to AssertJ ――Write only what you usually use --Since Map is not used, there is no verification. --Do not verify only String or int

Operating environment

--Java8 (Forgive me because it's my work environment ...)

The source is here

Original class system

Recursively validate fields of objects

ʻUsing RecursiveComparison` prevents instance comparisons in nested class validation

@Test
Recursively validate fields of void objects() {
    String studentIdentifier = generateUUID();
    String tutorIdentifier = generateUUID();
    Lesson actual = new Lesson(
        new Student(new StudentIdentifier(studentIdentifier), new StudentName("studentName")),
        new Tutor(new TutorIdentifier(tutorIdentifier), new TutorName("tutorName")));
    Lesson expected = new Lesson(
        new Student(new StudentIdentifier(studentIdentifier), new StudentName("studentName")),
        new Tutor(new TutorIdentifier(tutorIdentifier), new TutorName("tutorName")));
    Assertions.assertThat(actual).usingRecursiveComparison().isEqualTo(expected);
}

Multiple at once

@Test
void Do not end verification in the middle() {
    String studentIdentifier = generateUUID();
    String tutorIdentifier = generateUUID();
    Lesson actual = new Lesson(
        new Student(new StudentIdentifier(studentIdentifier), new StudentName("studentName")),
        new Tutor(new TutorIdentifier(tutorIdentifier), new TutorName("tutorName")));
    SoftAssertions softAssertions = new SoftAssertions();
    softAssertions.assertThat(actual.student().identifier().value()).as("OK").isEqualTo(studentIdentifier);
    softAssertions.assertThat(actual.student().name().value()).as("NG").isEqualTo("x");
    softAssertions.assertThat(actual.tutor().identifier().value()).as("OK").isEqualTo(tutorIdentifier);
    softAssertions.assertThat(actual.tutor().name().value()).as("OK").isEqualTo("tutorName");
    softAssertions.assertAll();
}

Collection system

size

@Test
void size verification() {
    List<String> actual = Arrays.asList("a", "b", "c");
    Assertions.assertThat(actual).hasSize(3);
}

All elements

Verification error will occur if all Tuple Iterable elements generated by extracting are not included.

@Test
void Verification of all elements() {

    //Generate the first lesson
    String student1Identifier = generateUUID();
    String tutor1Identifier = generateUUID();
    Lesson lesson1 = new Lesson(
            new Student(new StudentIdentifier(student1Identifier), new StudentName("student1")),
            new Tutor(new TutorIdentifier(tutor1Identifier), new TutorName("tutor1")));

    //Generate a second lesson
    String student2Identifier = generateUUID();
    String tutor2Identifier = generateUUID();
    Lesson lesson2 = new Lesson(
            new Student(new StudentIdentifier(student2Identifier), new StudentName("student2")),
            new Tutor(new TutorIdentifier(tutor2Identifier), new TutorName("tutor2")));

    Lessons actual = new Lessons(Arrays.asList(lesson1, lesson2));
    Assertions.assertThat(actual.list())
            .usingRecursiveFieldByFieldElementComparator()
            //Verification of all values, any order
            .containsOnly(
                    new Lesson(new Student(new StudentIdentifier(student2Identifier), new StudentName("student2")), new Tutor(new TutorIdentifier(tutor2Identifier), new TutorName("tutor2"))),
                    new Lesson(new Student(new StudentIdentifier(student1Identifier), new StudentName("student1")), new Tutor(new TutorIdentifier(tutor1Identifier), new TutorName("tutor1"))));
}

Order

@Test
void Order verification() {
    //Generate the first lesson
    String student1Identifier = generateUUID();
    String tutor1Identifier = generateUUID();
    Lesson lesson1 = new Lesson(
            new Student(new StudentIdentifier(student1Identifier), new StudentName("student1")),
            new Tutor(new TutorIdentifier(tutor1Identifier), new TutorName("tutor1")));

    //Generate a second lesson
    String student2Identifier = generateUUID();
    String tutor2Identifier = generateUUID();
    Lesson lesson2 = new Lesson(
            new Student(new StudentIdentifier(student2Identifier), new StudentName("student2")),
            new Tutor(new TutorIdentifier(tutor2Identifier), new TutorName("tutor2")));

    //Generate a third lesson
    String student3Identifier = generateUUID();
    String tutor3Identifier = generateUUID();
    Lesson lesson3 = new Lesson(
            new Student(new StudentIdentifier(student3Identifier), new StudentName("student3")),
            new Tutor(new TutorIdentifier(tutor3Identifier), new TutorName("tutor3")));

    Lessons actual = new Lessons(Arrays.asList(lesson1, lesson2, lesson3));

    Assertions.assertThat(actual.list())
            .usingRecursiveFieldByFieldElementComparator()
            //Verification of all values
            .containsExactly(
                    new Lesson(new Student(new StudentIdentifier(student1Identifier), new StudentName("student1")), new Tutor(new TutorIdentifier(tutor1Identifier), new TutorName("tutor1"))),
                    new Lesson(new Student(new StudentIdentifier(student2Identifier), new StudentName("student2")), new Tutor(new TutorIdentifier(tutor2Identifier), new TutorName("tutor2"))),
                    new Lesson(new Student(new StudentIdentifier(student3Identifier), new StudentName("student3")), new Tutor(new TutorIdentifier(tutor3Identifier), new TutorName("tutor3"))));
}

Some values

@Test
Extract and validate some values of a void object() {
    //Generate the first lesson
    Lesson lesson1 = new Lesson(
            new Student(new StudentIdentifier(generateUUID()), new StudentName("student1")),
            new Tutor(new TutorIdentifier(generateUUID()), new TutorName("tutor1")));

    //Generate a second lesson
    Lesson lesson2 = new Lesson(
            new Student(new StudentIdentifier(generateUUID()), new StudentName("student2")),
            new Tutor(new TutorIdentifier(generateUUID()), new TutorName("tutor2")));

    Lessons actual = new Lessons(Arrays.asList(lesson1, lesson2));
    Assertions.assertThat(actual.list())
            //Decompose the elements of Iterable to create a new Tuple Iterable
            .extracting(
                    lesson -> lesson.student().name().value(),
                    lesson -> lesson.tutor().name().value())
            //Verification of all values, any order
            .containsOnly(
                    //Validated with Tuple
                    Tuple.tuple("student2", "tutor2"),
                    Tuple.tuple("student1", "tutor1"));
}

exception

Is it the correct exception?

I dare to use ʻassertThatExceptionOfType`,

@Test
receive a void exception() {
    Assertions.assertThatExceptionOfType(IOException.class)
            .isThrownBy(() -> {
                throw new IOException("IOException Message");
            })
            .withMessage("%s Message", "IOException")
            .withMessageContaining("IOException")
            .withNoCause();
}

I wrote only Nullpo

@Test
void Nullpo only() {
    Assertions.assertThatNullPointerException().isThrownBy(() -> { throw new NullPointerException("Nullpo"); })
            .withMessage("Nullpo%s", "Gat")
            .withMessageContaining("Gat")
            .withNoCause();
}

Afterword

Let's add if something is used frequently Please comment if you have any recommendations

Recommended Posts

Test API often used in AssertJ
Gem often used in Rails
Matcher often used in RSpec
Syntax examples often used in Java
About methods often used in devise
Commands often used in MySQL operations
Ruby methods often used in Rails
Test the response JSON of the REST API created in Spring MVC using AssertJ in MockMvc
Zabbix API in Java
Personal summary of the guys often used in JUnit 4
About method matchers used in model unit test code (RSpec)
Test private methods in JUnit
Java Stream API in 5 minutes
Frequently used processes in SpreadSheet
Test private methods in JUnit
Mechanism and characteristics of Collection implementation class often used in Java