Brief JUnit Tutorial - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Brief JUnit Tutorial

Description:

{ return fAmount == m.fAmount && fCurrency.equals (m.fCurrency) ... of your TestCase subclass and initialize them by overriding the setUp method ... – PowerPoint PPT presentation

Number of Views:168
Avg rating:3.0/5.0
Slides: 13
Provided by: mat7154
Category:

less

Transcript and Presenter's Notes

Title: Brief JUnit Tutorial


1
Brief JUnit Tutorial
2
The Testing Problem
Should write
Do
programmers
few
Why?
It is time consuming
I am so busy
3
JUnit Tutorial
  • The testing problem
  • JUnit framework
  • An example

4
The Framework of JUnit
5
JUnit Tutorial
  • The testing problem
  • JUnit framework
  • An example

6
  • public class Money
  • private int fAmount
  • private String fCurrency
  • public Money(int amount, String currency)
  • fAmount amount fCurrency currency
  • public int amount() return this.fAmount
  • public String currency () return fCurrency
  • public String toString ()
  • return "" fAmount fCurrency
  • public boolean equals (Money m)
  • return fAmount m.fAmount
    fCurrency.equals (m.fCurrency)

An example The class to test
7
How to Write A TestCase
  • Create the objects you interact with during the
    test.
  • This testing context is sometimes referred to
    as a test's fixture.
  • Use the objects in the fixture.
  • Verify the result

8
  • public class MoneyTest extends TestCase
  • private Money f12CHF
  • private Money f14CHF
  • protected void setUp()
  • f12CHF new Money(12, "CHF")
  • f14CHF new Money(14, "CHF")
  • public void testSimpleAdd()
  • Money expected new Money(26, "CHF")
  • Money unexpected new Money(27, "CHF")
  • Money result null
  • try
  • result f12CHF.add(f14CHF)
  • catch (MoneyException me)

The Test Case
9
Structure
  • setUp()
  • Create the fixture's objects in instance
    variables of your TestCase subclass and
    initialize them by overriding the setUp method
  • tearDown()
  • Release the fixtures
  • run()
  • Defining how to run an individual test
    case.
  • Defining how to run a test suite.
  • testCase()

10
Assert
  • assertEquals(expected, actual)
  • assertEquals(message, expected, actual)
  • assertEquals(expected, actual, delta)
  • assertEquals(message, expected, actual, delta)
  • assertFalse(condition)
  • assertFalse(message, condition)
  • Assert(Not)Null(object)
  • Assert(Not)Null(message, object)
  • Assert(Not)Same(expected, actual)
  • Assert(Not)Same(message, expected, actual)
  • assertTrue(condition)
  • assertTrue(message, condition)

11
  • import junit.framework.Test
  • import junit.framework.TestSuite
  • public class AllTests
  • public static Test suite()
  • TestSuite suite new TestSuite("Test for
    com.matei")
  • //JUnit-BEGIN
  • suite.addTestSuite(MoneyTest.class)
  • //JUnit-END
  • return suite

The Test Suite
12
JUnit for Eclipse
Write a Comment
User Comments (0)
About PowerShow.com