Skip to content

Commit

Permalink
Add new test file
Browse files Browse the repository at this point in the history
-<modify>: remove `@Transactional`.
-<add>: create unit-test file related to `PetValidator`.
-<refactor>: move pet objects initialization to `@BeforeEach` setup.
  • Loading branch information
wickdynex authored and dsyer committed Nov 28, 2024
1 parent 214a8fb commit 40a4137
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public interface OwnerRepository extends JpaRepository<Owner, Integer> {
* @return a Collection of {@link PetType}s.
*/
@Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name")
@Transactional(readOnly = true)
List<PetType> findPetTypes();

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}

}

}

0 comments on commit 40a4137

Please sign in to comment.