75
Introduction to JUnit Nai-Wei Lin Department of Computer Science and Software Engineering National Chung Cheng University

Introduction to JUnit - ecourse2.ccu.edu.tw

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to JUnit - ecourse2.ccu.edu.tw

Introduction to JUnit

Nai-Wei Lin

Department of Computer Science and

Software Engineering

National Chung Cheng University

Page 2: Introduction to JUnit - ecourse2.ccu.edu.tw

2

Content

JUnit framework

Test classes and test methods

Assert methods

Test driven development

Page 3: Introduction to JUnit - ecourse2.ccu.edu.tw

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

Page 4: Introduction to JUnit - ecourse2.ccu.edu.tw

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

Page 5: Introduction to JUnit - ecourse2.ccu.edu.tw

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));

}

}

Page 6: Introduction to JUnit - ecourse2.ccu.edu.tw

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

Page 7: Introduction to JUnit - ecourse2.ccu.edu.tw

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

Page 8: Introduction to JUnit - ecourse2.ccu.edu.tw

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

Page 9: Introduction to JUnit - ecourse2.ccu.edu.tw

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

Page 10: Introduction to JUnit - ecourse2.ccu.edu.tw

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

Page 11: Introduction to JUnit - ecourse2.ccu.edu.tw

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() { … }

}

Page 12: Introduction to JUnit - ecourse2.ccu.edu.tw

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() { … }

}

Page 13: Introduction to JUnit - ecourse2.ccu.edu.tw

An Example

Create a new Java project “Complex”.

13

Page 14: Introduction to JUnit - ecourse2.ccu.edu.tw

14

Page 15: Introduction to JUnit - ecourse2.ccu.edu.tw

15

Page 16: Introduction to JUnit - ecourse2.ccu.edu.tw

16

Page 17: Introduction to JUnit - ecourse2.ccu.edu.tw

17

Page 18: Introduction to JUnit - ecourse2.ccu.edu.tw

An Example

Create a new source folder “test”.

18

Page 19: Introduction to JUnit - ecourse2.ccu.edu.tw

19

Page 20: Introduction to JUnit - ecourse2.ccu.edu.tw

20

Page 21: Introduction to JUnit - ecourse2.ccu.edu.tw

21

Page 22: Introduction to JUnit - ecourse2.ccu.edu.tw

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

Page 23: Introduction to JUnit - ecourse2.ccu.edu.tw

23

Page 24: Introduction to JUnit - ecourse2.ccu.edu.tw

24

Page 25: Introduction to JUnit - ecourse2.ccu.edu.tw

25

Page 26: Introduction to JUnit - ecourse2.ccu.edu.tw

26

Page 27: Introduction to JUnit - ecourse2.ccu.edu.tw

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

Page 28: Introduction to JUnit - ecourse2.ccu.edu.tw

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);

}

Page 29: Introduction to JUnit - ecourse2.ccu.edu.tw

29

Page 30: Introduction to JUnit - ecourse2.ccu.edu.tw

An Example

Automatically generate the frame of the class

“Complex” in source folder “src” using quick

fix.

30

Page 31: Introduction to JUnit - ecourse2.ccu.edu.tw

31

Page 32: Introduction to JUnit - ecourse2.ccu.edu.tw

32

Page 33: Introduction to JUnit - ecourse2.ccu.edu.tw

33

Page 34: Introduction to JUnit - ecourse2.ccu.edu.tw

34

Page 35: Introduction to JUnit - ecourse2.ccu.edu.tw

An Example

Automatically generate the frame of the

constructor Complex(double r, double i) in

source folder “src” using quick fix.

35

Page 36: Introduction to JUnit - ecourse2.ccu.edu.tw

36

Page 37: Introduction to JUnit - ecourse2.ccu.edu.tw

37

Page 38: Introduction to JUnit - ecourse2.ccu.edu.tw

38

Page 39: Introduction to JUnit - ecourse2.ccu.edu.tw

An Example

Implement Complex(double r, double i).

39

Page 40: Introduction to JUnit - ecourse2.ccu.edu.tw

40

Page 41: Introduction to JUnit - ecourse2.ccu.edu.tw

An Example

Create fields “re” and “im” using quick fix.

41

Page 42: Introduction to JUnit - ecourse2.ccu.edu.tw

42

Page 43: Introduction to JUnit - ecourse2.ccu.edu.tw

43

Page 44: Introduction to JUnit - ecourse2.ccu.edu.tw

44

Page 45: Introduction to JUnit - ecourse2.ccu.edu.tw

An Example

Automatically generate getters and setters.

45

Page 46: Introduction to JUnit - ecourse2.ccu.edu.tw

46

Page 47: Introduction to JUnit - ecourse2.ccu.edu.tw

47

Page 48: Introduction to JUnit - ecourse2.ccu.edu.tw

48

Page 49: Introduction to JUnit - ecourse2.ccu.edu.tw

49

Page 50: Introduction to JUnit - ecourse2.ccu.edu.tw

An Example

Automatically generate the frame of the

constructor Complex() in source folder “src”

using quick fix.

50

Page 51: Introduction to JUnit - ecourse2.ccu.edu.tw

51

Page 52: Introduction to JUnit - ecourse2.ccu.edu.tw

52

Page 53: Introduction to JUnit - ecourse2.ccu.edu.tw

53

Page 54: Introduction to JUnit - ecourse2.ccu.edu.tw

An Example

Implement Complex().

54

Page 55: Introduction to JUnit - ecourse2.ccu.edu.tw

55

Page 56: Introduction to JUnit - ecourse2.ccu.edu.tw

56

Page 57: Introduction to JUnit - ecourse2.ccu.edu.tw

An Example

Run As JUnit Test

Repeatly modify the constructor until it

passes the test.

57

Page 58: Introduction to JUnit - ecourse2.ccu.edu.tw

58

Page 59: Introduction to JUnit - ecourse2.ccu.edu.tw

59

Page 60: Introduction to JUnit - ecourse2.ccu.edu.tw

An Example

Use Complex(), getRe() and getIm() to test

equals().

Use Complex(), getRe() and getIm(), and

equals() to test other methods.

60

Page 61: Introduction to JUnit - ecourse2.ccu.edu.tw

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));

}

Page 62: Introduction to JUnit - ecourse2.ccu.edu.tw

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());

}

Page 63: Introduction to JUnit - ecourse2.ccu.edu.tw

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));

}

Page 64: Introduction to JUnit - ecourse2.ccu.edu.tw

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());

}

Page 65: Introduction to JUnit - ecourse2.ccu.edu.tw

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));

}

Page 66: Introduction to JUnit - ecourse2.ccu.edu.tw

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());

}

Page 67: Introduction to JUnit - ecourse2.ccu.edu.tw

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));

}

Page 68: Introduction to JUnit - ecourse2.ccu.edu.tw

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);

}

Page 69: Introduction to JUnit - ecourse2.ccu.edu.tw

An Example

69

c2 = new Complex();

try {

c3 = c1.div(c2);

assertFalse(“An exception should be thrown.”,

true);

} catch (DivByZeroException e) {

assertFalse(false);

}

}

Page 70: Introduction to JUnit - ecourse2.ccu.edu.tw

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);

}

Page 71: Introduction to JUnit - ecourse2.ccu.edu.tw

An Example

71

c2 = new Complex();

try {

c3 = c1.div(c2);

assertFalse(“An exception should be thrown.”,

true);

} catch (DivByZeroException e) {

assertFalse(false);

}

}

Page 72: Introduction to JUnit - ecourse2.ccu.edu.tw

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());

}

Page 73: Introduction to JUnit - ecourse2.ccu.edu.tw

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);

}

Page 74: Introduction to JUnit - ecourse2.ccu.edu.tw

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() { … }

}

Page 75: Introduction to JUnit - ecourse2.ccu.edu.tw

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 }

}