Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.

Similar presentations


Presentation on theme: "Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site."— Presentation transcript:

1 Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site

2 Unit testing with Junit and Clover2 JUnit (http://www.junit.org) A unit test framework for Java Authors: Erich Gamma, Kent Beck Part of XUnit family (HTTPUnit, Cactus), CppUnit Essential part of the eXtreme Programming methodology, but can be used independently Used for regression testing as well, but not for system testing Integrated to Eclipse, but can be used standalone

3 Unit testing with Junit and Clover3 eXtreme Programming (XP) and unit testing In XP, a test shall: Be written first —before any code Executed —will likely fail! Then: Implementation code should be written that would be the minimum code required to get the test to pass – and no extra functionality. Once the code is written, re-execute the test and it should pass. When needed, refactor the code mercilessly. —Improve performance, maintainability, readability

4 Unit testing with Junit and Clover4 Common XP day

5 Unit testing with Junit and Clover5 Some benefits of JUnit (and Test-Driven Development) Testing is a Good Thing Immediate gratification with build iterations Start with “The Simplest Thing That Could Possibly Work”. Green bar addiction! Break the cycle of “more pressure means fewer tests” Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead.” Martin Fowler Reduce code captivity If others can test it, others can work on it.

6 Unit testing with Junit and Clover6 What is a JUnit test? A test “script” is just a collection of small Java methods. General idea is to create a few Java objects, do something interesting with them, and then determine if the objects have the correct properties. What is added? Assertions! A package of methods that checks various properties: — equality of variables — identity of objects The assertions are used to determine the test case verdict.

7 Unit testing with Junit and Clover7 A JUnit test case /** Test of copy method, class ProblemBase.Value */ public void testCopy() { System.out.println("testCopy"); Value v1 = new Value( ); v1.setName( "X" ); Value v2 = v1.copy( ); v1.setName( "Y" ); String expected = "X"; String actual = v2.getName( ); Assert.assertEquals( expected, actual ); }

8 Unit testing with Junit and Clover8 A JUnit test case /** Test of copy method, class ProblemBase.Value */ public void testCopy() { System.out.println("testCopy"); Value v1 = new Value( ); v1.setName( "X" ); Value v2 = v1.copy( ); v1.setName( "Y" ); String expected = "X"; String actual = v2.getName( ); Assert.assertEquals( expected, actual ); } Method signature: no parameters

9 Unit testing with Junit and Clover9 A JUnit test case /** Test of copy method, class ProblemBase.Value */ public void testCopy() { System.out.println("testCopy"); Value v1 = new Value( ); v1.setName( "X" ); Value v2 = v1.copy( ); v1.setName( "Y" ); String expected = "X"; String actual = v2.getName( ); Assert.assertEquals( expected, actual ); } Objective: create a duplicate object, instead of copying reference

10 Unit testing with Junit and Clover10 A JUnit test case /** Test of copy method, class ProblemBase.Value */ public void testCopy() { System.out.println("testCopy"); Value v1 = new Value( ); v1.setName( "X" ); Value v2 = v1.copy( ); v1.setName( "Y" ); String expected = "X"; String actual = v2.getName( ); Assert.assertEquals( expected, actual ); } Check for a condition that should not be violated

11 Unit testing with Junit and Clover11 A JUnit test class import junit.framework.*; // Each test class must extend the Junit // TestCase class public class CopyTest extends TestCase { // Must provide a constructor with String // argument public CopyTest(String name) { super(name); } // Insert your test cases here. setup(), // tearDown() and main() methods can also be // added. }

12 Unit testing with Junit and Clover12 Assertions and verdicts Assertions are defined in the special JUnit class Assert If the assertions are true, the method continues executing. If any assertion is false, the method stops executing, and the result for the test case will be “fail”. If any other exception is thrown during the method, the result for the test case will be “error”. If no assertions were violated for the entire method, the test case will “pass”. The Assert class name is often not required assertEquals( expected, actual );

13 Unit testing with Junit and Clover13 Assertion methods Assertion methods can verify: Objects are identical, or not identical Objects are null or non-null “Equality” of objects — via == or equals() depending on type Boolean conditions are true or false There is also an unconditional failure method.

14 Unit testing with Junit and Clover14 JUnit execution (with failures)

15 Unit testing with Junit and Clover15 JUnit execution (success!)

16 Unit testing with Junit and Clover16 JUnit plugin for Eclipse

17 Unit testing with Junit and Clover17 run(TestResult) addTest() TestSuite run(TestResult) Test *suite(): TestSuite AnotherTestClass run(TestResult) runTest() setup() tearDown() fName TestCase Your tests here. *suite(): TestSuite ATestClass fTests TestResult JUnit framework

18 Unit testing with Junit and Clover18 Some benefits of this framework No major difference between a test case and a test suite Both can be invoked using the run() method Test cases are often associated with methods, and test suites with classes Uses reflection to lean about classes and methods Test suite structure discovered at run time Test suites can invoke the test cases automatically Common setup and teardown (clean up) for all test cases in a test suite Default ones can be overriden Integrated to Uis, and wizard for test creations Simple, efficient, and automated!

19 Unit testing with Junit and Clover19 Java code coverage tool: Clover Discovers sections of code that are not being adequately exercised by your (unit) tests. Supports method, statement, and branch coverage Reports its findings in multiple formats —From project level down to individual lines of source code Historical charting of code coverage and other metrics Integrated to Eclipse and other IDEs http://www.thecortex.net/clover/index.html

20 Unit testing with Junit and Clover20 Clover plugin for Eclipse

21 Unit testing with Junit and Clover21 Clover coverage filters One can choose not to instrument certain types of blocks in the code (e.g. assertions and exception catching), in order to focus on the coverage of interest.

22 Unit testing with Junit and Clover22 Clover plugin caveats This plugin may (very likely!) not work correctly if you have configured your Eclipse project so that the Java source and output directories are the same. —Use different directories for the original source code and instrumented source code When compiling your Java project with the Clover plugin, you must add and use a Java Development Kit (JDK) to your list of Installed JRE locations (see instructions). Not free software…

23 Unit testing with Junit and Clover23 Fixing the Clover plugin caveats

24 Unit testing with Junit and Clover24 For more information JUnit Web site http://junit.org Documentation on installation and use of JUnit E. Gamma, K. Beck, “JUnit Cookbook” http://junit.sourceforge.net/doc/cookbook/cookbook.htm Internals of JUnit “JUnit: A Cook’s Tour” http://junit.sourceforge.net/doc/cookstour/cookstour.htm Clover Eclipse Plugin (with installation instructions) http://www.thecortex.net/clover/userguide/eclipse/


Download ppt "Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site."

Similar presentations


Ads by Google