1 junit. 2 unit testing with junit if code has no automated test case written for it to prove that...

22
1 JUnit

Upload: lester-stevens

Post on 19-Jan-2018

213 views

Category:

Documents


0 download

DESCRIPTION

3 Don't have the time for unit test? Productivity is directly related to the stability of your code base. – The fewer test cases you write, the less stable your code base becomes. – You spend all your time fixing unintended side effects and bugs.

TRANSCRIPT

Page 1: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

1

JUnit

Page 2: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

2

Unit Testing with JUnit

• If code has no automated test case written for it to prove that it works, it must be assumed not to work.

• An API that does not have an automated test case to show how it works must be assumed un-maintainable.

• Software without automated test cases cannot be economically refactored. Software that that cannot be refactored cannot be extended economically.

Page 3: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

3

Don't have the time for unit test?

• Productivity is directly related to the stability of your code base.– The fewer test cases you write, the less s

table your code base becomes.– You spend all your time fixing unintende

d side effects and bugs.

Page 4: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

4

Where to Get JUnit

• JUnit was written by Erich Gamma and Kent Beck.

• http://www.junit.org/index.htm

Page 5: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

5

System.out.println is Not Enough

• It is hard to tell if a complex system is working because so many System.out.println methods are printing so much garbage.

• Second, you must determine if something works by looking at a String scrolling by on a console. The string of text scrolling by may make sense the day you wrote it, but will it still make sense in three months?

• Third, when you make changes, things can break in unexpected ways.

Page 6: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

6

Overview of JUnit

• JUnit is a framework for writing unit tests.• A test case defines a fixture to run a

related set of tests. Typically, every class that you write should have a test case.

• A test fixture provides resources: primitive variables and objects that tests need to run.

• A test suite is a collection of related test cases.

Page 7: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

7

Practice• 1. Subclass junit.framework.TestCase.• 2. If we need fixture objects, override the setUp

() method.• 3. Define a number of tests that return void and

whose method name begins with test, such as testAdd(), testPut(), and testIterator().

• 4. If we need to release resources that were part of the fixture, override the tearDown() method.

• 5. If we need to group a related set of test cases, define a suite of tests.

Page 8: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

8

An examplepackage xptoolkit.junit.example;

import junit.framework.*;import java.util.Map;import java.util.HashMap;import junit.extensions.*;public class HashMapTest extends TestCase {

private Map testMap; private Map testMap2; public HashMapTest(String name) { super(name); }

public static Test suite() { return new TestSuite(HashMapTest.class); }

Page 9: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

9

public static void main (String[] args) { junit.textui.TestRunner.run (suite()); } private static final String APPLE_KEY = "AppleCEO"; private static final String APPLE_VALUE = "AppleCEO"; protected void setUp() { testMap = new HashMap(); testMap.put(APPLE_KEY, APPLE_VALUE); testMap.put("OracleCEO","Larry Ellison"); testMap2 = new HashMap(); testMap2.put("1", "1"); testMap2.put("2", "2"); }

Page 10: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

10

public void testPut(){ String key = "Employee"; String value = "Rick Hightower"; //put the value in testMap.put(key, value); //read the value back out String value2 = (String)testMap.get(key); assertEquals("The value back from the map ", value, value2); } public void testSize(){ assertEquals (2, testMap.size()); } public void testGet(){ assertEquals(APPLE_VALUE, testMap.get(APPLE_KEY)); assertNull(testMap.get("JUNK_KEY")); }

Page 11: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

11

public void testPutAll(){ testMap.putAll(testMap2); assertEquals (4, testMap.size()); assertEquals("1", testMap.get("1")); testGet(); } public void testContainsKey(){ assert("It should contain the apple key", testMap.containsKey(APPLE_KEY)); }

public void testContainsValue(){ assert(testMap.containsKey(APPLE_VALUE)); }

Page 12: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

12

public void testRemove(){ String key = "Employee"; String value = "Rick Hightower"; //put the value in testMap.put(key, value); //remove it testMap.remove(key); //try to read the value back out assertNull(testMap.get(key));

} }

Page 13: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

13

Explanation 1

• Step 1 is to define a class that derives junit.framework.

import junit.framework.*;...public class HashMapTest extends TestCase {

Page 14: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

14

Explanation 2

• Next, if our test case needs a fixture, we override the setUp() method

protected void setUp() { testMap = new HashMap(); testMap.put(APPLE_KEY, APPLE_VALUE); testMap.put("OracleCEO","Larry Ellison"); testMap2 = new HashMap(); testMap2.put("1", "1"); testMap2.put("2", "2"); }

the fixture the test case sets up is actually instances of the class under test: the HashMapclass. Garbage-collection will destroy the object.

Page 15: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

15

Explanation 3

• The HashMapTest class defines several tests to test the HashMap class.

• The JUnit framework uses reflection to look for methods whose names begin with test and uses them as test cases.

Page 16: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

16

Explanation 4

• It does this when we invoke the TestSuite constructor in the static suite() method,

public static Test suite() { return new TestSuite(HashMapTest.class); }

the composite design pattern

Page 17: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

17

Explanation 5

• assertEquals()• assertTrue()

Page 18: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

18

Explanation 6

• Note that the setUp() and tearDown() methods are called before and after every textX() method that is run.

• Because the setUp() method does not allocate any resources that need to be released, the HashMapTest does not need to override the tearDown() method.

Page 19: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

19

JUnit and Ant

• Copy junit.jar to Ant’s lib directory.<project name="currency" default="test" basedir="."> <property name="lib" value="./lib"/> <property name = "report" value="./report" /> <path id="lib1"> <pathelement location="."/> <fileset dir="${lib}"> <include name="**/*.jar"/> </fileset> </path> <target name="init"> <property name="outdir" value = "class"/> </target> <target name="prepare" depends="init"> <mkdir dir="${outdir}"/> <mkdir dir="${report}"/> </target>

Page 20: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

20

<target name="compile" depends="prepare"> <javac srcdir ="./src" destdir = "${outdir}"> <classpath refid="lib1"/> </javac> </target> <target name="test" depends="compile"> <junit printsummary = "true"> <formatter type="xml" /> <test name="TestCurrency" todir="${report}"/> <classpath> <pathelement location="${outdir}"/> </classpath> </junit> <junitreport todir="report"> <fileset dir ="${report}" > <include name = "TEST-*.xml"/> </fileset> <report format="frames" todir="${report}/html"/> </junitreport> </target> <target name="clean"> <delete dir="${report}"/> </target></project>

Page 21: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

21

Page 22: 1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that

22

Reading work

• http://junit.sourceforge.net/doc/cookbook/cookbook.htm