From 28c18d66bbc2535ba3e0705a9b354e6815d480dc Mon Sep 17 00:00:00 2001 From: dasomel Date: Thu, 28 May 2026 00:45:07 +0900 Subject: [PATCH] =?UTF-8?q?test:=20EgovBindingInitializer=20=EB=8B=A8?= =?UTF-8?q?=EC=9C=84=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?=EB=B0=8F=20Lombok=20annotationProcessorPaths=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit EgovBindingInitializer의 initBinder 동작을 검증하는 단위 테스트 4개를 추가한다. - 유효한 yyyy-MM-dd 날짜 문자열이 Date로 변환됨을 검증 - 잘못된 날짜 형식(yyyy/MM/dd) 입력 시 예외 발생을 검증 - StringTrimmerEditor가 등록되어 공백 제거 동작을 검증 - CustomDateEditor가 PropertyEditorRegistry에 등록됨을 검증 아울러 maven-compiler-plugin에 annotationProcessorPaths를 추가해 Lombok @Slf4j 등이 테스트 소스에서 정상 동작하도록 하고, maven-surefire-plugin의 skipTests를 하드코딩 true에서 ${skipTests} property로 변경해 커맨드라인 옵션(-DskipTests=false)으로 테스트를 실행할 수 있게 한다. --- pom.xml | 8 +- .../cmmn/web/EgovBindingInitializerTest.java | 133 ++++++++++++++++++ 2 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 src/test/java/egovframework/example/cmmn/web/EgovBindingInitializerTest.java diff --git a/pom.xml b/pom.xml index 076f810..76b8acc 100644 --- a/pom.xml +++ b/pom.xml @@ -160,6 +160,12 @@ maven-compiler-plugin ${java.version} + + + org.projectlombok + lombok + + @@ -179,7 +185,7 @@ org.apache.maven.plugins maven-surefire-plugin - true + ${skipTests} xml **/Abstract*.java diff --git a/src/test/java/egovframework/example/cmmn/web/EgovBindingInitializerTest.java b/src/test/java/egovframework/example/cmmn/web/EgovBindingInitializerTest.java new file mode 100644 index 0000000..e1d834c --- /dev/null +++ b/src/test/java/egovframework/example/cmmn/web/EgovBindingInitializerTest.java @@ -0,0 +1,133 @@ +/* + * Copyright 2008-2009 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 + * + * http://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 egovframework.example.cmmn.web; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.beans.PropertyEditor; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.PropertyEditorRegistry; +import org.springframework.web.bind.WebDataBinder; + +import lombok.extern.slf4j.Slf4j; + +/** + * EgovBindingInitializer 단위 테스트 + * + * @author 이백행 + * @since 2024-09-21 + */ +@Slf4j +class EgovBindingInitializerTest { + + private EgovBindingInitializer bindingInitializer; + + private WebDataBinder binder; + + @BeforeEach + void setUp() { + bindingInitializer = new EgovBindingInitializer(); + binder = new WebDataBinder(null); + bindingInitializer.initBinder(binder); + } + + @Test + @DisplayName("날짜 문자열 yyyy-MM-dd 형식을 Date로 변환한다") + void testDateEditorValidFormat() throws ParseException { + // given + final String dateStr = "2024-01-15"; + final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()); + final Date expected = sdf.parse(dateStr); + + // when + final PropertyEditor editor = binder.findCustomEditor(Date.class, null); + assertNotNull(editor); + editor.setAsText(dateStr); + final Date result = (Date) editor.getValue(); + + // then + assertEquals(expected, result); + + if (log.isDebugEnabled()) { + log.debug("dateStr={}", dateStr); + log.debug("result={}", result); + } + } + + @Test + @DisplayName("잘못된 날짜 형식(yyyy/MM/dd)은 변환에 실패한다") + void testDateEditorInvalidFormat() { + // given + final String invalidDateStr = "2024/01/15"; + final PropertyEditor editor = binder.findCustomEditor(Date.class, null); + assertNotNull(editor); + + // when & then + assertThrows(Exception.class, () -> editor.setAsText(invalidDateStr)); + + if (log.isDebugEnabled()) { + log.debug("invalidDateStr={}", invalidDateStr); + } + } + + @Test + @DisplayName("StringTrimmerEditor가 등록되어 문자열 앞뒤 공백을 제거한다") + void testStringTrimmerEditor() { + // given + final String paddedStr = " hello world "; + + // when + final PropertyEditorRegistry registry = binder; + final PropertyEditor editor = registry.findCustomEditor(String.class, null); + + // then + assertNotNull(editor); + + editor.setAsText(paddedStr); + final String result = (String) editor.getValue(); + assertEquals("hello world", result); + + if (log.isDebugEnabled()) { + log.debug("paddedStr='{}'", paddedStr); + log.debug("result='{}'", result); + } + } + + @Test + @DisplayName("Date PropertyEditor가 등록되어 있다") + void testDateEditorIsRegistered() { + // given & when + final PropertyEditorRegistry registry = binder; + final PropertyEditor editor = registry.findCustomEditor(Date.class, null); + + // then + assertNotNull(editor); + + if (log.isDebugEnabled()) { + log.debug("editor={}", editor); + } + } + +}