Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions chapter02/2.5_학습_테스트로_배우는_스프링.md
Original file line number Diff line number Diff line change
@@ -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<JUnitTest> 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<JUnitTest> 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
* 학습 테스트를 작성할 때의 장점이 무엇인가요?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q. 학습 테스트를 작성할 때의 장점이 무엇인가요?

  • 다양한 조건에 따른 기능 테스트를 할 수 있습니다.
  • 테스트 코드는 계속 남아있으므로 개발 중에 참조할 수 있습니다.
  • 프레임워크나 제품을 업데이트할 때 호환성 검증에 도움을 줍니다.
  • 테스트 작성에 대한 훈련이 됩니다.