|
1. 단위 테스트 구조화
■ 테스트에 필요한 모든 조건과 상황을 준비 설정한다.(필요한 객체를 모두 생성하기, 필요한 자원을 모두 할당하기 등) ■ 테스트 대상이 되는 메서드를 호출 ■ 테스트 대상이 되는 메서드가 원하는 대로 동작한다는 것을 검증한다. ■ 리팩토링 2. JUnit의 단정 메서드 ■ assertEquals assertEquals([String message], expected, actual, tolerance) 예) assertEquals("Should be 3 1/3", 3.33, 10.0/3.0, 0.01); ■ assertNull assertNull([String message], java.lang.Object object) assertNotNull([String message], java.lang.Object object) 인자로 넘겨받은 객ㄹ체가 null인지(또는 null이 아닌지) 판정하고, 반대인 경우 실패로 처리. ■ assertSame assertSame([String message], expected, actual) assertNotSame([String message], expected, actual) expected와 actual이 같은 객체를 참조하는지 아닌지를 판정. ■ assertTrue assertTrue([String message], boolean condition) assertFalse([String message], boolean condition) boolean 조건이 참인지 아닌지 판정 ■ fail fail([String message]) 테스트를 바로 실패처리. 절대 수행되지 않아야 될 부분(예외가 발생되는 부분 다음)을 표시하는데 사용. 3. JUnit 프레임워크 import junit.framework.TestCase; public class LargestTest extends TestCase { private Largest largest; /* * 각각의 테스트 메서드들이 실행되기 전에 호출된다. */ protected void setUp() throws Exception { largest = new Largest(); } /* * 각각의 테스트 메서드들이 실행되고 난 다음에 호출된다. */ protected void tearDown() throws Exception { super.tearDown(); } /* * Test method for 'tdd.junit.ch2.Largest.largest(int[])' */ public void testLargest() { assertEquals(9, largest.largest(new int[] {9,8,7})); assertEquals(-7, largest.largest(new int[] {-9,-8,-7})); } } 4. JUnit 테스트 조합 여러개의 테스트 케이스를 통합한 것을 테스트 스위트(TestSuite)라고 한다. 테스트 스위트를 작성해 두면 여러개의 테스트 케이스를 한 번에 실행시킬 수 있다. import junit.framework.Test; import junit.framework.TestSuite; public class AllTests { public static Test suite() { TestSuite suite = new TestSuite("Test for tdd.junit"); //$JUnit-BEGIN$ suite.addTestSuite(TestJukebox.class); suite.addTestSuite(TestMyStack.class); //$JUnit-END$ return suite; } } 5. JUnit 사용자 정의 단정 메서드 경우에 따라 사용자 정의 단정 메서드를 만들어야 할 경우 작성. import junit.framework.*; /** * Project-wide base class for Testing */ public class ProjectTest extends TestCase { /** * Assert that the amount of money is an even * number of dollars (no cents) * * @param message Text message to display if the assertion fails * @param amount Money object to test */ public void assertEvenDollars(String message, Money amount) { assertEquals(message, amount.asDouble() - (int)amount.asDouble(), 0.0, 0.001); } /** * Assert that the amount of money is an even * number of dollars (no cents) * * @param amount Money object to test */ public void assertEvenDollars(Money amount) { assertEvenDollars("", amount); } } 이를 사용하기 위해서는 TestCaset를 바로 상속하지 않고 이 기반 클래스를 상속하여 사용. 6. JUnit과 예외 [테스트 메서드내 예외 선언] public void testForException() { try{ sortMyList(null); fail("Should have thrown an exception"); /* 예외발생시 실패 선언 */ } catch(RuntimeException e) { assertTrue(true); /* 성공처리 선언 - "제어 흐름이 반드시 여기를 지나야 한다"는 문서화 역할 */ } } [테스트 메서드 예외 선언] public void testData1 throws FileNotFoundException { FileInputStream in = new FileInputStream("data.txt"); }
|
메모장
이글루 파인더
카테고리
전체
Agile Methodology Design Patterns JAVA Frameworks Open API DB관련 개발Tool System Web Server HTML/SCRIPT Talk Talk My Life 생활의 발견 허슬 두산! Fun Fun!!! 즐겨찾기 준비중 방명록 최근 등록된 덧글
우하하-_-' 유용해서 ..
by 미나 at 11/20 함께 생각하고 행동할 수.. by 강민철 at 10/21 신입개발자 인데 미흡한.. by seo at 10/13 유용한 정보 감사합니다.. by Nerv\\\'s at 09/23 저도 좋은글 담아갑니다.. by 풍 at 09/18 ㅎㅎㅎㅎ 저는 지금 개.. by 이종화 at 08/19 디자인패턴 공부하려는데.. by 이현승 at 07/22 좋은 글 잘 봤습니다. .. by 김진학 at 06/29 좋은글 감사합니다. 유.. by 엄성권 at 03/11 좋은 글 보고 갑니다. 제.. by 우기 at 01/10 최근 등록된 트랙백
Soma buy soma chea..
by Watson soma cheap. Buy soma online. by Buying cheap soma f.. Retin a. by Retin a and lip rhytid.. joinc wiki::소프트웨어.. by yundream의 프로그래.. 내 손안의 PC - 자바가 .. by 上善若水 저도 agile에 관심이 많.. by lovelove님의 이글루 JUnit 4로 뛰어들기 by 고안해 내는 재능 inheritnace vs dele.. by Bullshit Life framework = framewor.. by 고안해 내는 재능 Struts iBATIS Spring by 고안해 내는 재능 포토로그
이전블로그
이글루링크
라이프로그
| |||