Skip to content

Commit

Permalink
Merge pull request spring-projects#360 from arey
Browse files Browse the repository at this point in the history
* pr/360:
  Polish "Migrate tests to JUnit 5"
  Migrate tests to JUnit 5

Closes spring-projectsgh-360
  • Loading branch information
snicoll committed Oct 20, 2019
2 parents ce7c3f9 + 253e6fd commit 6e4c31c
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 120 deletions.
18 changes: 18 additions & 0 deletions pom.xml
Expand Up @@ -57,6 +57,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- Databases - Uses HSQL by default -->
Expand Down Expand Up @@ -103,6 +109,18 @@
</dependency>
<!-- end of webjars -->

<!-- Testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand Down
Expand Up @@ -16,23 +16,19 @@

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 {
class PetclinicIntegrationTests {

@Autowired
private VetRepository vets;

@Test
public void testFindAll() throws Exception {
void testFindAll() throws Exception {
vets.findAll();
vets.findAll(); // served from cache
}
Expand Down
Expand Up @@ -22,8 +22,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;

Expand All @@ -33,7 +32,7 @@
* @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();
Expand All @@ -42,7 +41,7 @@ private Validator createValidator() {
}

@Test
public void shouldNotValidateWhenFirstNameEmpty() {
void shouldNotValidateWhenFirstNameEmpty() {

LocaleContextHolder.setLocale(Locale.ENGLISH);
Person person = new Person();
Expand Down
Expand Up @@ -23,16 +23,13 @@
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;
Expand All @@ -51,9 +48,8 @@
*
* @author Colin But
*/
@RunWith(SpringRunner.class)
@WebMvcTest(OwnerController.class)
public class OwnerControllerTests {
class OwnerControllerTests {

private static final int TEST_OWNER_ID = 1;

Expand All @@ -68,8 +64,8 @@ public class OwnerControllerTests {

private Owner george;

@Before
public void setup() {
@BeforeEach
void setup() {
george = new Owner();
george.setId(TEST_OWNER_ID);
george.setFirstName("George");
Expand All @@ -92,15 +88,15 @@ public void setup() {
}

@Test
public void testInitCreationForm() throws Exception {
void testInitCreationForm() throws Exception {
mockMvc.perform(get("/owners/new"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("owner"))
.andExpect(view().name("owners/createOrUpdateOwnerForm"));
}

@Test
public void testProcessCreationFormSuccess() throws Exception {
void testProcessCreationFormSuccess() throws Exception {
mockMvc.perform(post("/owners/new")
.param("firstName", "Joe")
.param("lastName", "Bloggs")
Expand All @@ -112,7 +108,7 @@ public void testProcessCreationFormSuccess() throws Exception {
}

@Test
public void testProcessCreationFormHasErrors() throws Exception {
void testProcessCreationFormHasErrors() throws Exception {
mockMvc.perform(post("/owners/new")
.param("firstName", "Joe")
.param("lastName", "Bloggs")
Expand All @@ -126,23 +122,23 @@ public void testProcessCreationFormHasErrors() throws Exception {
}

@Test
public void testInitFindForm() throws Exception {
void testInitFindForm() throws Exception {
mockMvc.perform(get("/owners/find"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("owner"))
.andExpect(view().name("owners/findOwners"));
}

@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())
.andExpect(view().name("owners/ownersList"));
}

@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")
Expand All @@ -152,7 +148,7 @@ public void testProcessFindFormByLastName() throws Exception {
}

@Test
public void testProcessFindFormNoOwnersFound() throws Exception {
void testProcessFindFormNoOwnersFound() throws Exception {
mockMvc.perform(get("/owners")
.param("lastName", "Unknown Surname")
)
Expand All @@ -163,7 +159,7 @@ public void testProcessFindFormNoOwnersFound() throws Exception {
}

@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"))
Expand All @@ -176,7 +172,7 @@ public void testInitUpdateOwnerForm() throws Exception {
}

@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")
Expand All @@ -189,7 +185,7 @@ public void testProcessUpdateOwnerFormSuccess() throws Exception {
}

@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")
Expand All @@ -203,7 +199,7 @@ public void testProcessUpdateOwnerFormHasErrors() throws Exception {
}

@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"))))
Expand Down
Expand Up @@ -24,35 +24,25 @@
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;

/**
* Test class for the {@link PetController}
*
* @author Colin But
*/
@RunWith(SpringRunner.class)
@WebMvcTest(value = PetController.class,
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;
Expand All @@ -67,8 +57,8 @@ public class PetControllerTests {
@MockBean
private OwnerRepository owners;

@Before
public void setup() {
@BeforeEach
void setup() {
PetType cat = new PetType();
cat.setId(3);
cat.setName("hamster");
Expand All @@ -79,15 +69,15 @@ public void setup() {
}

@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"))
.andExpect(model().attributeExists("pet"));
}

@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")
Expand All @@ -98,7 +88,7 @@ public void testProcessCreationFormSuccess() throws Exception {
}

@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")
Expand All @@ -112,15 +102,15 @@ public void testProcessCreationFormHasErrors() throws Exception {
}

@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"))
.andExpect(view().name("pets/createOrUpdatePetForm"));
}

@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")
Expand All @@ -131,7 +121,7 @@ public void testProcessUpdateFormSuccess() throws Exception {
}

@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")
Expand Down
Expand Up @@ -22,11 +22,12 @@
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;
Expand All @@ -36,38 +37,40 @@
*
* @author Colin But
*/
@RunWith(MockitoJUnitRunner.class)
public class PetTypeFormatterTests {
@ExtendWith(MockitoExtension.class)
class PetTypeFormatterTests {

@Mock
private PetRepository pets;

private PetTypeFormatter petTypeFormatter;

@Before
public void setup() {
@BeforeEach
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);
assertThat(petTypeName).isEqualTo("Hamster");
}

@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(expected = ParseException.class)
public void shouldThrowParseException() throws ParseException {
@Test
void shouldThrowParseException() throws ParseException {
given(this.pets.findPetTypes()).willReturn(makePetTypes());
petTypeFormatter.parse("Fish", Locale.ENGLISH);
Assertions.assertThrows(ParseException.class, () -> {
petTypeFormatter.parse("Fish", Locale.ENGLISH);
});
}

/**
Expand Down

0 comments on commit 6e4c31c

Please sign in to comment.