Unit testing Java programs Using JUnit 4 - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Unit testing Java programs Using JUnit 4

Description:

Unit testing Java programs Using JUnit 4 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:239
Avg rating:3.0/5.0
Slides: 10
Provided by: Ande100
Category:
Tags: beck | java | junit | programs | testing | unit | using

less

Transcript and Presenter's Notes

Title: Unit testing Java programs Using JUnit 4


1
Unit testing Java programs Using JUnit 4
  • 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
  • Annotate the test method with _at_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.

5
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
  • _at_org.junit.Before
  • Executed before each individual test
  • Used to initialize test data
  • Used quite often
  • _at_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

6
How to test exceptions
  • JUnit 3 Testing an expected exception
  • try
  • method()
  • fail(Exception expected)
  • catch (ExpectedException ex) / ignore /
  • JUnit 4 Testing an expected exception
  • Use an annotation
  • _at_Test (expected SomeException.class) public
    void testMetod()
  • If testMethod() does not throw SomeException the
    test fails.

7
NetBeans assistance
  • JUnit is a plugin to NetBeans
  • And many other IDEs
  • 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

8
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

9
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