From 40a41375e6b55d5cf246c483d63fa9380ccb9ff1 Mon Sep 17 00:00:00 2001 From: YiXuan Ding <1328032567@qq.com> Date: Tue, 12 Nov 2024 17:33:46 +0800 Subject: [PATCH] Add new test file -: remove `@Transactional`. -: create unit-test file related to `PetValidator`. -: move pet objects initialization to `@BeforeEach` setup. --- .../petclinic/owner/OwnerRepository.java | 1 - .../petclinic/owner/PetValidatorTests.java | 117 ++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTests.java diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java index b91d80d8b49..5d7a40fbbe9 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java @@ -44,7 +44,6 @@ public interface OwnerRepository extends JpaRepository { * @return a Collection of {@link PetType}s. */ @Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name") - @Transactional(readOnly = true) List findPetTypes(); /** diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTests.java new file mode 100644 index 00000000000..1a153bcbc55 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTests.java @@ -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")); + } + + } + +}