2007년 9월 17일 월요일

Eclipse JUnit 사용하기

드디어 JUnit Test를 써보았다.

써봐야지 써봐야지 하면서 안쓰고 있다가..

써봤는데.. 참 이게 재미있기도 하고, 정말 유용한거 같더라.



우선 테스트케이스를 생성하고 싶은 프로렉트에 오른쪽 버튼을 누르고, JUnit 선택


사용자 삽입 이미지



그 후에 클래스 이름을 써주고, 아래 체크할수 있는 란에 보면, setUp(), tearDown() 에 체크를 하여,

오버라이딩을 해준다.


사용자 삽입 이미지


그리고 클래스가 만들어지면 메소드 이름을 테스트 하려는 메소드 이름앞에 test를 붙여준다.

ex) 테스트 하려는 메소드: removeNull
     JUnit  메소드 : testRemoveNull


그후 JUnit 의 테스트 메소드인 assertEquals 를 이용하여 메소드를 만든다.

실행은 단축키인 Alt + Shift + X , T 를 누르면 된다.

아래 예제 코드를 첨부한다.


[code type=java5]
import junit.framework.TestCase;

public class CommonUtilTest extends TestCase {

 CommonUtil commonUtil;

public static void main(String[] args) {
  junit.textui.TestRunner.run(CommonUtilTest.class);
 }

 protected void setUp() throws Exception {
  super.setUp();
 }

 protected void tearDown() throws Exception {
  super.tearDown();
 }

 /*
  * Test method for 'CommonUtil.removeNull(String)'
  */
 public void testRemoveNull() {
  assertEquals("parameter remove Null Return String",
      "Test Abc",
      commonUtil.removeNull("Test Abc"));
 }
[/code]

댓글 없음:

댓글 쓰기