개발/Spring
[ JUnit ] JUnit Test
쩨이엠
2021. 1. 20. 14:24
728x90
반응형
SpringbootTest 어노테이션을 사용하다보면 굳이 서버를 올릴 필요 없이 간단하게 Test를 하고 싶을 때가 있다
예를 들면 정말 간단한 테스트코드 작성할 때
정말 Test만을 위한 프로젝트를 만들기로 한다
pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
package를 같이 해놔야 Test를 할 때 어려움이 없다
고 하지만 내 경우는 package도 default로 잡아놓았다 정말 자바코드를 위한 Test
import static org.junit.Assert.*;
import org.junit.Test;
public class ServiceTest {
@Test
public void example(){
String pattern = "01(0|1)-\\d{3,4}-\\d{4}";
assertTrue("010-8282-1234".matches(pattern));
assertTrue("010-828-1234".matches(pattern));
assertFalse("011-12-1234".matches(pattern));
}
@Test
public void example1(){
String pattern = "ab";
String str = "abcabcdabcde";
System.out.println(str.replaceAll(pattern, "*"));
}
}
static으로 import를 해놓으면 assertTrue, assertFalse등을 바로 사용할 수 있다
테스트를 돌리면 Test 어노테이션이 붙은 함수명이 보인다
끝 -
728x90
반응형