# 테스트 코드 작성하기
## 미션
Article 서비스를 검증하는 테스트 코드를 작성하시오.
![홍팍-스프링-부트-입문-테스트-코드-미션](http://drive.google.com/thumbnail?export=view&sz=w960&id=1e9l1HiKjOH8-YM-PfM5l2ivwJaLylYyE)
## 02:51 테스트 코드 틀잡기 - @Test, @SpringBootTest
#### ../test/.../service/ArticleServiceTest
```
package com.example.firstproject.service;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class ArticleServiceTest {
@Autowired
ArticleService articleService;
@Test
void index() {
// 예상
// 실제
// 검증
}
}
```
## 05:14 index() 테스트
#### ../test/.../service/ArticleServiceTest
```
...
@SpringBootTest
class ArticleServiceTest {
...
@Test
void index() {
// 예상
Article a = new Article(1L, "가가가가", "1111");
Article b = new Article(2L, "나나나나", "2222");
Article c = new Article(3L, "다다다다", "3333");
List<Article> expected = new ArrayList<Article>(Arrays.asList(a, b, c));
// 실제
List<Article> articles = articleService.index();
// 검증
assertEquals(expected.toString(), articles.toString());
}
}
```
## 09:54 show() 테스트
#### ../test/.../service/ArticleServiceTest
```
...
@SpringBootTest
class ArticleServiceTest {
...
@Test
void show_성공____존재하는_id_입력() {
// 예상
Long id = 1L;
Article expected = new Article(id, "가가가가", "1111");
// 실제
Article article = articleService.show(id);
// 비교
assertEquals(expected.toString(), article.toString());
}
@Test
void show_실패____존재하지_않는_id_입력() {
// 예상
Long id = -1L;
Article expected = null;
// 실제
Article article = articleService.show(id);
// 비교
assertEquals(expected, article);
}
}
```
## 15:13 create() 테스트
#### ../test/.../service/ArticleServiceTest
```
...
@SpringBootTest
class ArticleServiceTest {
...
@Test
void create_성공____title과_content만_있는_dto_입력() {
// 예상
String title = "라라라라";
String content = "4444";
ArticleForm dto = new ArticleForm(null, title, content);
Article expected = new Article(4L, title, content);
// 실제
Article article = articleService.create(dto);
// 비교
assertEquals(expected.toString(), article.toString());
}
@Test
void create_실패____id가_포함된_dto_입력() {
// 예상
String title = "라라라라";
String content = "4444";
ArticleForm dto = new ArticleForm(4L, title, content);
Article expected = null;
// 실제
Article article = articleService.create(dto);
// 비교
assertEquals(expected, article);
}
}
```
## 21:27 테스트를 위한 트랜잭션 - @Transactional
#### ../test/../service/ArticleServiceTest
```
package com.example.firstproject.service;
import com.example.firstproject.dto.ArticleForm;
import com.example.firstproject.entity.Article;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class ArticleServiceTest {
...
@Test
@Transactional
void create_성공____title과_content만_있는_dto_입력() {
...
}
@Test
@Transactional
void create_실패____id가_포함된_dto_입력() {
...
}
}
```
## 🔥 구글링 훈련하기
- 테스트 코드란
- TDD란
- @SpringBootTest
- jUnit5 @Test