Unit testing Java programs Using JUnit 3 - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Unit testing Java programs Using JUnit 3

Description:

Unit testing Java programs Using JUnit 3 If it isn't tested, it doesn t work Requirements for tests Tests must be executable A test must clearly show whether ... – PowerPoint PPT presentation

Number of Views:120
Avg rating:3.0/5.0
Slides: 14
Provided by: Ande74
Category:
Tags: java | junit | programs | testing | unit | using

less

Transcript and Presenter's Notes

Title: Unit testing Java programs Using JUnit 3


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

2
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.

3
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

4
Individual test cases
  • How to write a test case
  • Extends TestCase
  • class MyTest extends TestCase
  • A test method must have the signature
  • public void testXxx() throws ExceptionsAsYouLike
  • Generally you would like one test case pr. Java
    class
  • The unit to test is a class.

5
Fixtures setUp and tearDown
  • Sometimes you have 2 or more tests that must run
    on the same data. To ease this JUnit introduces
    the concept of a fixture.
  • 2 methods inherited from TestCase meant to be
    overridden
  • setUp()
  • Executed before each individual test
  • tearDown()
  • Executed after each individual test

6
Test suites
  • You get a lot of test cases (1 pr. Java class).
  • To several test cases at once you define a test
    suite
  • Make an object of class TestSuite
  • Add the individual test cases to the TestSuite
  • Run the TestSuite

7
How to test exceptions
  • Testing an expected exception
  • try
  • method()
  • fail(Exception expected)
  • catch (ExpectedException ex) / ignore /
  • A test method must have the signature
  • public void testXxx() throws Exception
  • That means that you can throw any exception from
    a test method.
  • However, if a test methods throws an exception it
    means that the test has not passed.

8
NetBeans assistance
  • NetBeans can assist you in making TestCases for
    individual Java class and in assembling the test
    cases into test suites.
  • TestCase
  • Tools ? JUnit tests
  • JUnit generates empty (in fact failing) tests for
    each public / protected method in a Java class.
  • Fill you the empty tests and run the test.
  • Test suites
  • New ? Test Suite
  • JUnit generates a test suite of all the test
    cases in the current test package.

9
Running tests
  • JUnit test can be run in several ways
  • With a graphical user interface
  • junit.swingui.TestRunner
  • Uses Swing classes for the GUI
  • With a textual user interface
  • Junit.textui.TextRunner
  • NetBeans uses a textual user interface when you
    run a test case or suite.

10
The internal structure of JUnit
  • JUnit uses the Reflection API
  • Objects of class Class are submitted to the test
    suites.
  • From an object Class JUnit finds all methods
    named testXxx() and executes those methods.
  • Calling setUp() before testXxx()
  • And calling tearDown() after testXxx()

11
Composite design pattern in JUnit
  • JUnit uses the composite design pattern to
    organize individual test cases and test suites

12
Testing in XP
  • 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

13
References
  • Beck Gamma JUnit Cookbook,
  • http//junit.sourceforge.net/doc/cookbook/cookboo
    k.htm
  • Kent Beck Erich Gamma invented JUnit
  • Martin Fowler Refactoring, Addison Wesley 2000
  • Chapter 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/l
    ibrary/os-junit
Write a Comment
User Comments (0)
About PowerShow.com