Introduction to JUnit
Nai-Wei Lin
Department of Computer Science and
Software Engineering
National Chung Cheng University
2
Content
JUnit framework
Test classes and test methods
Assert methods
Test driven development
JUnit Framework
JUnit is an open source framework that has
been designed for the purpose of writing and
running tests in the Java programming
language.
JUnit is a regression testing framework for
unit testing.
3
Test Classes and Test Methods
For each class C under test, define a test
class CTest.
Test classes inherit class TestCase in
package junit.framework.
For each method m under test, define a test
method testM.
Return type of a test method must be void.
Test method must not have any parameter.
Test method must not throw any exception.
4
An Example
5
class Calculator
{
public int sum(int n1, int n2) { return n1+n2; }
}
import junit.framework.TestCase;
class CalculatorTest extends TestCase {
public void testSum() { /* test sum()
Calculator cal = new Calculator();
assertEquals(2, cal.sum(1,1));
}
}
Assert Methods
assertEquals(expected, actual)If actual equals expected then does nothing; otherwise, fails.
assertEquals(message, expected, actual)If actual equals expected then does nothing; otherwise, fails and prints out message.
assertEquals(expected, actual, delta)If actual equals expected within delta then does nothing; otherwise, fails.
assertEquals(message, expected, actual, delta).
6
Assert Methods
assertFalse(condition)If condition is false then does nothing; otherwise, fails.
assertFalse(message, condition).
assertTrue(condition) If condition is true then does nothing; otherwise, fails.
assertTrue(message, condition).
7
Assert Methods
AssertNull(object)If object does not exist then does nothing; otherwise, fails.
AssertNull(message, object).
AssertNotNull(object)If object exists then does nothing; otherwise, fails.
AssertNotNull(message, object).
8
Assert Methods
AssertSame(expected, actual)If actual and expected refer to the same object then does nothing; otherwise, fails.
AssertSame(message, expected, actual)
AssertNotSame(expected, actual)If actual and expected don’t refer to the same object then does nothing; otherwise, fails.
AssertNotSame(message, expected, actual)
9
Test Driven Development
The test class is developed before the class
under test.
The development of the class under test
terminates if all test methods in the test class
succeed.
10
An Example
11
class Complex
{ // an abstract data type
private float re;
private float im;
public Complex() { … }
public Complex(float r, float i) { … }
public float getRe() { … }
public float getIm() { … }
public boolean equals(Complex c) { … }
public Complex add(Complex c) { … }
public Complex sub(Complex c) { … }
public Complex mul(Complex c) { … }
public Complex div(Complex c)
throws DivByZeroException { … }
public String toString() { … }
}
An Example
12
class ComplexTest extends TestCase {
public void testComplex() { … }
public void testEquals() { … }
public void testAdd() { … }
public void testSub() { … }
public void testMul() { … }
public void testDiv() { … }
public void testToString() { … }
…
}
An Example
Create a new Java project “Complex”.
13
14
15
16
17
An Example
Create a new source folder “test”.
18
19
20
21
An Example
Create a new JUnit Test Case “ComplexTest”
in source folder “test”.
If JUnit was not on the build path, then add
Junit on the build path.
22
23
24
25
26
An Example
getRe() and getIm() can be generated
automatically.
There is no need to test getRe() and getIm().
Change method “test” to “testComplex”.
Use getRe(), getIm(), and assertEquals() to
test constructors Complex().
27
An Example
28
public void testComplex() {
Complex c1 = new Complex();
Complex c2 = new Complex(2.0, 3.0);
assertEquals(0.0, c1.getRe(), 0.001);
assertEquals(0.0, c1.getIm(), 0.001);
assertEquals(2.0, c2.getRe(), 0.001);
assertEquals(3.0, c2.getIm(), 0.001);
}
29
An Example
Automatically generate the frame of the class
“Complex” in source folder “src” using quick
fix.
30
31
32
33
34
An Example
Automatically generate the frame of the
constructor Complex(double r, double i) in
source folder “src” using quick fix.
35
36
37
38
An Example
Implement Complex(double r, double i).
39
40
An Example
Create fields “re” and “im” using quick fix.
41
42
43
44
An Example
Automatically generate getters and setters.
45
46
47
48
49
An Example
Automatically generate the frame of the
constructor Complex() in source folder “src”
using quick fix.
50
51
52
53
An Example
Implement Complex().
54
55
56
An Example
Run As JUnit Test
Repeatly modify the constructor until it
passes the test.
57
58
59
An Example
Use Complex(), getRe() and getIm() to test
equals().
Use Complex(), getRe() and getIm(), and
equals() to test other methods.
60
An Example
61
public void testEquals() {
Complex c1 = new Complex();
Complex c2 = new Complex(2.0, 3.0);
Complex c3 = new Complex(0.0, 0.0);
assertFalse(c1.equals(c2));
assertTrue(c1.equals(c3));
}
An Example
62
public void testAdd() {
Complex c1 = new Complex(2.0, 3.0);
Complex c2 = new Complex(1.0, 1.0);
c3 = c1.add(c2);
assertEquals(3.0, c3.getRe());
assertEquals(4.0, c3.getIm());
}
An Example
63
public void testAdd() {
Complex c1, c2, c3, c4;
c1 = new Complex(2.0, 3.0);
c2 = new Complex(1.0, 1.0);
c3 = c1.add(c2);
c4 = new Complex(3.0, 4.0);
assertTrue(c4.equals(c3));
}
An Example
64
public void testSub() {
Complex c1, c2, c3;
c1 = new Complex(2.0, 3.0);
c2 = new Complex(1.0, 1.0);
c3 = c1.sub(c2);
assertEquals(1.0, c3.getRe());
assertEquals(2.0, c3.getIm());
}
An Example
65
public void testSub() {
Complex c1, c2, c3, c4;
c1 = new Complex(2.0, 3.0);
c2 = new Complex(1.0, 1.0);
c3 = c1.sub(c2);
c4 = new Complex(1.0, 2.0);
assertTrue(c4.equals(c3));
}
An Example
66
public void testMul() {
Complex c1, c2, c3;
c1 = new Complex(2.0, 3.0);
c2 = new Complex(1.0, 1.0);
c3 = c1.mul(c2);
assertEquals(-1.0, c3.getRe());
assertEquals(5.0, c3.getIm());
}
An Example
67
public void testMul() {
Complex c1, c2, c3, c4;
c1 = new Complex(2.0, 3.0);
c2 = new Complex(1.0, 1.0);
c3 = c1.mul(c2);
c4 = new Complex(-1.0, 5.0);
assertTrue(c4.equals(c3));
}
An Example
68
public void testDiv() {
Complex c1, c2, c3;
c1 = new Complex(2.0, 3.0);
c2 = new Complex(1.0, 1.0);
try {
c3 = c1.div(c2);
assertEquals(2.5, c3.getRe());
assertEquals(0.5, c3.getIm());
} catch (DivByZeroException e) {
assertTrue(“No exception should be thrown.”,
false);
}
An Example
69
c2 = new Complex();
try {
c3 = c1.div(c2);
assertFalse(“An exception should be thrown.”,
true);
} catch (DivByZeroException e) {
assertFalse(false);
}
}
An Example
70
public void testDiv() {
Complex c1, c2, c3;
c1 = new Complex(2.0, 3.0);
c2 = new Complex(1.0, 1.0);
try {
c3 = c1.div(c2);
c4 = new Complex(2.5, 0.5);
assertTrue(c4.equals(c3));
} catch (DivByZeroException e) {
assertTrue(“No exception should be thrown.”,
false);
}
An Example
71
c2 = new Complex();
try {
c3 = c1.div(c2);
assertFalse(“An exception should be thrown.”,
true);
} catch (DivByZeroException e) {
assertFalse(false);
}
}
An Example
72
public void testToString() {
Complex c1, c2;
c1 = new Complex();
assertEquals(“0.0+0.0i”, c1.toString());
c2 = new Complex(1.0, 1.0);
assertEquals(“1.0+1.0i”, c2.toString());
}
SetUp Method (Refactoring)
73
protected void setUp() { /* Initialize common objects
c1 = new Complex(2.0, 3.0);
c2 = new Complex(1.0, 1.0);
}
public void testAdd() {
Complex c3;
setUp();
c3 = c1.add(c2);
assertEquals(new Complex(3.0, 4.0), c3);
}
SetUp Method
74
class ComplexTest extends TestCase {
private Complex c1, c2;
public void testComplex() { … }
public void testEquals() { … }
public void testAdd() { … }
public void testSub() { … }
public void testMul() { … }
public void testDiv() { … }
public void testToString() { … }
protected void setUp() { … }
…
}
TearDown Method
75
class ComplexTest extends TestCase {
private Complex c1, c2;
public void testComplex() { … }
public void testEquals() { … }
public void testAdd() { … }
public void testSub() { … }
public void testMul() { … }
public void testDiv() { … }
public void testToString() { … }
protected void setUp() { … }
protected void tearDown() { /* Clean up }
…
}