-
Notifications
You must be signed in to change notification settings - Fork 0
2.5: 학습 테스트로 배우는 스프링 #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HyunJng
wants to merge
1
commit into
main
Choose a base branch
from
HyunJng
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| * 학습 테스트를 작성할 때의 장점이 무엇인가요? | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q. 학습 테스트를 작성할 때의 장점이 무엇인가요?