[Spring Boot] 테스트 코드 작성

Posted by 김성철

스프링부트 - 테스트코드 작성

참고 URL :  
	https://m.blog.naver.com/ssuyastory/221644575256  
	https://velog.io/@swchoi0329/Spring-Boot-%ED%85%8C%EC%8A%A4%ED%8A%B8-%EC%BD%94%EB%93%9C-%EC%9E%91%EC%84%B1  
	https://hirlawldo.tistory.com/39  

라이브러리 추가

그래들에 아래의 내용을 추가  
=================================================================================================================  
dependencies {  
		...........  
  
	//junit 사용을 위해 추가, spring-boot-starter-test를 사용한다면 아래와 같이 추가  
	testImplementation('org.springframework.boot:spring-boot-starter-test') {  
		exclude module: 'junit'  
	}  
  
	//junit 사용을 위해 추가,  
	testImplementation('org.junit.jupiter:junit-jupiter-api:5.2.0')  
	testCompile('org.junit.jupiter:junit-jupiter-params:5.2.0')  
	testRuntime('org.junit.jupiter:junit-jupiter-engine:5.2.0')  
  
}  
  
test {  
  
  useJUnitPlatform()  
  
}  
=================================================================================================================  

IfbuyApplicationTests 클래스 수정

경로 :  
	src₩test₩java₩com₩sungchul₩ifbuy  
  
아래와 같이 수정  
※ 기본적으로 설정이 아래와 같이 되어 있음 (test / java / 패키지 )  
※ 클래스명은 프로젝트생성할때 자동적으로 생성이됨  
=================================================================================================================  
	package com.sungchul.ifbuy;  
  
	import org.junit.jupiter.api.Test;  
	import org.springframework.boot.test.context.SpringBootTest;  
  
	@SpringBootTest  
	class IfbuyApplicationTests {  
  
		@Test  
		void contextLoads() {  
		}  
  
	}  
=================================================================================================================  

테스트를 실행할 클래스 생성

경로 : ₩src₩test₩java₩com₩sungchul₩ifbuy₩controller₩  
파일명 : CoinControllerTest.java  
  
클래스명 위에 @SpringBootTest  추가  
테스트할 메소드명 위에 @Test  추가 ( org.junit.jupiter.api)  
실행 순서를 정할때 메소드명 위에 @Order(2) 추가  
아래와 같이 수정  
* CoinControllerTest1 , CoinService 는 컨트롤러와 서비스에 있는 클래스  
=================================================================================================================  
  
	package com.sungchul.ifbuy.controller;  
  
	import com.sungchul.ifbuy.coin.controller.CoinControllerTest1;  
	import com.sungchul.ifbuy.coin.service.CoinService;  
	import com.sungchul.ifbuy.coin.vo.CoinVO;  
	import lombok.extern.slf4j.Slf4j;  
	import org.junit.jupiter.api.Test;  
	import org.springframework.beans.factory.annotation.Autowired;  
	import org.springframework.boot.test.context.SpringBootTest;  
	import org.springframework.core.annotation.Order;  
	import org.springframework.web.bind.annotation.RestController;  
  
	import static org.assertj.core.api.Assertions.assertThat;  
  
	@SpringBootTest  
	@RestController  
	@Slf4j  
	public class CoinControllerTest {  
  
		@Autowired  
		CoinControllerTest1 coinControllerTest1;  
  
		@Autowired  
		CoinService coinServices;  
  
		@Test  
		@Order(2)  
		void test(){  
			assertThat(coinControllerTest1.test().equals("test~~~~"));  
		}  
  
		@Test  
		@Order(1)  
		void test2(){  
			CoinVO coinVO = new CoinVO();  
			coinVO.setMarket("KRW-BTC");  
			log.info("#### price : {}",coinServices.getCoinPrice(coinVO).toString());  
  
		}  
  
	}  
  
=================================================================================================================  
  
assertThat 메소드 참고 URL :  
	https://m.blog.naver.com/PostView.nhn?blogId=simpolor&logNo=221327833587&proxyReferer=https:%2F%2Fwww.google.com%2F  

해당 테스트 클래스를 실행하면 테스트가 완료됨