14
Unit testing Java programs 1 Unit testing Java programs Using JUnit 4 “If it isn't tested, it doesn’t work”

Unit testing Java programs Using JUnit 4

  • Upload
    iorwen

  • View
    28

  • Download
    4

Embed Size (px)

DESCRIPTION

Unit testing Java programs Using JUnit 4. “If it isn't tested, it doesn’t 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

Citation preview

Page 1: Unit testing Java programs Using JUnit 4

Unit testing Java programs 1

Unit testing Java programs Using JUnit 4

“If it isn't tested, it doesn’t work”

Page 2: Unit testing Java programs Using JUnit 4

Testing

• Larman – a disciplin in UPbut very little in practice

• UPEDU – description

Unit testing Java programs 2

Page 3: Unit testing Java programs Using JUnit 4

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

Start Delivery

Requirement Specification Accept Test

Program Design Integration Test

Module Design Module Test

Module Coding

Page 4: Unit testing Java programs Using JUnit 4

Unit testing Java programs 4

Testing in XP – Module code Test

• Testing is an important discipline in XP (Extreme Programming)

• XP idea: Create the test before the code to be tested– Writing the test makes you thing about detailed

design• Test is an executable requirements

– Writing (and running) test will be a positive experience.

– We know when a class is done• When all tests run

Page 5: Unit testing Java programs Using JUnit 4

Unit testing Java programs 5

Requirements for tests

• Tests must be executable

• A test must clearly show whether it executed successfully or not– The not-so-successful part of the test must

not be buried in a pile of test reports.

Page 6: Unit testing Java programs Using JUnit 4

Unit testing Java programs 6

Which methods should be tested

• Test a method if you are not 100% sure that the method is correct.

• Methods that usually does not need testing– Simple get and set methods

• However, you might call get and set methods in testing other (more complex) methods

– Simple toString methods

Page 7: Unit testing Java programs Using JUnit 4

The Unified Software Development Process: Example testcase

Test Step

Input/Action Expected Output Result

1 Create an account or look up an existing one.Use the apposite link to create a billing event.  

A billing event is created.The account balance change reflects the presence of the new billing event

Account balance: an amount is not shown in a proper way. Se screen shot Test Case 1.

Page 8: Unit testing Java programs Using JUnit 4

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

Unit testing Java programs 8

Test Case

Test value expected result

1 1 (before boundary) rejected

2 2 (the boundary) accepted

3 3 (after boundary) accepted

4 8 (before boundary) accepted

5 9 (the boundary) accepted

6 10 (after boundary) rejected

7 5 (some value in middle) accepted

8 -3 (a negative number) rejected

Page 9: Unit testing Java programs Using JUnit 4

Do the exercise

Unit testing Java programs 9

Page 10: Unit testing Java programs Using JUnit 4

Unit testing Java programs 10

Individual test casesHow to in JUnit

• Annotate the test method with @org.junit.Test– No need to extends any classes or use

special method names• Unlike JUnit 3

• Generally you would like one test class pr. Java class– The unit to test is a class.

Page 11: Unit testing Java programs Using JUnit 4

Unit testing Java programs 11

Fixtures: Before and After• Sometimes 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.Before

• Executed before each individual test• Used to initialize test data• Used quite often

– @org.junit.After• Executed after each individual test• Used to clean up after the test

– Examples: Close database or socket connections• Not used very often

Page 12: Unit testing Java programs Using JUnit 4

Unit testing Java programs 12

How to test exceptions

• JUnit 3: Testing an expected exceptiontry { method(); fail(“Exception expected”);}catch (ExpectedException ex) { /* ignore */ }

• JUnit 4: Testing an expected exception– Use an annotation

• @Test (expected = SomeException.class) public void testMetod() { … }

• If testMethod() does not throw SomeException the test fails.

Page 13: Unit testing Java programs Using JUnit 4

Unit testing Java programs 13

NetBeans assistance

• JUnit is a plugin to NetBeans– And many other IDE’s

• NetBeans 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 test– Tools → JUnit tests

• Choose 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

Page 14: Unit testing Java programs Using JUnit 4

Unit testing Java programs 14

References

Beck & Gamma JUnit Cookbook,http://junit.sourceforge.net/doc/cookbook/cookbook.htm

Kent Beck & Erich Gamma invented JUnit

Martin Fowler Refactoring, Addison Wesley 2000Chapter 4 Building Tests, page 89-102

Extreme 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