From 0202d131ab108c1befe114c019ace16ea224cc5a Mon Sep 17 00:00:00 2001 From: Auto_EPMD-EDP AIAssistant Date: Fri, 25 Oct 2024 17:01:53 +0300 Subject: [PATCH] Add unit test for validate method in PetValidator class --- .../petclinic/owner/PetValidatorTest.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTest.java diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTest.java b/src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTest.java new file mode 100644 index 000000000..1257b27d9 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTest.java @@ -0,0 +1,56 @@ +/* + * Copyright 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.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.validation.BeanPropertyBindingResult; +import org.springframework.validation.Errors; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(MockitoExtension.class) +@DisplayName("PetValidator Tests") +class PetValidatorTest { + + @InjectMocks + private PetValidator petValidator; + + private Pet pet; + + private Errors errors; + + @BeforeEach + void setUp() { + pet = new Pet(); + errors = new BeanPropertyBindingResult(pet, "pet"); + } + + @Test + @DisplayName("Validate Pet with missing fields") + void testValidatePetWithMissingFields() { + petValidator.validate(pet, errors); + + assertThat(errors.getFieldError("name").getCode()).isEqualTo("required"); + assertThat(errors.getFieldError("type").getCode()).isEqualTo("required"); + assertThat(errors.getFieldError("birthDate").getCode()).isEqualTo("required"); + } + +}