From 9d17c51e0f7126705355038c119c63e5a600de37 Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Wed, 10 Oct 2018 18:41:57 +0200 Subject: [PATCH] Migrating from JUnit 4 to JUnit 5 --- pom.xml | 19 +++++++++++++++++ .../petclinic/PetclinicIntegrationTests.java | 6 +----- .../petclinic/model/ValidatorTests.java | 3 +-- .../petclinic/owner/OwnerControllerTests.java | 12 +++-------- .../petclinic/owner/PetControllerTests.java | 16 +++----------- .../owner/PetTypeFormatterTests.java | 21 +++++++++++-------- .../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, 51 insertions(+), 66 deletions(-) diff --git a/pom.xml b/pom.xml index 12462197e75..ff32589ed1c 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 3f48c0d4a53..be9e00258fd 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 7da0d3dea5e..cc1a9df982e 100644 --- a/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java +++ b/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java @@ -6,8 +6,7 @@ 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 7fccb3b044a..3bbe38abcfd 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java @@ -10,16 +10,11 @@ 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.samples.petclinic.owner.Owner; -import org.springframework.samples.petclinic.owner.OwnerController; -import org.springframework.samples.petclinic.owner.OwnerRepository; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; /** @@ -27,7 +22,6 @@ * * @author Colin But */ -@RunWith(SpringRunner.class) @WebMvcTest(OwnerController.class) public class OwnerControllerTests { @@ -41,7 +35,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 f95d7c87cd1..d58444417f7 100755 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java @@ -8,22 +8,13 @@ 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; /** @@ -31,7 +22,6 @@ * * @author Colin But */ -@RunWith(SpringRunner.class) @WebMvcTest(value = PetController.class, includeFilters = @ComponentScan.Filter( value = PetTypeFormatter.class, @@ -51,7 +41,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 4e8e36c147c..1bc483595be 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java @@ -6,21 +6,22 @@ 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.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * Test class for {@link PetTypeFormatter} * * @author Colin But */ -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class PetTypeFormatterTests { @Mock @@ -28,7 +29,7 @@ public class PetTypeFormatterTests { private PetTypeFormatter petTypeFormatter; - @Before + @BeforeEach public void setup() { this.petTypeFormatter = new PetTypeFormatter(pets); } @@ -48,10 +49,12 @@ public void shouldParse() throws ParseException { assertEquals("Bird", petType.getName()); } - @Test(expected = ParseException.class) + @Test public void shouldThrowParseException() throws ParseException { Mockito.when(this.pets.findPetTypes()).thenReturn(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 08d61360eb9..b9fdaffc387 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java @@ -7,17 +7,12 @@ 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; /** @@ -25,7 +20,6 @@ * * @author Colin But */ -@RunWith(SpringRunner.class) @WebMvcTest(VisitController.class) public class VisitControllerTests { @@ -40,7 +34,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 9f12151d993..a1a83b3a485 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 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.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 @@ * @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 3f108bfe9d4..7e034071206 100644 --- a/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java @@ -1,12 +1,9 @@ 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; @@ -20,9 +17,8 @@ * * @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 21f2edaa4a3..67600409deb 100644 --- a/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java @@ -9,21 +9,18 @@ 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 { @@ -33,7 +30,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 de3a7b9bba6..c512e86487a 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;