JUnit 4 - PowerPoint PPT Presentation

About This Presentation
Title:

JUnit 4

Description:

JUnit 4 makes it easier to test that exceptions are thrown when they should be ... Be warned: You don't know in what order they will execute ... – PowerPoint PPT presentation

Number of Views:704
Avg rating:3.0/5.0
Slides: 17
Provided by: DavidMa6
Category:
Tags: junit | warned

less

Transcript and Presenter's Notes

Title: JUnit 4


1
JUnit 4
2
Comparing JUnit 3 to JUnit 4
  • All the old assertXXX methods are the same
  • Most things are about equally easy
  • JUnit 4 makes it easier to test that exceptions
    are thrown when they should be
  • JUnit 4 can still run JUnit 3 tests
  • JUnit 4 provides protection against infinite
    loops
  • JUnit 4 has some additional features

3
Migrating from JUnit 3
  • JUnit 4 requires Java 5 or newer
  • Dont extend junit.framework.TestCase just use
    an ordinary class
  • Import org.junit. and org.junit.Assert.
  • Use a static import for org.junit.Assert.
  • Static imports replace inheritance from
    junit.framework.TestCase
  • Use annotations instead of special method names
  • Instead of a setUp method, put _at_Before before
    some method
  • Instead of a tearDown method, put _at_After before
    some method
  • Instead of beginning test method names with
    test, put _at_Test before each test method

4
Writing a JUnit test class, I
  • Start by importing the JUnit 4 classes you need
  • import org.junit.import static
    org.junit.Assert.
  • Declare your class in the usual way
  • public class MyProgramTest
  • Declare any variables you are going to use
    frequently, typically including an instance of
    the class being tested
  • MyProgram programint arrayint solution

5
Writing a JUnit test class, II
  • If you wish, you can declare one method to be
    executed just once, when the class is first
    loaded
  • This is for expensive setup, such as connecting
    to a database
  • _at_BeforeClasspublic static void setUpClass()
    throws Exception // one-time
    initialization code
  • If you wish, you can declare one method to be
    executed just once, to do cleanup after all the
    tests have been completed
  • _at_AfterClasspublic static void tearDownClass()
    throws Exception // one-time cleanup
    code

6
Writing a JUnit test class, III
  • You can define one or more methods to be executed
    before each test typically such methods
    initialize values, so that each test starts with
    a fresh set
  • _at_Beforepublic void setUp() program new
    MyProgram() array new int 1, 2, 3, 4,
    5
  • You can define one or more methods to be executed
    after each test typically such methods release
    resources, such as files
  • _at_Afterpublic void tearDown()

7
_at_Before and _at_After methods
  • You can have as many _at_Before and _at_After methods
    as you want
  • Be warned You dont know in what order they will
    execute
  • You can inherit _at_Before and _at_After methods from a
    superclass execution is as follows
  • Execute the _at_Before methods in the superclass
  • Execute the _at_Before methods in this class
  • Execute a _at_Test method in this class
  • Execute the _at_After methods in this class
  • Execute the _at_After methods in the superclass

8
Writing a JUnit test class, IV
  • A test method is annotated with _at_Test, takes no
    parameters, and returns no result
  • All the usual assertXXX methods can be used
  • _at_Testpublic void sum() assertEquals(15,
    program.sum(array)) assertTrue(program.min(ar
    ray) gt 0)

9
Special features of _at_Test
  • You can limit how long a method is allowed to
    take
  • This is good protection against infinite loops
  • The time limit is specified in milliseconds
  • The test fails if the method takes too long
  • _at_Test (timeout10) public void greatBig()
    assertTrue(program.ackerman(5, 5) gt 10e12)
  • Some method calls should throw an exception
  • You can specify that a particular exception is
    expected
  • The test will pass if the expected exception is
    thrown, and fail otherwise
  • _at_Test (expectedIllegalArgumentException.class)pu
    blic void factorial() program.factorial(-5)

10
Parameterized tests
  • Using _at_RunWith(valueParameterized.class) and a
    _at_Parameters method, you can run the same tests
    with multiple datasets
  • _at_RunWith(valueParameterized.class)public class
    FactorialTest private long expected
    private int value _at_Parameters public
    static Collection data() return
    Arrays.asList( new Object 1, 0 , 1,
    1 , 2, 2 , 120, 5 ) public
    FactorialTest(long expected, int value) //
    constructor this.expected expected
    this.value value _at_Test
    public void factorial()
    assertEquals(expected, new Calculator().factorial(
    value))
  • Source http//today.java.net/pub/a/today/2006/12/
    07/junit-reloaded.html

11
Ignoring a test
  • The _at_Ignore annotation says to not run a test
  • _at_Ignore("I dont want Dave to know this doesnt
    work")_at_Testpublic void add()
    assertEquals(4, program.sum(2, 2))
  • You shouldnt use _at_Ignore without a very good
    reason!

12
Test suites
  • As before, you can define a suite of tests
  • _at_RunWith(valueSuite.class)_at_SuiteClasses(valueM
    yProgramTest.class,
    AnotherTest.class)public class AllTests

13
Other stuff
  • Failed tests now throw an AssertionError, rather
    than JUnit 3s AssertionFailedError
  • There is now a version of assertEquals for arrays
    of objectsassertEquals(Object expected,
    Object actual)
  • Unfortunately, there is still no assertEquals for
    arrays of primitives
  • JUnit 3 had an assertEquals(p, p) method for each
    kind of primitive p, but JUnit 4 only has an
    assertEquals(object, object) and depends on
    autoboxing

14
A gotcha
  • The following method
  • long sum(long x, long y) return x y
  • with the following test
  • _at_Testpublic void sum() assertEquals(4,
    s.sum(2, 2))
  • gives
  • expected lt4gt but was lt4gt
  • This is due to your friend, autoboxing
  • assertEquals no longer exists for primitives,
    only for objects
  • Hence, the 4 is autoboxed to an Integer, while
    sum returns a long
  • The error message means expected int 4, but got
    long 4
  • To make this work, change the 4 to a 4L

15
JUnit 4 in Eclipse and NetBeans
  • As usual, the easiest way to create a test class
    is just to let your IDE do it for you
  • Here is the recommended test-driven approach
  • Create a class containing all the stub methods
    you initially think you will need
  • Have the IDE create the test class, with all the
    test methods
  • Repeat
  • Write a test
  • Make sure the test fails
  • Write the method being tested
  • Make sure the test now succeeds
  • Note When you create the test class, NetBeans in
    particular puts a lot of garbage lines into each
    test method you can just delete these and put in
    your own code

16
The End
Write a Comment
User Comments (0)
About PowerShow.com