Unit Testing with JUnit and Clover - PowerPoint PPT Presentation

About This Presentation
Title:

Unit Testing with JUnit and Clover

Description:

JUnit plugin for Eclipse JUnit framework Some benefits of this framework No major difference between a test case and a test suite Both can be invoked using the run() ... – PowerPoint PPT presentation

Number of Views:551
Avg rating:3.0/5.0
Slides: 25
Provided by: DanielA180
Category:
Tags: clover | junit | testing | unit

less

Transcript and Presenter's Notes

Title: Unit Testing with JUnit and Clover


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

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

4
Common XP day
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.

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.

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 )

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
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
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
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.

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 Assert class name is often not required
  • assertEquals( expected, actual )

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.

14
JUnit execution (with failures)
15
JUnit execution (success!)
16
JUnit plugin for Eclipse
17
JUnit framework
TestResult
fTests
Your tests here.
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!

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

20
Clover plugin for Eclipse
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.

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

23
Fixing the Clover plugin caveats
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 Cooks Tour
  • http//junit.sourceforge.net/doc/cookstour/cooksto
    ur.htm
  • Clover Eclipse Plugin (with installation
    instructions)
  • http//www.thecortex.net/clover/userguide/eclipse/
Write a Comment
User Comments (0)
About PowerShow.com