――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
--Java8 (Forgive me because it's my work environment ...)
The source is here
ʻ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);
}
@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();
}
@Test
void size verification() {
List<String> actual = Arrays.asList("a", "b", "c");
Assertions.assertThat(actual).hasSize(3);
}
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"))));
}
@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"))));
}
@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"));
}
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();
}
Let's add if something is used frequently Please comment if you have any recommendations
Recommended Posts