1 / 24

Unit Testing with JUnit and Clover

Unit Testing with JUnit and Clover. Based on material from: Daniel Amyot JUnit Web site. JUnit (http://www.junit.org). A unit test framework for Java Authors: Erich Gamma, Kent Beck Part of XUnit family (HTTPUnit, Cactus), CppUnit

sari
Download Presentation

Unit Testing with JUnit and Clover

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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 Unit testing with Junit and Clover

  3. 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 Unit testing with Junit and Clover

  4. Common XP day Unit testing with Junit and Clover

  5. 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. Unit testing with Junit and Clover

  6. 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. Unit testing with Junit and Clover

  7. 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 ); • } Unit testing with Junit and Clover

  8. 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 Unit testing with Junit and Clover

  9. 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 Unit testing with Junit and Clover

  10. 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 Unit testing with Junit and Clover

  11. 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. • } Unit testing with Junit and Clover

  12. 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 Assertclass name is often not required • assertEquals( expected, actual ); Unit testing with Junit and Clover

  13. 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. Unit testing with Junit and Clover

  14. JUnit execution (with failures) Unit testing with Junit and Clover

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

  16. JUnit plugin for Eclipse Unit testing with Junit and Clover

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

  18. 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! Unit testing with Junit and Clover

  19. 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 Unit testing with Junit and Clover

  20. Clover plugin for Eclipse Unit testing with Junit and Clover

  21. 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. Unit testing with Junit and Clover

  22. 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… Unit testing with Junit and Clover

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

  24. 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/ Unit testing with Junit and Clover

More Related