From aeca8aea5d126804a64cb140fda0d82f3fcdcea0 Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Sun, 31 Dec 2023 16:23:52 +0100 Subject: [PATCH] Fix resource leak --- .../petclinic/model/ValidatorTests.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java b/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java index fd011e5c4..af2cc1a81 100644 --- a/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java +++ b/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java @@ -8,6 +8,8 @@ import jakarta.validation.ConstraintViolation; import jakarta.validation.Validator; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; @@ -19,10 +21,17 @@ */ class ValidatorTests { - private Validator createValidator() { - LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean(); + private LocalValidatorFactoryBean localValidatorFactoryBean; + + @BeforeEach + void setUp() { + localValidatorFactoryBean = new LocalValidatorFactoryBean(); localValidatorFactoryBean.afterPropertiesSet(); - return localValidatorFactoryBean.getValidator(); + } + + @AfterEach + void tearDown() { + localValidatorFactoryBean.close(); } @Test @@ -42,4 +51,8 @@ void shouldNotValidateWhenFirstNameEmpty() { assertThat(violation.getMessage()).isEqualTo("must not be empty"); } + private Validator createValidator() { + return localValidatorFactoryBean.getValidator(); + } + }