From 3f838c2df27013cccadd4b05a11a491038167eb3 Mon Sep 17 00:00:00 2001 From: Johannes Wengert Date: Fri, 31 May 2019 23:08:22 +0200 Subject: [PATCH 1/2] Cleanup tests by using more idiomatic assertj assertions See gh-418 --- .../samples/petclinic/model/ValidatorTests.java | 2 +- .../petclinic/owner/PetTypeFormatterTests.java | 12 ++++++------ .../petclinic/service/ClinicServiceTests.java | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java b/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java index 151c0ce7016..e37a9656255 100644 --- a/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java +++ b/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java @@ -53,7 +53,7 @@ public void shouldNotValidateWhenFirstNameEmpty() { Set> constraintViolations = validator .validate(person); - assertThat(constraintViolations.size()).isEqualTo(1); + assertThat(constraintViolations).hasSize(1); ConstraintViolation violation = constraintViolations.iterator().next(); assertThat(violation.getPropertyPath().toString()).isEqualTo("firstName"); assertThat(violation.getMessage()).isEqualTo("must not be empty"); diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java index 47812a812ed..cf5f6cfb24d 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java @@ -26,10 +26,10 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; /** * Test class for {@link PetTypeFormatter} @@ -54,19 +54,19 @@ public void testPrint() { PetType petType = new PetType(); petType.setName("Hamster"); String petTypeName = this.petTypeFormatter.print(petType, Locale.ENGLISH); - assertEquals("Hamster", petTypeName); + assertThat(petTypeName).isEqualTo("Hamster"); } @Test public void shouldParse() throws ParseException { - Mockito.when(this.pets.findPetTypes()).thenReturn(makePetTypes()); + when(this.pets.findPetTypes()).thenReturn(makePetTypes()); PetType petType = petTypeFormatter.parse("Bird", Locale.ENGLISH); - assertEquals("Bird", petType.getName()); + assertThat(petType.getName()).isEqualTo("Bird"); } @Test(expected = ParseException.class) public void shouldThrowParseException() throws ParseException { - Mockito.when(this.pets.findPetTypes()).thenReturn(makePetTypes()); + when(this.pets.findPetTypes()).thenReturn(makePetTypes()); petTypeFormatter.parse("Fish", Locale.ENGLISH); } diff --git a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java index 8a7a2f0a5f7..c103caac0c4 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java @@ -79,17 +79,17 @@ public class ClinicServiceTests { @Test public void shouldFindOwnersByLastName() { Collection owners = this.owners.findByLastName("Davis"); - assertThat(owners.size()).isEqualTo(2); + assertThat(owners).hasSize(2); owners = this.owners.findByLastName("Daviss"); - assertThat(owners.isEmpty()).isTrue(); + assertThat(owners).isEmpty(); } @Test public void shouldFindSingleOwnerWithPet() { Owner owner = this.owners.findById(1); assertThat(owner.getLastName()).startsWith("Franklin"); - assertThat(owner.getPets().size()).isEqualTo(1); + assertThat(owner.getPets()).hasSize(1); assertThat(owner.getPets().get(0).getType()).isNotNull(); assertThat(owner.getPets().get(0).getType().getName()).isEqualTo("cat"); } @@ -213,7 +213,7 @@ public void shouldAddNewVisitForPet() { @Test public void shouldFindVisitsByPetId() throws Exception { Collection visits = this.visits.findByPetId(7); - assertThat(visits.size()).isEqualTo(2); + assertThat(visits).hasSize(2); Visit[] visitArr = visits.toArray(new Visit[visits.size()]); assertThat(visitArr[0].getDate()).isNotNull(); assertThat(visitArr[0].getPetId()).isEqualTo(7); From 8919c7317b26854dd742be5ebd345badaed0bcb2 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Sat, 1 Jun 2019 18:09:33 +0200 Subject: [PATCH 2/2] Polish "Cleanup tests by using more idiomatic assertj assertions" See gh-418 --- .../samples/petclinic/owner/PetTypeFormatterTests.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java index cf5f6cfb24d..9ba43148019 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java @@ -29,7 +29,7 @@ import org.mockito.junit.MockitoJUnitRunner; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.when; +import static org.mockito.BDDMockito.given; /** * Test class for {@link PetTypeFormatter} @@ -59,14 +59,14 @@ public void testPrint() { @Test public void shouldParse() throws ParseException { - when(this.pets.findPetTypes()).thenReturn(makePetTypes()); + given(this.pets.findPetTypes()).willReturn(makePetTypes()); PetType petType = petTypeFormatter.parse("Bird", Locale.ENGLISH); assertThat(petType.getName()).isEqualTo("Bird"); } @Test(expected = ParseException.class) public void shouldThrowParseException() throws ParseException { - when(this.pets.findPetTypes()).thenReturn(makePetTypes()); + given(this.pets.findPetTypes()).willReturn(makePetTypes()); petTypeFormatter.parse("Fish", Locale.ENGLISH); }