J unit스터디슬라이드


Published on

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

J unit스터디슬라이드

  1. 1. JUnitksa-in project study
  2. 2. 배경 지식
  3. 3. JUnit 이란?
  4. 4. JUnit 이란?Unit testing framework for Java
  5. 5. Test Phases• Unit testing on individual units of source code (= smallest testable part)• Integration testing on groups of individual software modules• System testing on a complete, integrated system (evaluate compliance with requirements)
  6. 6. TDD(Test Driven Development)• Software Development Process – Family of Agile• Test code를 작성하는 것이 소프트웨어 개 발의 시작!• Automated Test
  7. 7. TDD Process Refactor Add a test code Run all Run the tests andtests and see if thesee them new one succeed fails Write some code
  8. 8. Junit 4 뭐가 달라졌나?import static com.wakaleo.gameoflife.domain.Cell.DEAD_CELL;import static com.wakaleo.gameoflife.domain.Cell.LIVE_CELL;import static org.hamcrest.MatcherAssert.assertThat;import static org.hamcrest.Matchers.is; No need to extend TestCaseimport org.junit.Test;public class WhenYouCreateACell { Annotation based @Test public void aLiveCellShouldBeRepresentedByAnAsterisk() { Cell cell = Cell.fromSymbol("*"); assertThat(cell, is(LIVE_CELL)); } Call the tests anything you want @Ignore("Pending Implementation") Annotations for test metadata @Test public void aDeadCellShouldBeRepresentedByADot() { Cell cell = Cell.fromSymbol("."); assertThat(cell, is(DEAD_CELL)); } ...}
  9. 9. 일단 시작해봅시다!
  10. 10. 간단한 test
  11. 11. 간단한 test
  12. 12. 고쳐보자
  13. 13. 성공?
  14. 14. 좀 더 복잡한 테스트
  15. 15. 제대로 고치면 성공!
  16. 16. 일단 Test는 짰는데 구현하기는 어렵고…Test 할 때마다 빨간 막대기 보기 싫고…
  17. 17. @Before & @After• @Before는 해당 클래스의 각각의 test가 실행되기 전에 동작(setup)• @After는 해당 클래스의 각각의 test가 실행된 이후에 동작(tear down)• @Before public void runBeforeEveryTest() { simpleMath = new SimpleMath(); }• @After public void runAfterEveryTest() { simpleMath = null; }
  18. 18. @BeforeClass & @AfterClass• @BeforeClass는 해당 클래스의 test들이 시작되기 전 한 번 동작 (setup)• @AfterClass는 해당 클래스의 test가 모두 실행된 이후에 동작(tear down)• @BeforeClass public static void runBeforeClass() { // run for one time before all test cases }• @AfterClass public static void runAfterClass() { // run for one time after all test cases }
  19. 19. Exception Handling• @Test(expected = <Exception 종류>• 해당 Exception이 나야 test를 통과, Exception이 나지 않고 테스트가 끝나면 fail• @Test(expected = ArithmeticException.class) public void divisionWithException() { // divide by zero simpleMath.divide(1, 0); }
  20. 20. Timeout• @Test(timeout = <시간, ms단위>)• 해당 시간 내에 test가 끝나지 않으면 fail• @Test(timeout = 1000) public void infinity() { while (true) ; }
  21. 21. 유닛 테스트가 좋은건 알겠는데…모든 메소드를 테스트 해야되나?
  22. 22. 유닛 테스트가 좋은건 알겠는데…모든 메소드를 테스트 해야되나? GET, SET 메소드도?
  23. 23. 모범 답안• Code를 쓰기 전에 Test를 씁시다• 뭔가 깨질 껀덕지가 보이면 Test를 합시다.• get, set 메소드처럼 깨지기엔 너무 간단한 (too simple to break) 애들은 하지 말고요.• Test code를 짰으면 자주 실행시킵시다.
  24. 24. Test Double• Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.• Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).• Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside whats programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it sent, or maybe only how many messages it sent.• Mocks are objects pre-programmed with expectations which form a specification of the calls they are expected to receive.
  25. 25. Stub(1/2)• public interface MailService { public void send (Message msg); } public class MailServiceStub implements MailService { private List<Message> messages = new ArrayList<Message>(); public void send (Message msg) { messages.add(msg); } public int numberSent() { return messages.size(); } }
  26. 26. Stub(2/2)• class OrderStateTester... public void testOrderSendsMailIfUnfilled() { Order order = new Order(TALISKER, 51); MailServiceStub mailer = new MailServiceStub(); order.setMailer(mailer); order.fill(warehouse); assertEquals(1, mailer.numberSent()); }
  27. 27. Mock• class OrderInteractionTester... public void testOrderSendsMailIfUnfilled() { Order order = new Order(TALISKER, 51); Mock warehouse = mock(Warehouse.class); Mock mailer = mock(MailService.class); order.setMailer((MailService) mailer.proxy()); mailer.expects(once()).method("send"); warehouse.expects(once()).method("hasInventory") .withAnyArguments() .will(returnValue(false)); order.fill((Warehouse) warehouse.proxy()); } }
  28. 28. Reference• Unit testing with JUnit – http://www.slideshare.net/tom.zimmermann/unit- testing-with-junit• JUnit 4 in 60 seconds – http://www.cavdar.net/2008/07/21/junit-4-in-60- seconds/• Mocks aren’t stubs – http://martinfowler.com/articles/mocksArentStubs. html#TheDifferenceBetweenMocksAndStubs