본문 바로가기

SpringBoot/스파르타 스프링 심화

[스파르타 스프링 심화] 03.03 JUnit 을 이용한 단위 테스트

728x90
반응형

##JUnit 을 이용한 단위 테스트

1. 단위 테스트란?

: 프로그램을 작은 단위로 쪼개서 테스트 하는 것

 

-개발이 진행 될 수록 버그 처리비용이 늘어난다.

 

2.JUnit

: 테스트를 위한 자바 라이브러리

 

3.테스트 파일 생성

1) 파일 찾아서 

-> 마우스 우클릭 

-> generate -> Test

->하고 테스트 파일 생성

 

 

4. 상품 생성 테스트 코드

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.sparta_spring.sparta_springcore_week01.model;
 
import com.sparta_spring.sparta_springcore_week01.dto.ProductRequestDto;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
 
import static org.junit.jupiter.api.Assertions.*;
 
class ProductTest {
    @Test
    @DisplayName("정상 케이스")
    void createProduct_Normal() {
        // given
        // 이러한 정보가 주어젔다고 가정
        Long userId = 100L;
        String title = "오리온 꼬북칩 초코츄러스맛 160g";
        String image = "https://shopping-phinf.pstatic.net/main_2416122/24161228524.20200915151118.jpg";
        String link = "https://search.shopping.naver.com/gate.nhn?id=24161228524";
        int lprice = 2350;
 
        ProductRequestDto requestDto = new ProductRequestDto(title, image, link, lprice);
        // 주의: ProductRequestDto 인자 순서 클래스와 다르면 에러
 
        // when
        // 상품 객체 생성
        Product product = new Product(requestDto, userId);
 
        // then
        // 일치하는지 테스트
        assertNull(product.getId());
        assertEquals(userId, product.getUserId());
        assertEquals(title, product.getTitle());
        assertEquals(image, product.getImage());
        assertEquals(link, product.getLink());
        assertEquals(lprice, product.getLprice());
        assertEquals(0, product.getMyprice());
    }
}
cs
반응형