mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-04-25 20:02:47 +00:00
Add new test file
-<modify>: remove `@Transactional`. -<add>: create unit-test file related to `PetValidator`. -<refactor>: move pet objects initialization to `@BeforeEach` setup.
This commit is contained in:
parent
214a8fb87f
commit
40a41375e6
2 changed files with 117 additions and 1 deletions
|
@ -44,7 +44,6 @@ public interface OwnerRepository extends JpaRepository<Owner, Integer> {
|
||||||
* @return a Collection of {@link PetType}s.
|
* @return a Collection of {@link PetType}s.
|
||||||
*/
|
*/
|
||||||
@Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name")
|
@Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name")
|
||||||
@Transactional(readOnly = true)
|
|
||||||
List<PetType> findPetTypes();
|
List<PetType> findPetTypes();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -0,0 +1,117 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2024 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.samples.petclinic.owner;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.condition.DisabledInNativeImage;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import org.springframework.validation.Errors;
|
||||||
|
import org.springframework.validation.MapBindingResult;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for {@link PetValidator}
|
||||||
|
*
|
||||||
|
* @author Wick Dynex
|
||||||
|
*/
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
@DisabledInNativeImage
|
||||||
|
public class PetValidatorTests {
|
||||||
|
|
||||||
|
private PetValidator petValidator;
|
||||||
|
|
||||||
|
private Pet pet;
|
||||||
|
|
||||||
|
private PetType petType;
|
||||||
|
|
||||||
|
private Errors errors;
|
||||||
|
|
||||||
|
private static final String petName = "Buddy";
|
||||||
|
|
||||||
|
private static final String petTypeName = "Dog";
|
||||||
|
|
||||||
|
private static final LocalDate petBirthDate = LocalDate.of(1990, 1, 1);
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
petValidator = new PetValidator();
|
||||||
|
pet = new Pet();
|
||||||
|
petType = new PetType();
|
||||||
|
errors = new MapBindingResult(new HashMap<>(), "pet");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidate() {
|
||||||
|
petType.setName(petTypeName);
|
||||||
|
pet.setName(petName);
|
||||||
|
pet.setType(petType);
|
||||||
|
pet.setBirthDate(petBirthDate);
|
||||||
|
|
||||||
|
petValidator.validate(pet, errors);
|
||||||
|
|
||||||
|
assertFalse(errors.hasErrors());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
class ValidateHasErrors {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateWithInvalidPetName() {
|
||||||
|
petType.setName(petTypeName);
|
||||||
|
pet.setName("");
|
||||||
|
pet.setType(petType);
|
||||||
|
pet.setBirthDate(petBirthDate);
|
||||||
|
|
||||||
|
petValidator.validate(pet, errors);
|
||||||
|
|
||||||
|
assertTrue(errors.hasFieldErrors("name"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateWithInvalidPetType() {
|
||||||
|
pet.setName(petName);
|
||||||
|
pet.setType(null);
|
||||||
|
pet.setBirthDate(petBirthDate);
|
||||||
|
|
||||||
|
petValidator.validate(pet, errors);
|
||||||
|
|
||||||
|
assertTrue(errors.hasFieldErrors("type"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateWithInvalidBirthDate() {
|
||||||
|
petType.setName(petTypeName);
|
||||||
|
pet.setName(petName);
|
||||||
|
pet.setType(petType);
|
||||||
|
pet.setBirthDate(null);
|
||||||
|
|
||||||
|
petValidator.validate(pet, errors);
|
||||||
|
|
||||||
|
assertTrue(errors.hasFieldErrors("birthDate"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue