junit eclipse

Upload: ghardash

Post on 03-Apr-2018

243 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 JUnit Eclipse

    1/34

    JUnit & Eclipse 1

    JUnit & Eclipse

    DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING

    CONCORDIA UNIVERSITY

    Feb 2, 2009

    revision 1.2 Feb 2, 2009

    by Emil Vassev & Joey Paquet

  • 7/28/2019 JUnit Eclipse

    2/34

  • 7/28/2019 JUnit Eclipse

    3/34

    JUnit & Eclipse 3

    Testing

    Software testing is meant to avoid software failure.

    A failure is caused by a fault in the code base. A symptom is an observable behavior of the system that enables us to observe

    a failure and possibly find its corresponding fault.

    The process of discovering what caused a failure is called fault identification.

    The process of ensuring that the failure does not happen again is called fault

    correction, orfault removal.

    Fault identification and fault correction is popularly called debugging.

    Software testing, in practice, is about identifying a certain possible system

    failure and design a test case that proves that this particular failure is not

    experienced by the software.

    testing can reveal only the presence of faults, never their absence. [Dijkstra]

    :: Definition

  • 7/28/2019 JUnit Eclipse

    4/34

    JUnit & Eclipse 4

    Testing

    There are many driving sources for software testing: Requirements-driven testing, Structure-driven testing, Statistics-driven testing,

    Risk-driven testing.

    There are many levels and kinds of software testing:

    Unit Testing, Integration Testing, Function Testing, Acceptance Testing,Installation Testing.

    At the day-to-day programming level, unit testing can easily be integrated in

    the programming effort by using a Unit Testing Framework.

    However, unit testing cannot be applied for higher-level testing purposes such

    as function testing or acceptance testing, which are system-level testing

    activities.

    :: Definition

  • 7/28/2019 JUnit Eclipse

    5/34

  • 7/28/2019 JUnit Eclipse

    6/34

    JUnit & Eclipse 6

    Unit Testing

    Facilitates change: Unit testing allows the programmer to change or refactor code at a later date,

    and make sure the module still works correctly (i.e. regression testing).

    Simplifies integration:

    Unit testing helps to eliminate uncertainty in the units and can be used in abottom-up integration testing style approach.

    Documentation:

    Unit testing provides a sort of living documentation of the specifications of the

    units of the system. Developers looking to learn what functionality is providedby a unit and how to use it can look at the unit tests to gain understanding of the

    units API specifications.

    :: Benefits

  • 7/28/2019 JUnit Eclipse

    7/34

    JUnit & Eclipse 7

    Unit Testing

    Identifies defects early in the development cycle.

    Many small bugs ultimately leads to chaotic system behavior,

    which becomes increasingly difficult to work on.

    Successful (and meaningful) tests breed confidence.

    Makes sure that further changes do not introduce problems into

    previously correct code.

    Testing forces the programmers to read and analyze their code,

    thus removing defects through constant code verification.

    :: Benefits (cont.)

  • 7/28/2019 JUnit Eclipse

    8/34

    JUnit & Eclipse 8

    Unit Testing Framework

    For a large system, there can be thousands of unit tests, which

    can be tedious to maintain and execute.Automated tests support maintainability and extensibility along

    with efficiency.

    A xUnit Testing Frameworklets a programmer associate Classes

    and Methods to corresponding Test Classes and Test Methods.Automation is achieved by automatically setting up a testing

    context, calling each test case, verifying their corresponding

    expected result, and reporting the status of all tests.

    Can be combined with the use of a Software VersioningRepository: prior to any commit being made, unit testing is re-

    applied to make sure that the committed code is still working

    properly.

    :: Rationale

  • 7/28/2019 JUnit Eclipse

    9/34

    JUnit & Eclipse 9

    JUnitthe Javas xUnit Testing Framework

    In Java, the standard unit testing framework is known as JUnit.

    Test Cases and Test Results are Java objects.

    JUnit was created by Erich Gamma and Kent Beck, two authors

    best known for Design Patterns and eXtreme Programming,

    respectively.

    Using JUnit you can easily and incrementally build a test suite

    that will help you measure your progress, spot unintended side

    effects, and focus your development efforts.

    :: Introduction

  • 7/28/2019 JUnit Eclipse

    10/34

    JUnit & Eclipse 10

    JUnitthe Javas Unit Testing Framework

    Tested Classthe class that is being tested.

    Tested Methodthe method that is tested.

    Test Casethe testing of a classs method against some

    specified conditions.

    Test Case Classa class performing the test cases.Test Case Method a Test Case Classs method

    implementing a test case.

    Test Suitea collection of test cases that can be tested ina single batch.

    :: Key JUnit Notions

  • 7/28/2019 JUnit Eclipse

    11/34

    JUnit & Eclipse 11

    JUnitthe Javas Unit Testing Framework

    The class TestCase has four important methodsrun(), setUp(), tearDown() and

    runTest().

    TestCase.run() applies Template Method pattern

    public voidrun(){

    setUp();

    runTest();

    tearDown();

    }

    The Template Method pattern defines the skeleton of an algorithm

    in an operation, deferring some steps to subclasses.

    All Test Case classes need to be subclasses to the TestCase class.

    :: TestCase Class

  • 7/28/2019 JUnit Eclipse

    12/34

    JUnit & Eclipse 12

    JUnitthe Javas Unit Testing Framework

    JUnit test runners automatically invoke the setUp() method before running

    each Test Class. This method typically initializes fields, turns on logging, resets environment

    variables, and so forth, i.e. it sets up a context for the test cases to be applied.

    protected voidsetUp()

    {

    System.out.println(" Before testing" );}

    In JUnit 4, the initialization method no longer needs to be called setUp().

    It just needs to be denoted with the @Before annotation.

    We can have multiple methods noted @Before, each running before testing.

    @Beforeprotected void ini tiali ze()

    {

    System.out.println(" Before testing" );

    }

    :: TestCase Class SetUp

  • 7/28/2019 JUnit Eclipse

    13/34

    JUnit & Eclipse 13

    JUnitthe Javas Unit Testing Framework

    If we need at the end of each test to do a cleanup operation, we can use

    JUnits tearDown()method. For example we can call the garbage collectorthere in case our tests consume large amount of memory.

    protected voidtearDown()

    {

    System.out.println(Af ter testing" );

    System.gc();}

    In JUnit 4, we can give it a more natural name and annotate it with @After.

    @Afterprotected void disposeObjects ()

    {

    System.out.println(Af ter testing" );

    System.gc();

    }

    :: TestCase Class TearDown

  • 7/28/2019 JUnit Eclipse

    14/34

    JUnit & Eclipse 14

    JUnitthe Javas Unit Testing Framework

    1. Create the class that you want to test.

    2.

    Build the test class - with the needed imports and extensions for JUnit. Extend this class from junit.framework.TestCase.

    Name all the test methods with a prefix of test.

    3. Code the actual test cases.

    Validate conditions and invariants using one of the several assert methods.

    import juni t.fr amework.* ;public class TestFailureextendsTestCase{

    public voidtestSquareRootException() {

    try {

    SquareRoot.sqrt(-4, 1);fail("Should raise an exception");

    }

    catch (Exception success) { }

    }

    }

    :: Basics - JUnit 3

    Tested Class and Method

    Test Case Method

    Assertion Statement

    Test Case Class

  • 7/28/2019 JUnit Eclipse

    15/34

    JUnit & Eclipse 15

    JUnitthe Javas Unit Testing Framework

    Tests are identified by an @Test annotation and we no longer need to prefix

    our test methods with test. This lets us follow the naming convention that best fits our application.

    import juni t.fr amework.* ;

    import org.junit.Test;

    publi c classTestAdditionextendsTestCase{

    private int x = 1;

    pri vate int y = 1;

    @Testpublic void addition()

    {

    in t z = x + y;

    assertEqual s(2, z);

    }

    }

    :: Basics JUnit 4

  • 7/28/2019 JUnit Eclipse

    16/34

    JUnit & Eclipse 16

    JUnitthe Javas Unit Testing Framework:: Example Tested Class

  • 7/28/2019 JUnit Eclipse

    17/34

    JUnit & Eclipse 17

    JUnitthe Javas Unit Testing Framework:: Example Test Class

  • 7/28/2019 JUnit Eclipse

    18/34

    JUnit & Eclipse 18

    JUnitthe Javas Unit Testing Framework

    List of different types of assertion statements that you can use to test

    your code. These assertions are taken from the JUnit API. assertEquals(expected, actual)

    assertEquals(message, expected, actual)

    assertEquals(expected, actual, delta)- used on doubles or

    floats, where delta is the difference in precision.

    assertEquals(message, expected, actual, delta) - used on

    doubles or floats, where delta is the difference in precision.

    assertFalse(condition)

    assertF alse(message, condi tion)

    assertNotNull(object)

    assertNotNull(message, object)

    :: Assertion Statement Reference I

    http://www.junit.org/junit/javadoc/3.8.1/index.htmhttp://www.junit.org/junit/javadoc/3.8.1/index.htmhttp://www.junit.org/junit/javadoc/3.8.1/index.htmhttp://www.junit.org/junit/javadoc/3.8.1/index.htm
  • 7/28/2019 JUnit Eclipse

    19/34

    JUnit & Eclipse 19

    JUnitthe Javas Unit Testing Framework

    assertNotSame(expected, actual)

    assertNotSame(message, expected, actual) assertNull(object)

    assertNull(message, object)

    assertSame(expected, actual)

    assertSame(message, expected, actual)

    assertTrue(condition)

    assertTrue(message, condition)

    fail()

    fail(message)

    failNotEquals(message, expected, actual)

    failNotSame(message, expected, actual)

    failSame(message)

    :: Assertion Statement Reference II

    U i T i i E li i JU i

  • 7/28/2019 JUnit Eclipse

    20/34

    JUnit & Eclipse 20

    Unit Testing in Eclipse using JUnit

    Eclipse comes with both JUnit and a plug-in for

    creating and working with JUnit tests.

    Eclipse allows you to quickly create test case classes

    and test suite classes to write your test code in.

    With Eclipse, Test Driven Development (TDD),becomes very easy to organize and implement.

    Eclipse facilitates the testing by generating

    automatically stubs for testing class methods.

    :: Introduction

    U it T ti i E li i JU it

  • 7/28/2019 JUnit Eclipse

    21/34

    JUnit & Eclipse 21

    Unit Testing in Eclipse using JUnit

    Once the class we want to test, is created we can start with

    building the test cases. To create a test case do [File New JUnit Test Case]

    :: Adding a Test Case to the Project

    Put the test case class into the same

    package as the tested class.

    U it T ti i E li i JU it

  • 7/28/2019 JUnit Eclipse

    22/34

    JUnit & Eclipse 22

    Unit Testing in Eclipse using JUnit:: Writing Test Cases

    This is a test case class for testing SquareRoot

    class. The following test case methods test

    different aspects of the sqrt()method:

    The testSquareRootException() method

    demonstrates how to test if an exception is

    properly thrown.

    The testSquareRootPrecision() method

    tests against the precision ofSquareRoot.sqrt().

    The testSquareRootAlgorithm() method

    tests the square root algorithm.

    U it T ti i E li i JU it

  • 7/28/2019 JUnit Eclipse

    23/34

    JUnit & Eclipse 23

    Unit Testing in Eclipse using JUnit:: Running the Test

    From Menu bar [Run Run As JUnit Test],

    or:

    U it T ti i E li i JU it

  • 7/28/2019 JUnit Eclipse

    24/34

    JUnit & Eclipse 24

    Unit Testing in Eclipse using JUnit:: Test Result Analysis

    These two test

    case methods

    reported failures

    U it T ti i E li i JU it

  • 7/28/2019 JUnit Eclipse

    25/34

    JUnit & Eclipse 25

    Unit Testing in Eclipse using JUnit:: Test Result Analysis

    The following test fails because of insufficient precision:

    Abs(Math.sqrt(67) - SquareRoot.sqrt(67, 5)) > 0.000001

    public voidtestSquareRootPrecision()thr ows Exception

    {

    Assert.assertEquals(" SquareRoot precision less than 0.000001" ,Math.sqrt(67), SquareRoot.sqrt(67, 5), 0.000001);

    }

    In order to increase the precision, we increase the number ofiterations from 5 to 6, to arrive at:

    Assert.assertEquals(" SquareRoot precision less than 0.000001" ,

    Math.sqrt(67), SquareRoot.sqrt(67, 6), 0.000001);

    U it T ti i E li i JU it

  • 7/28/2019 JUnit Eclipse

    26/34

    JUnit & Eclipse 26

    Unit Testing in Eclipse using JUnit:: Test Result Analysis

    The following test fails because the SquareRoot.sqrt()method

    does notcompute the exact square root of number 67.

    public voidtestSquareRootAlgorithm()throws Exception

    {

    Assert.assertTrue(67 == (SquareRoot.sqrt(67, 6) * SquareRoot.sqrt(67, 6)));}

    In order to fix this problem we increase the number of iterations

    from 6 to 7, to arrive at:

    Assert.assertTr ue(67 == (SquareRoot.sqrt(67, 7) * SquareRoot.sqrt(67, 7)));

    U it T ti i E li i JU it

  • 7/28/2019 JUnit Eclipse

    27/34

    JUnit & Eclipse 27

    Unit Testing in Eclipse using JUnit:: Rerunning the Test

    All the test case

    methods passed

    the test.

    Unit Testing in Eclipse using JUnit

  • 7/28/2019 JUnit Eclipse

    28/34

    JUnit & Eclipse 28

    Unit Testing in Eclipse using JUnit:: Test Suit - Introduction

    We have performed tests on only one class, i.e. we have tested

    methods under the consideration they belong to the same class.In large projects we have many classes with methods that should

    be tested.

    For testing multiple classes Eclipse and JUnit expose the concept

    ofTest Suit.A Test Suit is a collection of test cases that can be tested in a

    single batch.

    A Test Suite is a simple way of running one program that, in

    turn, runs all test cases.

    Unit Testing in Eclipse using JUnit

  • 7/28/2019 JUnit Eclipse

    29/34

    JUnit & Eclipse 29

    Unit Testing in Eclipse using JUnit:: Creating a Test Suit

    There are four ways to create a JUnit Test Suite Class. First,

    select the directory (usually unittests/) that you wish to create thetest suite class in.

    Select [File New Other... Java JUnit JUnit Test

    Suite].

    Select the arrow of the button in the upper left of thetoolbar. Select [Other... Java JUnit JUnit Test Suite].

    Right click on a package in the Package Explorer view in the

    Java Perspective, and select [Other... Java JUnit

    JUnit Test Suite]. You can create a normal Java class, but import the package

    junit.f rameworkand extend the TestSuite class.

    Unit Testing in Eclipse using JUnit

  • 7/28/2019 JUnit Eclipse

    30/34

    JUnit & Eclipse 30

    Unit Testing in Eclipse using JUnit:: Adding a New Class to be Tested

    This class is taken from the tutorial

    paper JUnit Test Infected:

    Programmers Love Writing Tests,

    by Kent Beck and Erich Gamma

    Unit Testing in Eclipse using JUnit

  • 7/28/2019 JUnit Eclipse

    31/34

    JUnit & Eclipse 31

    Unit Testing in Eclipse using JUnit:: Adding a New TestCase Class

    This class is taken from the tutorial paper JUnit Test Infected:

    Programmers Love Writing Tests,by Kent Beck and Erich Gamma.

    Unit Testing in Eclipse using JUnit

  • 7/28/2019 JUnit Eclipse

    32/34

    JUnit & Eclipse 32

    Unit Testing in Eclipse using JUnit:: Creating the Test Suite Class

    Creating a Test Suit is straight forward and you just have to

    follow the wizard. All the tests would be taken care of by JUnit.

    Unit Testing in Eclipse using JUnit

  • 7/28/2019 JUnit Eclipse

    33/34

    JUnit & Eclipse 33

    Unit Testing in Eclipse using JUnit:: Running All Tests

    Right click on the test suite class and select [Run As JUnit Test]

    Resources

  • 7/28/2019 JUnit Eclipse

    34/34

    JUnit & Eclipse 34

    JUnit

    JUnit FAQ

    JUnit API

    Eclipse

    An early look at JUnit 4

    Pragmatic Unit Testing in Java with JUnit

    http://newton.cs.concordia.ca/~paquet/wiki/index.php/Testing

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

    Resources

    http://www.junit.org/http://junit.sourceforge.net/doc/faq/faq.htmhttp://www.junit.org/junit/javadoc/3.8.1/index.htmhttp://www.eclipse.org/http://www-128.ibm.com/developerworks/java/library/j-junit4.htmlhttp://www.pragmaticprogrammer.com/starter_kit/utj/whatis.pdfhttp://newton.cs.concordia.ca/~paquet/wiki/index.php/Testinghttp://en.wikipedia.org/wiki/List_of_unit_testing_frameworkshttp://en.wikipedia.org/wiki/List_of_unit_testing_frameworkshttp://newton.cs.concordia.ca/~paquet/wiki/index.php/Testinghttp://www.pragmaticprogrammer.com/starter_kit/utj/whatis.pdfhttp://www-128.ibm.com/developerworks/java/library/j-junit4.htmlhttp://www.eclipse.org/http://www.junit.org/junit/javadoc/3.8.1/index.htmhttp://junit.sourceforge.net/doc/faq/faq.htmhttp://www.junit.org/