UT Java Eclipse SVN

  • Upload
    damdm

  • View
    236

  • Download
    0

Embed Size (px)

Citation preview

  • 8/11/2019 UT Java Eclipse SVN

    1/18

    Introduction to unit testing

    with Java, Eclipse and Subversion

    Table of Contents

    1. About Unit Tests................................................................................................................... 21.1. Introduction...................................................................................................................... 21.2. Unit tests frameworks...................................................................................................... 3

    2. A first test class.................................................................................................................... 42.1. Problem description......................................................................................................... 4

    2.2. Code organisation........................................................................................................... 42.3. Test implementation........................................................................................................ 62.4. anaging t!e pro"ect #ersions wit! $ub#ersion............................................................ 1%

    3. Stubs.................................................................................................................................... 143.1. & second package......................................................................................................... 14

    4. Test suites........................................................................................................................... 174.1. 'rouping unit tests in suites.......................................................................................... 1(

    Introduction to Unit Testing with Java, Eclipse and SubVersion 2006 lo!onsult S"#$ %&%'

  • 8/11/2019 UT Java Eclipse SVN

    2/18

    1. About Unit Tests

    1.1. Introduction

    Unit testing is a ke) tec!ni*ue in toda)+s agile software de#elopment. Unit tests are written b)t!e de#eloper during t!e coding acti#it), and t!us pertain in t!e w!ite bo- testing categor)

    Unit tests !a#e following c!aracteristics

    t!e) are automatic/do not re*uire !uman inter#ention during t!e tests0

    t!e) are reproducible

    t!e) are independentfrom eac! ot!er

    t!eir resultis eit!er OK/all tests !a#e been successful0, or NOK/at least one test failed,in t!is case details are gi#en to t!e user about t!e failure/s00

    ma) be combined in test suitesto allow nonregression testing during t!e de#elopment

    T!is document will present progressi#el) concrete unit testing cases, using

    a!aas programming language /!ttpwww."a#a.com0,

    "clipseas e#elopment n#ironment /!ttpwww.eclipse.org0

    Sub!ersionas #ersion control s)stem /!ttpsub#ersion.tigris.org0, its 'UI for 5indowsTortoiseS#N /!ttpwww.eclipse.org0, and its Subclipse plugin for clipse/!ttpsubclipse.tigris.org0.

    "nterprise Arc$itect as U odeling tool/!ttpwww.spar-s)stems.comproductsea.!tml0. .

    Introduction to Unit Testing with Java, Eclipse and SubVersion 2006 lo!onsult S"#$ 2&%'

    input output

    %lac& bo'testing

    input output

    ($ite bo'testing

    http://www.java.com/http://www.eclipse.org/http://subversion.tigris.org/http://www.eclipse.org/http://subclipse.tigris.org/http://www.sparxsystems.com/products/ea.htmlhttp://www.eclipse.org/http://subversion.tigris.org/http://www.eclipse.org/http://subclipse.tigris.org/http://www.sparxsystems.com/products/ea.htmlhttp://www.java.com/
  • 8/11/2019 UT Java Eclipse SVN

    3/18

    1.2. Unit tests frameworks

    7Unit is t!e original 7a#a librar) for unit testing, ad suc! frameworks e-ist nowada)s for man)programming languages. T!e) all aim at following t!e software design

    test1() and test2() are met!ods of t!e concrete test class ConcreteTestCase, onesuc! class being de#eloped per nominal application class /TestClass0. T!e) contain codetesting t!e ser#ices of t!is application class.

    8ote t!at t!e 9testfirst: practice recommend writing t!ose test met!ods before writing t!enominal code, because it !elps to keep t!e design simple from t!e start /see for e-ample t!earticle of ;on 7effries !ttpwww.-programming.com-pmagtest

  • 8/11/2019 UT Java Eclipse SVN

    4/18

    2. A first test class

    2.1. Problem description

    et us first t!ink of a Personclass, !a#ing a birt! date as attribute, and an age calculationmet!od

    2.2. Code organisation

    T!is class will be placed in t!e people package, itself in t!esrc.main.java.floconsult.unittests packages

    8ote t!at t!e director) structure follows t!e recommendation of t!e pro"ect building tool a#en/!ttpma#en.apac!e.orgma#en1.-referencecon#entions.!tml0.

    Using t!e 7Unit enabled menus of clipse, we ma) directl) create t!e associated test case /int!e parallel package tree under srctest0

    Introduction to Unit Testing with Java, Eclipse and SubVersion 2006 lo!onsult S"#$ )&%'

    Person

    birt!ate ate

    get&ge/0 int

    people

    Precondition8ot;espected4-ception

    utils

    ? preself.birt!ate@e-ists/0

    post result A date/toda)0 B self.birt!date

    DDt!rows@@

    http://maven.apache.org/maven-1.x/reference/conventions.htmlhttp://maven.apache.org/maven-1.x/reference/conventions.html
  • 8/11/2019 UT Java Eclipse SVN

    5/18

    Eere we c!ose to place t!e test class in t!e test part, and not to redefine t!e setUp()andtearDown()met!ods /t!e ot!er data is left as proposed b) clipse0

    5e t!us !a#e t!e following director) organisation in our code director), w!ic! at t!e same

    Introduction to Unit Testing with Java, Eclipse and SubVersion 2006 lo!onsult S"#$ *&%'

  • 8/11/2019 UT Java Eclipse SVN

    6/18

  • 8/11/2019 UT Java Eclipse SVN

    7/18

    T!en, to allow compiling t!e test code, let us define t!e skeleton /wit!out met!odimplementation0 of t!e Personclass

    ;unning t!e PersonTestunit tests

    5e !a#e /as awaited as t!e !et"!e#n$ears()0 met!odis not )et implemented0 bot! testsfail

    Introduction to Unit Testing with Java, Eclipse and SubVersion 2006 lo!onsult S"#$ +&%'

    $er#ice offered b) t!e Person class/not )et implemented0

  • 8/11/2019 UT Java Eclipse SVN

    8/18

    5e are read) now for implementing t!e met!od

    Introduction to Unit Testing with Java, Eclipse and SubVersion 2006 lo!onsult S"#$ '&%'

  • 8/11/2019 UT Java Eclipse SVN

    9/18

    5!at allow us to see our first 7Unit green bar, indicating t!e success of our 2 tests

    =efore detailing t!e notion of test suites and of stubs, let us take a look at t!e #ersionmanagement of our pro"ect files using $ub#ersion /$G80

    Introduction to Unit Testing with Java, Eclipse and SubVersion 2006 lo!onsult S"#$ &%'

  • 8/11/2019 UT Java Eclipse SVN

    10/18

    2.4. Managing te pro!ect "ersions wit #ub"ersion

    Considering we place t!e pro"ect in $ub#ersion before implementing t!e met!od

    T!en t!e user !as to select t!e $G8 repositor) to use /!ere file%&&&C%&localSvnRepo&0,and confirm t!e pro"ect import into t!is repositor). T!is done, t!e pro"ect ma) be seen in t!e$G8 ;epositor) #iew in clipse

    Introduction to Unit Testing with Java, Eclipse and SubVersion 2006 lo!onsult S"#$ %0&%'

    $G8 #ersion number oft!e file in t!e repositor)

    http://c/localSvnRepohttp://c/localSvnRepo
  • 8/11/2019 UT Java Eclipse SVN

    11/18

    Hr directl) in t!e Package -plorer #iew

    &fter t!e successful run of t!e tests, it is a good rule of t!umb to commit t!e modifications int!e $G8 repositor)

    Introduction to Unit Testing with Java, Eclipse and SubVersion 2006 lo!onsult S"#$ %%&%'

    = icon indicates t!e $ub#ersion link/becomes a star w!en file modified

    and not )et committed in $ub#ersion0

  • 8/11/2019 UT Java Eclipse SVN

    12/18

    &s soon as a file is managed under $ub#ersion, we ma) look at its !istor) in $ub#ersion

    $electing 2 #ersions, we ma) compare t!e files

    Introduction to Unit Testing with Java, Eclipse and SubVersion 2006 lo!onsult S"#$ %2&%'

  • 8/11/2019 UT Java Eclipse SVN

    13/18

    T!is to demonstrate t!e #er) nice integration of $ub#ersion in clipse /using t!e plugin gi#enin 1.1.Introduction0.

    Introduction to Unit Testing with Java, Eclipse and SubVersion 2006 lo!onsult S"#$ %(&%'

  • 8/11/2019 UT Java Eclipse SVN

    14/18

    3. Stubs

    3.1. $ second package

    et us now consider a package 9compan':, wit! an mplo'ee, being a Person, and !a#ing asalar) categor), w!ic!, allows to calculate !is!er salar)

    T!e calculation of t!e salar) needs a class implementing t!e interface Salar'!r, w!ic! isnot detailed !ere.

    To unit test t!e mplo'ee class, we cannot afford !a#ing a dependence on an e-ternalunknown class, and we t!us !a#e to introduce an implementation of Salar'!r, !a#ing a

    perfectl) predictable be!a#iour. T!is is a 9Stub: as represented !ereunder

    Introduction to Unit Testing with Java, Eclipse and SubVersion 2006 lo!onsult S"#$ %)&%'

    Person

    birt!ate ate

    get&ge/0 int

    people

    4mplo)ee

    salar)Categor) int A J1,2,3K

    getCurrent$alar)/0 int

    compan)

    get$alar)

  • 8/11/2019 UT Java Eclipse SVN

    15/18

    5e will place t!is stub in t!e test part of t!e pro"ect director), in t!e compan) package

    5!at allow us to test t!e !etCurrentSalar'()met!od of t!e mplo'eeclass

    Introduction to Unit Testing with Java, Eclipse and SubVersion 2006 lo!onsult S"#$ %*&%'

  • 8/11/2019 UT Java Eclipse SVN

    16/18

    T!is $tub is instantiated in t!e SetUp()met!od of t!e mplo'eeTestclass /recall t!at t!ismet!od is called before eac! test e-ecution0, and passed to t!e !etCurrentSalar'()met!od to allow testing it

    Hb#iousl), in t!is #er) simple e-ample, mplo'eedoes not add an) be!a#iour to t!e one oft!e Salar'!r, but t!is tec!ni*ue of stubbing is applicable to an) situation were we want ourclasses, and t!us our unit tests, to be independent of e-ternal /unpredictable0 be!a#iours.

    Introduction to Unit Testing with Java, Eclipse and SubVersion 2006 lo!onsult S"#$ %6&%'

  • 8/11/2019 UT Java Eclipse SVN

    17/18

    4. Test suites

    4.1. %rouping unit tests in suites

    & last important notion in t!is introduction to unit testing wit! 7a#a and clipse is t!e notion oftest suites.

    In t!e pre#ious paragrap!s, we successi#el) tested t!e Personclass of t!e peoplepackage/2 tests0, and t!e mplo'eeclass of t!e compan'package /3 tests0.

    &s said in t!e introduction, unit tests ma) be combined in suites, allowing to /re0e-ecute allt!e e-isting unit tests, and to #erif) t!at t!e s)stem did not 9regress: following modifications oradditions in t!e code.

    &gain, clipse offers a wiMard to ease t!e creation of test suites.

  • 8/11/2019 UT Java Eclipse SVN

    18/18

    T!is suite class e-ecutes all t!e test classes in t!e package

    5e similarl) create a test suite at t!e compan) le#el, and t!en a 9parent: test suite at t!e!ig!er le#el, w!ic! call t!e lowerle#el test suites.

    -ecuting t!is suite runs t!e L tests at once

    Introduction to Unit Testing with Java, Eclipse and SubVersion 2006 lo!onsult S"#$ %'&%'