From ac69dbba23d9956b7806b2f69e65d950755bab5a Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Wed, 10 Oct 2018 18:41:57 +0200 Subject: [PATCH 1/2] Migrate tests to JUnit 5 See gh-360 --- pom.xml | 19 +++++++++++++++++++ .../petclinic/PetclinicIntegrationTests.java | 6 +----- .../petclinic/model/ValidatorTests.java | 3 +-- .../petclinic/owner/OwnerControllerTests.java | 10 +++------- .../petclinic/owner/PetControllerTests.java | 16 +++------------- .../owner/PetTypeFormatterTests.java | 19 +++++++++++-------- .../petclinic/owner/VisitControllerTests.java | 12 +++--------- .../petclinic/service/ClinicServiceTests.java | 6 ++---- .../system/CrashControllerTests.java | 10 +++------- .../petclinic/vet/VetControllerTests.java | 9 +++------ .../samples/petclinic/vet/VetTests.java | 3 +-- 11 files changed, 50 insertions(+), 63 deletions(-) diff --git a/pom.xml b/pom.xml index 0f50398bf..f23a192fa 100644 --- a/pom.xml +++ b/pom.xml @@ -57,6 +57,13 @@ org.springframework.boot spring-boot-starter-test test + + + + junit + junit + + @@ -103,6 +110,18 @@ + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.mockito + mockito-junit-jupiter + test + + org.springframework.boot spring-boot-devtools diff --git a/src/test/java/org/springframework/samples/petclinic/PetclinicIntegrationTests.java b/src/test/java/org/springframework/samples/petclinic/PetclinicIntegrationTests.java index 669dbddaa..425172b30 100644 --- a/src/test/java/org/springframework/samples/petclinic/PetclinicIntegrationTests.java +++ b/src/test/java/org/springframework/samples/petclinic/PetclinicIntegrationTests.java @@ -16,15 +16,11 @@ package org.springframework.samples.petclinic; -import org.junit.Test; -import org.junit.runner.RunWith; - +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.samples.petclinic.vet.VetRepository; -import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringRunner.class) @SpringBootTest public class PetclinicIntegrationTests { 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 e37a96562..30397742e 100644 --- a/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java +++ b/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java @@ -22,8 +22,7 @@ import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.Validator; -import org.junit.Test; - +import org.junit.jupiter.api.Test; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; diff --git a/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java index 5084f4245..899da1471 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java @@ -23,16 +23,13 @@ import java.util.List; import org.assertj.core.util.Lists; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; - +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.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.samples.petclinic.visit.Visit; import org.springframework.samples.petclinic.visit.VisitRepository; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import static org.hamcrest.Matchers.empty; @@ -51,7 +48,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * * @author Colin But */ -@RunWith(SpringRunner.class) @WebMvcTest(OwnerController.class) public class OwnerControllerTests { @@ -68,7 +64,7 @@ public class OwnerControllerTests { private Owner george; - @Before + @BeforeEach public void setup() { george = new Owner(); george.setId(TEST_OWNER_ID); diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java index 2ffae3025..4c0d69538 100755 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java @@ -24,22 +24,13 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; import org.assertj.core.util.Lists; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +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.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.FilterType; -import org.springframework.samples.petclinic.owner.Owner; -import org.springframework.samples.petclinic.owner.OwnerRepository; -import org.springframework.samples.petclinic.owner.Pet; -import org.springframework.samples.petclinic.owner.PetController; -import org.springframework.samples.petclinic.owner.PetRepository; -import org.springframework.samples.petclinic.owner.PetType; -import org.springframework.samples.petclinic.owner.PetTypeFormatter; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; /** @@ -47,7 +38,6 @@ import org.springframework.test.web.servlet.MockMvc; * * @author Colin But */ -@RunWith(SpringRunner.class) @WebMvcTest(value = PetController.class, includeFilters = @ComponentScan.Filter( value = PetTypeFormatter.class, @@ -67,7 +57,7 @@ public class PetControllerTests { @MockBean private OwnerRepository owners; - @Before + @BeforeEach public void setup() { PetType cat = new PetType(); cat.setId(3); 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 9ba431480..af98b5cfd 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java @@ -22,11 +22,12 @@ import java.util.Collection; import java.util.List; import java.util.Locale; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; @@ -36,7 +37,7 @@ import static org.mockito.BDDMockito.given; * * @author Colin But */ -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class PetTypeFormatterTests { @Mock @@ -44,7 +45,7 @@ public class PetTypeFormatterTests { private PetTypeFormatter petTypeFormatter; - @Before + @BeforeEach public void setup() { this.petTypeFormatter = new PetTypeFormatter(pets); } @@ -64,10 +65,12 @@ public class PetTypeFormatterTests { assertThat(petType.getName()).isEqualTo("Bird"); } - @Test(expected = ParseException.class) + @Test public void shouldThrowParseException() throws ParseException { given(this.pets.findPetTypes()).willReturn(makePetTypes()); - petTypeFormatter.parse("Fish", Locale.ENGLISH); + Assertions.assertThrows(ParseException.class, () -> { + petTypeFormatter.parse("Fish", Locale.ENGLISH); + }); } /** diff --git a/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java index 3edd20959..93f30b9aa 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java @@ -23,17 +23,12 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +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.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.samples.petclinic.owner.Pet; -import org.springframework.samples.petclinic.owner.PetRepository; -import org.springframework.samples.petclinic.owner.VisitController; import org.springframework.samples.petclinic.visit.VisitRepository; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; /** @@ -41,7 +36,6 @@ import org.springframework.test.web.servlet.MockMvc; * * @author Colin But */ -@RunWith(SpringRunner.class) @WebMvcTest(VisitController.class) public class VisitControllerTests { @@ -56,7 +50,7 @@ public class VisitControllerTests { @MockBean private PetRepository pets; - @Before + @BeforeEach public void init() { given(this.pets.findById(TEST_PET_ID)).willReturn(new Pet()); } 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 c103caac0..562025de9 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java @@ -21,8 +21,8 @@ import static org.assertj.core.api.Assertions.assertThat; import java.time.LocalDate; import java.util.Collection; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.ComponentScan; @@ -36,7 +36,6 @@ import org.springframework.samples.petclinic.vet.VetRepository; import org.springframework.samples.petclinic.visit.Visit; import org.springframework.samples.petclinic.visit.VisitRepository; import org.springframework.stereotype.Service; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.Transactional; /** @@ -60,7 +59,6 @@ import org.springframework.transaction.annotation.Transactional; * @author Dave Syer */ -@RunWith(SpringRunner.class) @DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class)) public class ClinicServiceTests { diff --git a/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java b/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java index cc1e235cb..b38a7840d 100644 --- a/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java @@ -16,13 +16,10 @@ package org.springframework.samples.petclinic.system; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; - +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @@ -36,9 +33,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * * @author Colin But */ -@RunWith(SpringRunner.class) // Waiting https://github.com/spring-projects/spring-boot/issues/5574 -@Ignore +@Disabled @WebMvcTest(controllers = CrashController.class) public class CrashControllerTests { diff --git a/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java index 145c61596..c3681f88f 100644 --- a/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java @@ -25,21 +25,18 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; import org.assertj.core.util.Lists; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +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.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; /** * Test class for the {@link VetController} */ -@RunWith(SpringRunner.class) @WebMvcTest(VetController.class) public class VetControllerTests { @@ -49,7 +46,7 @@ public class VetControllerTests { @MockBean private VetRepository vets; - @Before + @BeforeEach public void setup() { Vet james = new Vet(); james.setFirstName("James"); diff --git a/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java b/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java index 542250751..0e32c531f 100644 --- a/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java +++ b/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java @@ -15,8 +15,7 @@ */ package org.springframework.samples.petclinic.vet; -import org.junit.Test; - +import org.junit.jupiter.api.Test; import org.springframework.util.SerializationUtils; import static org.assertj.core.api.Assertions.assertThat; From 253e6fde5f488384e0f91c7668917269a780c6d7 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Sun, 20 Oct 2019 16:49:17 +0200 Subject: [PATCH 2/2] Polish "Migrate tests to JUnit 5" See gh-360 --- pom.xml | 5 ++-- .../petclinic/PetclinicIntegrationTests.java | 4 +-- .../petclinic/model/ValidatorTests.java | 4 +-- .../petclinic/owner/OwnerControllerTests.java | 26 +++++++++---------- .../petclinic/owner/PetControllerTests.java | 16 ++++++------ .../owner/PetTypeFormatterTests.java | 10 +++---- .../petclinic/owner/VisitControllerTests.java | 10 +++---- .../petclinic/service/ClinicServiceTests.java | 26 +++++++++---------- .../system/CrashControllerTests.java | 4 +-- .../petclinic/vet/VetControllerTests.java | 8 +++--- .../samples/petclinic/vet/VetTests.java | 5 ++-- 11 files changed, 57 insertions(+), 61 deletions(-) diff --git a/pom.xml b/pom.xml index f23a192fa..9802b8935 100644 --- a/pom.xml +++ b/pom.xml @@ -58,10 +58,9 @@ spring-boot-starter-test test - - junit - junit + org.junit.vintage + junit-vintage-engine diff --git a/src/test/java/org/springframework/samples/petclinic/PetclinicIntegrationTests.java b/src/test/java/org/springframework/samples/petclinic/PetclinicIntegrationTests.java index 425172b30..217d552f4 100644 --- a/src/test/java/org/springframework/samples/petclinic/PetclinicIntegrationTests.java +++ b/src/test/java/org/springframework/samples/petclinic/PetclinicIntegrationTests.java @@ -22,13 +22,13 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.samples.petclinic.vet.VetRepository; @SpringBootTest -public class PetclinicIntegrationTests { +class PetclinicIntegrationTests { @Autowired private VetRepository vets; @Test - public void testFindAll() throws Exception { + void testFindAll() throws Exception { vets.findAll(); vets.findAll(); // served from cache } 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 30397742e..4be38f714 100644 --- a/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java +++ b/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java @@ -32,7 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Michael Isvy Simple test to make sure that Bean Validation is working (useful * when upgrading to a new version of Hibernate Validator/ Bean Validation) */ -public class ValidatorTests { +class ValidatorTests { private Validator createValidator() { LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean(); @@ -41,7 +41,7 @@ public class ValidatorTests { } @Test - public void shouldNotValidateWhenFirstNameEmpty() { + void shouldNotValidateWhenFirstNameEmpty() { LocaleContextHolder.setLocale(Locale.ENGLISH); Person person = new Person(); diff --git a/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java index 899da1471..1956e2a24 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java @@ -49,7 +49,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * @author Colin But */ @WebMvcTest(OwnerController.class) -public class OwnerControllerTests { +class OwnerControllerTests { private static final int TEST_OWNER_ID = 1; @@ -65,7 +65,7 @@ public class OwnerControllerTests { private Owner george; @BeforeEach - public void setup() { + void setup() { george = new Owner(); george.setId(TEST_OWNER_ID); george.setFirstName("George"); @@ -88,7 +88,7 @@ public class OwnerControllerTests { } @Test - public void testInitCreationForm() throws Exception { + void testInitCreationForm() throws Exception { mockMvc.perform(get("/owners/new")) .andExpect(status().isOk()) .andExpect(model().attributeExists("owner")) @@ -96,7 +96,7 @@ public class OwnerControllerTests { } @Test - public void testProcessCreationFormSuccess() throws Exception { + void testProcessCreationFormSuccess() throws Exception { mockMvc.perform(post("/owners/new") .param("firstName", "Joe") .param("lastName", "Bloggs") @@ -108,7 +108,7 @@ public class OwnerControllerTests { } @Test - public void testProcessCreationFormHasErrors() throws Exception { + void testProcessCreationFormHasErrors() throws Exception { mockMvc.perform(post("/owners/new") .param("firstName", "Joe") .param("lastName", "Bloggs") @@ -122,7 +122,7 @@ public class OwnerControllerTests { } @Test - public void testInitFindForm() throws Exception { + void testInitFindForm() throws Exception { mockMvc.perform(get("/owners/find")) .andExpect(status().isOk()) .andExpect(model().attributeExists("owner")) @@ -130,7 +130,7 @@ public class OwnerControllerTests { } @Test - public void testProcessFindFormSuccess() throws Exception { + void testProcessFindFormSuccess() throws Exception { given(this.owners.findByLastName("")).willReturn(Lists.newArrayList(george, new Owner())); mockMvc.perform(get("/owners")) .andExpect(status().isOk()) @@ -138,7 +138,7 @@ public class OwnerControllerTests { } @Test - public void testProcessFindFormByLastName() throws Exception { + void testProcessFindFormByLastName() throws Exception { given(this.owners.findByLastName(george.getLastName())).willReturn(Lists.newArrayList(george)); mockMvc.perform(get("/owners") .param("lastName", "Franklin") @@ -148,7 +148,7 @@ public class OwnerControllerTests { } @Test - public void testProcessFindFormNoOwnersFound() throws Exception { + void testProcessFindFormNoOwnersFound() throws Exception { mockMvc.perform(get("/owners") .param("lastName", "Unknown Surname") ) @@ -159,7 +159,7 @@ public class OwnerControllerTests { } @Test - public void testInitUpdateOwnerForm() throws Exception { + void testInitUpdateOwnerForm() throws Exception { mockMvc.perform(get("/owners/{ownerId}/edit", TEST_OWNER_ID)) .andExpect(status().isOk()) .andExpect(model().attributeExists("owner")) @@ -172,7 +172,7 @@ public class OwnerControllerTests { } @Test - public void testProcessUpdateOwnerFormSuccess() throws Exception { + void testProcessUpdateOwnerFormSuccess() throws Exception { mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID) .param("firstName", "Joe") .param("lastName", "Bloggs") @@ -185,7 +185,7 @@ public class OwnerControllerTests { } @Test - public void testProcessUpdateOwnerFormHasErrors() throws Exception { + void testProcessUpdateOwnerFormHasErrors() throws Exception { mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID) .param("firstName", "Joe") .param("lastName", "Bloggs") @@ -199,7 +199,7 @@ public class OwnerControllerTests { } @Test - public void testShowOwner() throws Exception { + void testShowOwner() throws Exception { mockMvc.perform(get("/owners/{ownerId}", TEST_OWNER_ID)) .andExpect(status().isOk()) .andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin")))) diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java index 4c0d69538..f7c8fe726 100755 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java @@ -42,7 +42,7 @@ import org.springframework.test.web.servlet.MockMvc; includeFilters = @ComponentScan.Filter( value = PetTypeFormatter.class, type = FilterType.ASSIGNABLE_TYPE)) -public class PetControllerTests { +class PetControllerTests { private static final int TEST_OWNER_ID = 1; private static final int TEST_PET_ID = 1; @@ -58,7 +58,7 @@ public class PetControllerTests { private OwnerRepository owners; @BeforeEach - public void setup() { + void setup() { PetType cat = new PetType(); cat.setId(3); cat.setName("hamster"); @@ -69,7 +69,7 @@ public class PetControllerTests { } @Test - public void testInitCreationForm() throws Exception { + void testInitCreationForm() throws Exception { mockMvc.perform(get("/owners/{ownerId}/pets/new", TEST_OWNER_ID)) .andExpect(status().isOk()) .andExpect(view().name("pets/createOrUpdatePetForm")) @@ -77,7 +77,7 @@ public class PetControllerTests { } @Test - public void testProcessCreationFormSuccess() throws Exception { + void testProcessCreationFormSuccess() throws Exception { mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID) .param("name", "Betty") .param("type", "hamster") @@ -88,7 +88,7 @@ public class PetControllerTests { } @Test - public void testProcessCreationFormHasErrors() throws Exception { + void testProcessCreationFormHasErrors() throws Exception { mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID) .param("name", "Betty") .param("birthDate", "2015-02-12") @@ -102,7 +102,7 @@ public class PetControllerTests { } @Test - public void testInitUpdateForm() throws Exception { + void testInitUpdateForm() throws Exception { mockMvc.perform(get("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID)) .andExpect(status().isOk()) .andExpect(model().attributeExists("pet")) @@ -110,7 +110,7 @@ public class PetControllerTests { } @Test - public void testProcessUpdateFormSuccess() throws Exception { + void testProcessUpdateFormSuccess() throws Exception { mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID) .param("name", "Betty") .param("type", "hamster") @@ -121,7 +121,7 @@ public class PetControllerTests { } @Test - public void testProcessUpdateFormHasErrors() throws Exception { + void testProcessUpdateFormHasErrors() throws Exception { mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID) .param("name", "Betty") .param("birthDate", "2015/02/12") 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 af98b5cfd..e27fce21e 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java @@ -38,7 +38,7 @@ import static org.mockito.BDDMockito.given; * @author Colin But */ @ExtendWith(MockitoExtension.class) -public class PetTypeFormatterTests { +class PetTypeFormatterTests { @Mock private PetRepository pets; @@ -46,12 +46,12 @@ public class PetTypeFormatterTests { private PetTypeFormatter petTypeFormatter; @BeforeEach - public void setup() { + void setup() { this.petTypeFormatter = new PetTypeFormatter(pets); } @Test - public void testPrint() { + void testPrint() { PetType petType = new PetType(); petType.setName("Hamster"); String petTypeName = this.petTypeFormatter.print(petType, Locale.ENGLISH); @@ -59,14 +59,14 @@ public class PetTypeFormatterTests { } @Test - public void shouldParse() throws ParseException { + void shouldParse() throws ParseException { given(this.pets.findPetTypes()).willReturn(makePetTypes()); PetType petType = petTypeFormatter.parse("Bird", Locale.ENGLISH); assertThat(petType.getName()).isEqualTo("Bird"); } @Test - public void shouldThrowParseException() throws ParseException { + void shouldThrowParseException() throws ParseException { given(this.pets.findPetTypes()).willReturn(makePetTypes()); Assertions.assertThrows(ParseException.class, () -> { petTypeFormatter.parse("Fish", Locale.ENGLISH); diff --git a/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java index 93f30b9aa..f61d34c9c 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java @@ -37,7 +37,7 @@ import org.springframework.test.web.servlet.MockMvc; * @author Colin But */ @WebMvcTest(VisitController.class) -public class VisitControllerTests { +class VisitControllerTests { private static final int TEST_PET_ID = 1; @@ -51,19 +51,19 @@ public class VisitControllerTests { private PetRepository pets; @BeforeEach - public void init() { + void init() { given(this.pets.findById(TEST_PET_ID)).willReturn(new Pet()); } @Test - public void testInitNewVisitForm() throws Exception { + void testInitNewVisitForm() throws Exception { mockMvc.perform(get("/owners/*/pets/{petId}/visits/new", TEST_PET_ID)) .andExpect(status().isOk()) .andExpect(view().name("pets/createOrUpdateVisitForm")); } @Test - public void testProcessNewVisitFormSuccess() throws Exception { + void testProcessNewVisitFormSuccess() throws Exception { mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID) .param("name", "George") .param("description", "Visit Description") @@ -73,7 +73,7 @@ public class VisitControllerTests { } @Test - public void testProcessNewVisitFormHasErrors() throws Exception { + void testProcessNewVisitFormHasErrors() throws Exception { mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID) .param("name", "George") ) 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 562025de9..f11f98373 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java @@ -22,7 +22,6 @@ import java.time.LocalDate; import java.util.Collection; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.ComponentScan; @@ -58,9 +57,8 @@ import org.springframework.transaction.annotation.Transactional; * @author Michael Isvy * @author Dave Syer */ - @DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class)) -public class ClinicServiceTests { +class ClinicServiceTests { @Autowired protected OwnerRepository owners; @@ -75,7 +73,7 @@ public class ClinicServiceTests { protected VetRepository vets; @Test - public void shouldFindOwnersByLastName() { + void shouldFindOwnersByLastName() { Collection owners = this.owners.findByLastName("Davis"); assertThat(owners).hasSize(2); @@ -84,7 +82,7 @@ public class ClinicServiceTests { } @Test - public void shouldFindSingleOwnerWithPet() { + void shouldFindSingleOwnerWithPet() { Owner owner = this.owners.findById(1); assertThat(owner.getLastName()).startsWith("Franklin"); assertThat(owner.getPets()).hasSize(1); @@ -94,7 +92,7 @@ public class ClinicServiceTests { @Test @Transactional - public void shouldInsertOwner() { + void shouldInsertOwner() { Collection owners = this.owners.findByLastName("Schultz"); int found = owners.size(); @@ -113,7 +111,7 @@ public class ClinicServiceTests { @Test @Transactional - public void shouldUpdateOwner() { + void shouldUpdateOwner() { Owner owner = this.owners.findById(1); String oldLastName = owner.getLastName(); String newLastName = oldLastName + "X"; @@ -127,7 +125,7 @@ public class ClinicServiceTests { } @Test - public void shouldFindPetWithCorrectId() { + void shouldFindPetWithCorrectId() { Pet pet7 = this.pets.findById(7); assertThat(pet7.getName()).startsWith("Samantha"); assertThat(pet7.getOwner().getFirstName()).isEqualTo("Jean"); @@ -135,7 +133,7 @@ public class ClinicServiceTests { } @Test - public void shouldFindAllPetTypes() { + void shouldFindAllPetTypes() { Collection petTypes = this.pets.findPetTypes(); PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1); @@ -146,7 +144,7 @@ public class ClinicServiceTests { @Test @Transactional - public void shouldInsertPetIntoDatabaseAndGenerateId() { + void shouldInsertPetIntoDatabaseAndGenerateId() { Owner owner6 = this.owners.findById(6); int found = owner6.getPets().size(); @@ -169,7 +167,7 @@ public class ClinicServiceTests { @Test @Transactional - public void shouldUpdatePetName() throws Exception { + void shouldUpdatePetName() throws Exception { Pet pet7 = this.pets.findById(7); String oldName = pet7.getName(); @@ -182,7 +180,7 @@ public class ClinicServiceTests { } @Test - public void shouldFindVets() { + void shouldFindVets() { Collection vets = this.vets.findAll(); Vet vet = EntityUtils.getById(vets, Vet.class, 3); @@ -194,7 +192,7 @@ public class ClinicServiceTests { @Test @Transactional - public void shouldAddNewVisitForPet() { + void shouldAddNewVisitForPet() { Pet pet7 = this.pets.findById(7); int found = pet7.getVisits().size(); Visit visit = new Visit(); @@ -209,7 +207,7 @@ public class ClinicServiceTests { } @Test - public void shouldFindVisitsByPetId() throws Exception { + void shouldFindVisitsByPetId() throws Exception { Collection visits = this.visits.findByPetId(7); assertThat(visits).hasSize(2); Visit[] visitArr = visits.toArray(new Visit[visits.size()]); diff --git a/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java b/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java index b38a7840d..f15689ce2 100644 --- a/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java @@ -36,13 +36,13 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. // Waiting https://github.com/spring-projects/spring-boot/issues/5574 @Disabled @WebMvcTest(controllers = CrashController.class) -public class CrashControllerTests { +class CrashControllerTests { @Autowired private MockMvc mockMvc; @Test - public void testTriggerException() throws Exception { + void testTriggerException() throws Exception { mockMvc.perform(get("/oups")).andExpect(view().name("exception")) .andExpect(model().attributeExists("exception")) .andExpect(forwardedUrl("exception")).andExpect(status().isOk()); diff --git a/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java index c3681f88f..5b6d387e7 100644 --- a/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java @@ -38,7 +38,7 @@ import org.springframework.test.web.servlet.ResultActions; * Test class for the {@link VetController} */ @WebMvcTest(VetController.class) -public class VetControllerTests { +class VetControllerTests { @Autowired private MockMvc mockMvc; @@ -47,7 +47,7 @@ public class VetControllerTests { private VetRepository vets; @BeforeEach - public void setup() { + void setup() { Vet james = new Vet(); james.setFirstName("James"); james.setLastName("Carter"); @@ -64,7 +64,7 @@ public class VetControllerTests { } @Test - public void testShowVetListHtml() throws Exception { + void testShowVetListHtml() throws Exception { mockMvc.perform(get("/vets.html")) .andExpect(status().isOk()) .andExpect(model().attributeExists("vets")) @@ -72,7 +72,7 @@ public class VetControllerTests { } @Test - public void testShowResourcesVetList() throws Exception { + void testShowResourcesVetList() throws Exception { ResultActions actions = mockMvc.perform(get("/vets") .accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()); actions.andExpect(content().contentType(MediaType.APPLICATION_JSON)) diff --git a/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java b/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java index 0e32c531f..82163eb14 100644 --- a/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java +++ b/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java @@ -22,12 +22,11 @@ import static org.assertj.core.api.Assertions.assertThat; /** * @author Dave Syer - * */ -public class VetTests { +class VetTests { @Test - public void testSerialization() { + void testSerialization() { Vet vet = new Vet(); vet.setFirstName("Zaphod"); vet.setLastName("Beeblebrox");