――L'assertion de JUnit5 est excellente, mais j'ose AssertJ ―― N'écrivez que ce que vous utilisez habituellement
--Java8 (Pardonnez-moi car c'est l'environnement que j'utilise au travail ...)
La source est ici
ʻUsingRecursiveComparison` empêche la validation de classe imbriquée à partir des comparaisons d'instances
@Test
Valider récursivement les champs d'objets vides() {
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 Ne pas terminer la vérification au milieu() {
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
vérification de la taille annulée() {
List<String> actual = Arrays.asList("a", "b", "c");
Assertions.assertThat(actual).hasSize(3);
}
Une erreur de vérification se produira si tous les éléments de Tuple's Iterable générés par extraction ne sont pas inclus.
@Test
void Vérification de tous les éléments() {
//Générer la première leçon
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")));
//Générer une deuxième leçon
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()
//Vérification de toutes les valeurs, de toute commande
.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
annulation de la vérification de la commande() {
//Générer la première leçon
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")));
//Générer une deuxième leçon
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")));
//Générer une troisième leçon
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()
//Vérification de toutes les valeurs
.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
Extraire quelques valeurs de l'objet void et vérifier() {
//Générer la première leçon
Lesson lesson1 = new Lesson(
new Student(new StudentIdentifier(generateUUID()), new StudentName("student1")),
new Tutor(new TutorIdentifier(generateUUID()), new TutorName("tutor1")));
//Générer une deuxième leçon
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())
//Décomposer les éléments de Iterable pour créer un nouveau Tuple Iterable
.extracting(
lesson -> lesson.student().name().value(),
lesson -> lesson.tutor().name().value())
//Vérification de toutes les valeurs, de toute commande
.containsOnly(
//Validé avec Tuple
Tuple.tuple("student2", "tutor2"),
Tuple.tuple("student1", "tutor1"));
}
J'ose utiliser ʻassertThatExceptionOfType`,
@Test
recevoir une exception nulle() {
Assertions.assertThatExceptionOfType(IOException.class)
.isThrownBy(() -> {
throw new IOException("IOException Message");
})
.withMessage("%s Message", "IOException")
.withMessageContaining("IOException")
.withNoCause();
}
J'ai écrit seulement tiède
@Test
annuler Nurupo uniquement() {
Assertions.assertThatNullPointerException().isThrownBy(() -> { throw new NullPointerException("Visqueux"); })
.withMessage("Visqueux%s", "Flingue")
.withMessageContaining("Flingue")
.withNoCause();
}
Ajoutons si quelque chose est fréquemment utilisé Veuillez commenter si vous avez des recommandations
Recommended Posts