dorosy 2014. 12. 18. 17:00

JUnit : 함수를 테스트하는 클래스

 

 

1. poem.xml 에 JUnit 포함


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>

 

 

2. JUnitTest.class  파일에서


import org.junit.Before;
import org.junit.Test; // 단위 테스트 도구.

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;

class Calc{
private int x,y;
public void setX(int x){this.x = x;}
public void setY(int y){this.y = y;}

public int sum(){return x+y;}
public int sub(){return x-y;}
public int mul(){return x*y;}
}

public class JUnitTest2 {
Calc ob;
@Before
public void setUp(){ // 테스트하기 전에 전체 테스트를 준비하는 메소드.
ob = new Calc();
ob.setX(10);
ob.setY(20);
}
@Test
public void testSum(){assertThat(ob.sum(), is(30));}
@Test
public void testSub(){assertThat(ob.sub(), is(10));}
@Test
public void testMul(){assertThat(ob.mul(), is(200));}