Testing With JUnit - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Testing With JUnit

Description:

Testing With JUnit. Samir Tartir. What is JUnit ... A structure for writing test drivers. JUnit features include: ... JUnit is used to test: an entire object ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 10
Provided by: samirt
Category:
Tags: junit | testing

less

Transcript and Presenter's Notes

Title: Testing With JUnit


1
Testing With JUnit
  • Samir Tartir

2
What is JUnit
  • Open source Java testing framework used to write
    and run repeatable automated tests
  • A structure for writing test drivers
  • JUnit features include
  • Assertions for testing expected results
  • Test features for sharing common test data
  • Test suites for easily organizing and running
    tests
  • Graphical and textual test runners
  • JUnit is widely used in industry
  • JUnit can be used as stand alone Java programs
    (from the command line) or within an IDE such as
    Eclipse

3
What Can JUnit Do?
  • JUnit is used to test
  • an entire object
  • part of an object a method or some interacting
    methods
  • interaction between several objects
  • A tester class contains more than one test
  • Each test is written into one test method
  • Test classes include
  • A test runner to run the tests (main())
  • A collection of test methods
  • Methods to set up the state before and update the
    state after each test and before and after all
    tests

4
How to Write JUnit Tests
  • Need to use the methods of the junit.framework.ass
    ert class
  • Each test method checks a condition (assertion)
    and reports back to the test runner whether the
    test failed or succeeded
  • All of the methods return void
  • A few representative methods of
    junit.framework.assert
  • assertTrue (boolean)
  • assertTrue (String, boolean)
  • assertEquals (Object, Object)
  • assertNull (Object)
  • Fail (String)

5
Sample Assertions
  • static void assertEquals (boolean expected,
    boolean actual)
  • Asserts that two booleans are equal.
  • assertNotNull(java.lang.Object object)
  • Asserts that an object isn't null.
  • assertNotSame(java.lang.Object unexpected,
    java.lang.Object actual)
  • Asserts that two objects do not refer to the
    same object.
  • assertTrue(java.lang.String message, boolean
    condition)
  • Asserts that the condition is true.
  • fail(java.lang.String message)
  • Fails a test with the given message.
  • For a complete list, see
  • http//junit.sourceforge.net/javadoc/org/junit/As
    sert.html

6
JUnit Versions
  • In JUnit 3.X (Used in the Book)
  • import junit.framework.
  • extend TestCase.
  • name the test methods with a prefix of test
  • validate conditions using one of the several
    assert methods
  • Explicit use of the setup and tearDown methods
  • In JUnit 4.0 and later
  • Do not extend from Junit.framework.TestCase
  • Do not prefix the test method with test
  • No need for explicit use of the setUp and
    tearDown methods
  • Use one of the assert methods
  • Run the test using JUnit4TestAdapter
  • _at_NAME syntax introduced

7
Preparing to Test Rectangle
  • import JUnit.framework. //
    JUnit stuff
  • import JUnit.extensions. //
    JUnit stuff
  • import gray.adts.shapes.Rectangle // the class
    to test
  • public class RectangleTester extends TestCase
  • private Rectangle r1, r2 //
    test fixture, objects under test
  • / Called AUTOMATICALLY before EACH testXXX()
    method is run. /
  • protected void setUp()
  • r1 new Rectangle()
  • r2 new Rectangle( 2.0, 3.0 )
  • / Called AUTOMATICALLY after EACH testXXX()
    method is run. /
  • protected void tearDown()
  • r1 null
  • r2 null

8
Sample Test Cases
/ Test Case 2.1 Verify that instantiation was
done properly using default values for the
Rectangles dimensions. / public void
testInstantiateDefault() assertEquals( 1.0,
r1.getLength() ) assertEquals( 1.0,
r1.getHeight() )
/ Test Case 2.6 Verify mutator for length
catches illegal input. / public void
testLengthMutatorIllegal () try
r1.setLength( 0 ) // 0 is illegal exception
should be thrown fail( Should raise
IllegalArgumentException) catch (
IllegalArgumentException e )
assertTrue(true) // expected an exception, so
the test passes
9
Additional Resources
  • JUnit Javadoc
  • http//junit.sourceforge.net/javadoc/
  • JUnit Home page www.junit.org
Write a Comment
User Comments (0)
About PowerShow.com