From db39bf3a2765dc69b4c99f22ce15edbaf3bb692f Mon Sep 17 00:00:00 2001 From: HyunJng Date: Sat, 14 Feb 2026 03:16:59 +0900 Subject: [PATCH] docs: 2.5 --- ...4\355\212\270_\354\240\201\354\232\251.md" | 0 ...4_\354\212\244\355\224\204\353\247\201.md" | 111 ++++++++++++++++++ 2 files changed, 111 insertions(+) rename "chapter01/2.4_\354\212\244\355\224\204\353\247\201_\355\205\214\354\212\244\355\212\270_\354\240\201\354\232\251.md" => "chapter02/2.4_\354\212\244\355\224\204\353\247\201_\355\205\214\354\212\244\355\212\270_\354\240\201\354\232\251.md" (100%) create mode 100644 "chapter02/2.5_\355\225\231\354\212\265_\355\205\214\354\212\244\355\212\270\353\241\234_\353\260\260\354\232\260\353\212\224_\354\212\244\355\224\204\353\247\201.md" diff --git "a/chapter01/2.4_\354\212\244\355\224\204\353\247\201_\355\205\214\354\212\244\355\212\270_\354\240\201\354\232\251.md" "b/chapter02/2.4_\354\212\244\355\224\204\353\247\201_\355\205\214\354\212\244\355\212\270_\354\240\201\354\232\251.md" similarity index 100% rename from "chapter01/2.4_\354\212\244\355\224\204\353\247\201_\355\205\214\354\212\244\355\212\270_\354\240\201\354\232\251.md" rename to "chapter02/2.4_\354\212\244\355\224\204\353\247\201_\355\205\214\354\212\244\355\212\270_\354\240\201\354\232\251.md" diff --git "a/chapter02/2.5_\355\225\231\354\212\265_\355\205\214\354\212\244\355\212\270\353\241\234_\353\260\260\354\232\260\353\212\224_\354\212\244\355\224\204\353\247\201.md" "b/chapter02/2.5_\355\225\231\354\212\265_\355\205\214\354\212\244\355\212\270\353\241\234_\353\260\260\354\232\260\353\212\224_\354\212\244\355\224\204\353\247\201.md" new file mode 100644 index 0000000..490eb70 --- /dev/null +++ "b/chapter02/2.5_\355\225\231\354\212\265_\355\205\214\354\212\244\355\212\270\353\241\234_\353\260\260\354\232\260\353\212\224_\354\212\244\355\224\204\353\247\201.md" @@ -0,0 +1,111 @@ +# 2.5. 학습 테스트로 배우는 스프링 +## 1. 학습 테스트란? +자신이 만들지 않는 프렝임워크나 라이브러리 등에 대해 기능을 익히기 위해 테스트를 작성하는 것 +* 장점: + * 다양한 조건에 따른 기능을 테스트할 수 있다. + * 테스트 코드는 계속 남아있기에 개발 중에 참조할 수 있다 + * 프레임워크나 제품을 업그레이드할 때 호환성 검증을 도와준다 + * 테스트 작성에 대한 좋은 훈련이 된다. + +## 2. 학습 테스트 예제 +## 2.1. JUnit 테스트 오브젝트가 매번 새롭게 생성되는지 테스트 +### 1) 첫번째 시도 +```java +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import org.junit.Test; + +public class JUnitTest { + static JUnitTest jUnitTest; + + @Test + public void testSameObject1() { + assertThat(this, is(not(sameInstance(jUnitTest)))); + jUnitTest = this; + } + + @Test + public void testSameObject2() { + assertThat(this, is(not(sameInstance(jUnitTest)))); + jUnitTest = this; + } +} +``` +* 문제: 직전 테스트하고만 비교하기에 3개 이상이면 A!=B, B!=C, C!=A가 성립하지 않는다. + +### 2) 두번째 시도 +```java +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import org.junit.Test; + +public class JUnitTest2 { + static Set jUnitTests = new HashSet<>(); + + @Test + public void testSameObject1() { + assertThat(jUnitTests, not(hasItem(this))); + jUnitTests.add(this); + } + + @Test + public void testSameObject2() { + assertThat(jUnitTests, not(hasItem(this))); + jUnitTests.add(this); + } +} +``` + +## 2.2. 스프링 테스트 컨텍스트가 딱 한 개만 생성되는지 테스트 +```java +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import java.util.HashSet; +import java.util.Set; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations="/applicationContext.xml") +public class SpringContextTest { + @Autowired + private ApplicationContext context; + + static Set testObjects = new HashSet<>(); + static ApplicationContext contextObject = null; + + @Test + public void testSameObject1() { + assertThat(testObjects, not(hasItem(this))); + testObjects.add(this); + + assertThat(contextObject == null || contextObject == this.context, is(true)); + contextObject = this.context; + } + + @Test + public void testSameObject2() { + assertThat(testObjects, not(hasItem(this))); + testObjects.add(this); + + assertThat(contextObject == null || contextObject == this.context, is(true)); + contextObject = this.context; + } +} +``` + +## 3. 버그 테스트란? +실제 버그가 발생했을 때 그 버그를 재현하는 테스트 코드를 작성하는 것 +* 장점: + * 기존 테스트에서 검증하지 못한 부분을 보완할 수 있다. + * 버그를 재현하는 과정을 통해 문제의 원인을 파악할 수 있다. + * 버그 수정 후에 동일한 문제가 재발하지 않도록 방지할 수 있다. + * 버그 수정이 올바르게 이루어졌는지 검증할 수 있다. + +--- +## QnA +* 학습 테스트를 작성할 때의 장점이 무엇인가요? \ No newline at end of file