package com.example.firstproject.service;
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 java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest // 해당 클래스는 스프링부트와 연동되어 테스트됨
class ArticleServiceTest {
@Autowired ArticleService articleService;
@Test
void index() {
// 예상
Article a = new Article(1L, "aaaa", "1111");
Article b = new Article(2L, "bbbb", "2222");
Article c = new Article(3L, "cccc", "3333");
List<Article> expected = new ArrayList<Article>(Arrays.asList(a,b,c));
// 실제
List<Article> articles =articleService.index();
// 비교
assertEquals(expected.toString(), articles.toString());
}
}
@SpringBootTest
스프링부트와 연동해서 테스트
테스트할 서비스 클래스를 @Autowired
로 DI를 통해 가져옴
예상 데이터와 실제 데이터를 비교해준다
테스트 메소드 옆 재생 표시로 테스트해준다
테스트 성공 확인
@Test
void show_성공____존재하는_id_입력() {
//예상
Long id = 1L;
Article expected = new Article(id,"aaaa","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);
//비교 null은 toString을 호출할 수 없으니까 지워주자
assertEquals(expected,article);
}
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 javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest // 해당 클래스는 스프링부트와 연동되어 테스트됨
class ArticleServiceTest {
@Autowired ArticleService articleService;
@Test
void index() {
// 예상
Article a = new Article(1L, "aaaa", "1111");
Article b = new Article(2L, "bbbb", "2222");
Article c = new Article(3L, "cccc", "3333");
List<Article> expected = new ArrayList<Article>(Arrays.asList(a,b,c));
// 실제
List<Article> articles =articleService.index();
// 비교
assertEquals(expected.toString(), articles.toString());
}
@Test
void show_성공____존재하는_id_입력() {
//예상
Long id = 1L;
Article expected = new Article(id,"aaaa","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);
//비교 null은 toString을 호출할 수 없으니까 지워주자
assertEquals(expected,article);
}
@Test
@Transactional
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
@Transactional
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);
}
}
@Transactional
어노테이션을 붙여줌