Extending Jun It

Embed Size (px)

Citation preview

  • 8/6/2019 Extending Jun It

    1/33

    ExtendingJUnit 4

    im Hurne, Systems Architect

  • 8/6/2019 Extending Jun It

    2/33

    eview of Unit Testing

    Unit smallest testable part of an application

    Unit Test validate each individual unit of code.

  • 8/6/2019 Extending Jun It

    3/33

    haracteristics of a good Unit Test

    Isolated

    Test Single Unit

    Fast Dont waste the developers time

  • 8/6/2019 Extending Jun It

    4/33

    haracteristics of a good Unit Test

    Self Contained

    Doesnt rely on external resources that could fail

    Independent Doesnt depend on any other tests

  • 8/6/2019 Extending Jun It

    5/33

    haracteristics of a good Unit Test

    Maintainable

    Treat just like any other code

    Well documented

    i i

  • 8/6/2019 Extending Jun It

    6/33

    tegration Testing

    Test how different units

    integrate with each other

    Test object interactions

    Test interaction with outside

    systems

    i i i h i

  • 8/6/2019 Extending Jun It

    7/33

    nit Testing with JUnit 3

    Create class that extends TestCase

    Provides testing infrastructure Provides assertion methods

    Write methods with a name that starts with

    test

    No Parameter Return void

    i i i h i 3

  • 8/6/2019 Extending Jun It

    8/33

    nit Testing with JUnit 3

    ublic class HelloWorldTest extends TestCase {

    HelloWorld classUnderTest;

    public voidsetUp() {

    classUnderTest = new HelloWorld();

    }

    public voidtestHelloWorldReturned() {

    assertEquals(Hello World, classUnderTest.sayHello());

    }

    di JU i 3

  • 8/6/2019 Extending Jun It

    9/33

    xtending JUnit 3

    Extend TestCase

    Provide custom assertions

    Define custom naming

    conventions

  • 8/6/2019 Extending Jun It

    10/33

    XMLUnit

    public class MyXMLTestCase extends XMLTestCase {

    . . .

    public void testForEquality() throws Exception {

    . . .

    assertXMLEqual(myControlXML, myTestXML);

    assertXMLNotEqual(myControlXML, myTestXML);

    }

    }

    d f JU i 3

  • 8/6/2019 Extending Jun It

    11/33

    dvantages of JUnit 3

    Supports Java 1.4 and earlier

    Many extensions already available

  • 8/6/2019 Extending Jun It

    12/33

    it T ti ith JU it 4

  • 8/6/2019 Extending Jun It

    13/33

    nit Testing with JUnit 4

    static import org.junit.Assert.*;

    public class HelloWorldTest {

    HelloWorld classUnderTest;

    @Beforepublic void createTestObject {

    classUnderTest = new HelloWorld();

    }

    @Testpublic void helloWorldReturned() {

    assertEquals(Hello World, classUnderTest.sayHello())

    }

    }

    t di JU it 4

  • 8/6/2019 Extending Jun It

    14/33

    xtending JUnit 4

    Create custom test runner

    Implement Runner Use the @RunWith annotation

    Provide static custom assertion

    methods

    Take advantage of static imports

    Define custom annotations

  • 8/6/2019 Extending Jun It

    15/33

    d t f JU it 4

  • 8/6/2019 Extending Jun It

    16/33

    dvantages of JUnit 4

    Simplified and easier to use

    Doesnt burn up inheritance

    Additional capabilities and features

    l t t I T h S t

  • 8/6/2019 Extending Jun It

    17/33

    evelopment at ImageTech Systems

    Integrating third-party applications

    Connectors Complex Independent Systems

    Poorly designed and documented APIs Difficult to incorporate in tests

    Missing or inaccurate documentation Need to perform Integration Testing

    h P bl ith I t ti T ti

  • 8/6/2019 Extending Jun It

    18/33

    he Problem with Integration Testing

    Variable Test Parameters

    Changes from workstation to workstation Requires Configuration

    Complex Test Dependencies Cumbersome to create in test code

    Same test code, many inputs Many extremes or boundary cases

    h t W N d d D t D i T t

  • 8/6/2019 Extending Jun It

    19/33

    hat We Needed: Data-Driven Tests

    Tests are defined by the test code AND the

    test data

    Test Code separated from Test Data

    Q i k A id T ti P tt

  • 8/6/2019 Extending Jun It

    20/33

    Quick Aside: Testing Patterns

    Design Patterns for Testing

    Examples The Simple Pass/Fail Test Pattern

    The Performance Test Pattern

    The Data-Driven Test Pattern

    h Fi t S l ti

  • 8/6/2019 Extending Jun It

    21/33

    he First Solution

    Define helper method for each data set

    Define separate test for each data set

    h S d S l ti P t i d

  • 8/6/2019 Extending Jun It

    22/33

    he Second Solution: Parameterized

    Use JUnit 4 Parameterized

    custom test runner

    Define data sets in static

    method

  • 8/6/2019 Extending Jun It

    23/33

    MLParametrizedRunner

    Custom test runner

    Data sets defined in XML file

    ML File Format

  • 8/6/2019 Extending Jun It

    24/33

    ML File Format

    Based on Spring bean declaration syntax

    Data for each test defined in

    element.

    ML File Format

  • 8/6/2019 Extending Jun It

    25/33

    ML File Format

    Basic Value Object Parameters:

    A String Value

    12

    JavaBean Parameters:

  • 8/6/2019 Extending Jun It

    26/33

    plementation

    Implements Runner by extending internal

    JUnit TestClassRunner

    Lists of parameters generated by factory

    object (ParameterFactory)

    karta Commons Digester

  • 8/6/2019 Extending Jun It

    27/33

    karta Commons Digester

    Default factory object uses Jakarata

    Commons Digester to parse XML

    High-level API on top of SAX

    Pattern matching, rules based API

    he JUnit 4 Extensions Project

  • 8/6/2019 Extending Jun It

    28/33

    he JUnit 4 Extensions Project

    Collection of extensions for JUnit 4

    Additional Annotations @Prerequisite

    @Category

    Support for running JUnit 4 with JVM 1.4 or

    earlierHome for legacy Swing Test Runner

  • 8/6/2019 Extending Jun It

    29/33

    lans

    Continue to develop the XML format

    Provide optional support for Spring and

    other application frameworks

    Provide support for ignoring tests

    nitExt Future Plans

  • 8/6/2019 Extending Jun It

    30/33

    nitExt - Future Plans

    Chainable Test Runners

    Testing tools for multi-threading

    Migration of JUnit 3 extensions to JUnit 4

    esources and Links

  • 8/6/2019 Extending Jun It

    31/33

    esources and Links

    JUnit http://www.junit.org

    http://junit.sourceforge.net

    JUnit 4 Extensions Project http://www.junitext.org

    http://code.google.com/junitext

    esources and Links

  • 8/6/2019 Extending Jun It

    32/33

    esources and Links

    XMLUnit http://xmlunit.sourceforge.net/

    Jakarta Commons Digester http://jakarta.apache.org/commons/digester/

    esources and Links

  • 8/6/2019 Extending Jun It

    33/33

    esources and Links

    Automated Test Patterns http://www.codeproject.com/gen/design/autp5.asp

    Robots on Wikipedia http://en.wikipedia.org/wiki/R._Daneel_Olivaw

    http://en.wikipedia.org/wiki/Marvin_the_Paranoid_Android

    http://en.wikipedia.org/wiki/Johnny_5

    http://en.wikipedia.org/wiki/HAL9000