Unit testing Java programs Using JUnit 4

  • Published on
    09-Jan-2016

  • View
    24

  • Download
    4

Embed Size (px)

DESCRIPTION

Unit testing Java programs Using JUnit 4. If it isn't tested, it doesnt work. Testing. Larman a disciplin in UP but very little in practice UPEDU description. 1.1.1 Test Case 1 Verification of the generation of the billing events through the Customer Care interface. - PowerPoint PPT Presentation

Transcript

  • Unit testing Java programs*Unit testing Java programs Using JUnit 4If it isn't tested, it doesnt work

    Unit testing Java programs

  • TestingLarman a disciplin in UP but very little in practice

    UPEDU description

    Unit testing Java programs*

    Unit testing Java programs

  • The Unified Software Development Process: Test procedure (V-model)

    1.1.1 Test Case 1Verification of the generation of the billing events through the Customer Care interface

    StartDeliveryRequirement SpecificationAccept Test Program DesignIntegration Test

    Module Design Module Test

    Module Coding

  • Unit testing Java programs*Testing in XP Module code TestTesting is an important discipline in XP (Extreme Programming)XP idea: Create the test before the code to be testedWriting the test makes you thing about detailed designTest is an executable requirementsWriting (and running) test will be a positive experience.We know when a class is doneWhen all tests run

    Unit testing Java programs

  • Unit testing Java programs*Requirements for testsTests must be executableA test must clearly show whether it executed successfully or notThe not-so-successful part of the test must not be buried in a pile of test reports.

    Unit testing Java programs

  • Unit testing Java programs*Which methods should be testedTest a method if you are not 100% sure that the method is correct. Methods that usually does not need testingSimple get and set methodsHowever, you might call get and set methods in testing other (more complex) methodsSimple toString methods

    Unit testing Java programs

  • The Unified Software Development Process: Example testcase

    1.1.1 Test Case 1Verification of the generation of the billing events through the Customer Care interface

  • Test cases - ExampleA property value of int between 2 and 9

    Unit testing Java programs*

    Test CaseTest value expectedresult11 (before boundary)rejected22 (the boundary)accepted33 (after boundary)accepted48 (before boundary)accepted59 (the boundary)accepted610 (after boundary)rejected75 (some value in middle)accepted8-3 (a negative number)rejected

    Unit testing Java programs

  • Do the exerciseUnit testing Java programs*

    Unit testing Java programs

  • Unit testing Java programs*Individual test casesHow to in JUnitAnnotate the test method with @org.junit.TestNo need to extends any classes or use special method namesUnlike JUnit 3Generally you would like one test class pr. Java classThe unit to test is a class.

    Unit testing Java programs

  • Unit testing Java programs*Fixtures: Before and AfterSometimes you have 2 or more tests that must run on the same data. To ease this JUnit introduces the concept of a fixture.You can annotate 2 methods in your test class from@org.junit.BeforeExecuted before each individual testUsed to initialize test dataUsed quite often@org.junit.AfterExecuted after each individual testUsed to clean up after the testExamples: Close database or socket connectionsNot used very often

    Unit testing Java programs

  • Unit testing Java programs*How to test exceptionsJUnit 3: Testing an expected exceptiontry { method(); fail(Exception expected);}catch (ExpectedException ex) { /* ignore */ }JUnit 4: Testing an expected exceptionUse an annotation@Test (expected = SomeException.class) public void testMetod() { }If testMethod() does not throw SomeException the test fails.

    Unit testing Java programs

  • Unit testing Java programs*NetBeans assistanceJUnit is a plugin to NetBeansAnd many other IDEsNetBeans can assist you in making TestCases for individual Java class and in assembling the test cases into test suites.Right click the class you want to testTools JUnit testsChoose JUnit 4 (not JUnit 3)JUnit generates empty tests for each public / protected method in a Java class.Fill you the empty tests and run the test.Like you run an ordinary program

    Unit testing Java programs

  • Unit testing Java programs*ReferencesBeck & Gamma JUnit Cookbook,http://junit.sourceforge.net/doc/cookbook/cookbook.htm Kent Beck & Erich Gamma invented JUnitMartin Fowler Refactoring, Addison Wesley 2000Chapter 4 Building Tests, page 89-102Extreme Programming, Code the Unit Test First http://www.extremeprogramming.org/rules/testfirst.html Testing is an important discipline in XP (eXtreme Programming), which is another Kent Bech invention.Alex Garrett JUnit antipatterns http://www-128.ibm.com/developerworks/opensource/library/os-junit

    Unit testing Java programs

    **

Recommended

View more >