3 JUnit
3 JUnit
1
Junit (3.x and 4.x)
Test framework
test cases are Java code
test case = “sequence of operations +inputs + expected
values”
Test code
Production code
testDoubleOf2(){ //..
int doubleOf2(){ //… doubleOf2();
} //..
}
2
Framework elements
TestCase
Base class for classes that contain tests
assert*()
Method family to check conditions
TestSuite
Enables grouping several test cases
Testcase 1
Testsuite Testcase 2
Testcase 3
3
class Stack {
public boolean isEmpty(){ ... }
An example public void push(int i){ ... }
public int pop(){ ... }
…
}
import junit.framework.TestCase;
public class StackTester extends TestCase {
public StackTester(String name) {
super(name);
}
public void testStack() { Must begin with
Stack aStack = new Stack(); “test”
if(!aStack.isEmpty()) {
System.out.println(“Stack should be empty!”);
aStack.push(10);
aStack.push(-4);
System.out.println(“Last element:“ + aStack.pop());
System.out.println(“First element: “ +aStack.pop());
}
}
4
Method family to check conditions …
Assert*()
They are public methods defined in the base class
TestCase
Their names begin with “assert” and are used in test
methods
es. assertTrue(“stack should be empty”, aStack.empty());
If the condition is false:
test fails
execution skips the rest of the test method
the message (if any) is printed
If the condition is true:
execution continues normally
5
Assert*()
for a boolean condition
assertTrue(“message for fail”, condition);
assertFalse(“message”, condition);
for object, int, long, and byte values obtained
assertEquals(expected_value, expression);
for float and double values
assertEquals(expected, expression, error);
for objects references
assertNull(reference)
assertNotNull(reference)
…
6
http://junit.org/apidocs/org/junit/Assert.html
Assert: example
class Stack {
public boolean isEmpty(){ ... }
public void push(int i){ ... }
public int pop(){ ... }
…
}
7
Code Modularization …
One concept at a time …
8
TestSuite
junit.framework.*
Groups several test cases:
9
Test of “Exceptions”
10
We expect a normal behavior …
try {
// We call the method with correct parameters
object.method("Parameter");
assertTrue(true); // OK
} catch(PossibleException e){
fail(“method should not fail !!!");
}
class TheClass {
public void method(String p)
throws PossibleException
{ /*... */ }
}
11
We expect an exception …
try {
// we call the method with wrong parameters
object.method(null);
fail(“method should fail!!");
} catch(PossibleException e){
assertTrue(true); // OK
} class TheClass {
public void method(String p)
throws PossibleException
{ /*... */ }
}
12
SetUp() and tearDown()
ShoppingCart cart;
Book book;
protected void setUp() {
cart = new ShoppingCart();
book = new Book(“JUnit", 29.95);
cart.addItem(book);
}
…
13
Junit in eclipse - Setup
In Eclipse
Create a new project
Open project’s property
window (File ->
Properties)
Select: Java build path
Select: libraries
Add Library
Select Junit
Select the type 3.x or 4.x
14
Create a new JUnit test case
Eclipse Menu
File Edit Source Refactor Navigate Search Project Run Window Help
File
New
Junit Test Case
Set the parameters:
Junit 3.x or 4.x
name of the class
etc.
Finish
15
Run as JUnit Test
Eclipse Menu
File Edit Source Refactor Navigate Search Project Run Window Help
Run
Run As
Junit Test
16
Red / Green Bar
Fail
Pass
17
Test First Advantages
18