05-JUnit.pdf - Test with JUnit Version 2 April 2013 \u00a9 Maurizio Morisio Marco Torchiano 2013 Test cases can be Informal undocumented \u2013 Also called

05-JUnit.pdf - Test with JUnit Version 2 April 2013 u00a9...

This preview shows page 1 - 7 out of 21 pages.

1 Test with JUnit Version 2 - April 2013 © Maurizio Morisio, Marco Torchiano, 2013 Test cases can be Informal, undocumented – Also called ‘informal testing’ Documented As text, tables, pseudo code As source code (usually in same language as tested code)
Image of page 1
2 Testing with JUnit JUnit is a testing framework for Java programs Idea of Kent Beck It is a framework with unit-testing functionalities Integrated in Eclipse development Environment What JUnit does (1) JUnit runs a suite of tests and reports results For each test method in the test suite: JUnit calls the method If the methods terminates without problems The test is considered passed.
Image of page 2
3 Example Test Stack class Stack public class Stack{ public Stack(){ //… } public boolean isEmpty(){ } public int pop(){ } public void push(int value){ }
Image of page 3
4 High level test cases T1 Create stack Stack empty Push(10) Stack not empty T2 Create stack Push(10) Push(-4) Pop() -4 Pop() 10 Pop() empty Junit test cases public void testStackT1() { Stack aStack = new Stack(); assertTrue( Stack should be empty! , aStack.isEmpty()); aStack.push(10); assertTrue( Stack should not be empty! , !aStack.isEmpty()); } public void testStackT2() { Stack aStack = new Stack(); aStack.push(10); aStack.push(-4); assertEquals(-4, aStack.pop()); assertEquals(10, aStack.pop()); }
Image of page 4
5 Junit test cases public class StackTest extends TestCase { public void testStackT1() { Stack aStack = new Stack(); assertTrue( Stack should be empty! , aStack.isEmpty()); aStack.push(10); assertTrue( Stack should not be empty! , !aStack.isEmpty()); } public void testStackT2() { Stack aStack = new Stack(); aStack.push(10); aStack.push(-4); assertEquals(-4, aStack.pop()); assertEquals(10, aStack.pop()); } } Extends TestCase Test method name: test Something Execution Junit executes each test...() method in the Test class If the tests run correctly, nothing is done If a test fails, it throws an AssertionFailedError The JUnit framework catches the error and deals with it; you don t have to do anything
Image of page 5
6 Execution (2) setUp() method is run before each test..() method Useful to create the needed clean context for the test tearDown() method is run after Useful to clean up, when needed They can be override protected void runTest() { setUp() ; test…(); tearDown() ; }
Image of page 6
Image of page 7

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture