Chapter 8: Unit Testing with JUnit - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Chapter 8: Unit Testing with JUnit

Description:

Test Case Class extends junit.framework.TestCase. Constructor with ... { String name = 'EllWood'; target.setName(name); assertEquals(name, target.getName()); 7 ... – PowerPoint PPT presentation

Number of Views:115
Avg rating:3.0/5.0
Slides: 11
Provided by: conest
Category:

less

Transcript and Presenter's Notes

Title: Chapter 8: Unit Testing with JUnit


1
Chapter 8Unit Testing with JUnit
Reference Building J2EE Apps with WebSphere
2
Objectives
  • Understand JUnit
  • Write a Test Case
  • Run a JUnit Test
  • Build a Test Suite
  • Run a Test Suite

3
What is JUnit?
  • Java-based testing framework
  • Available as a WSAD plug-in
  • JUnit test case
  • Runs under TestRunner provided by the framework
  • GUI interface
  • Text interface

4
Write a Test Case
  • Reference Page 157 - 161
  • General Steps
  • Test Case Class extends junit.framework.TestCase
  • Constructor with test name string
  • Instance variable for each test component
  • Optional setup() and tearDown()
  • Write a test for each functional unit
  • Use TestCase.assertX() methods (e.g.,
    assertEquals())
  • Adding junit.jar to build path
  • See Advances Tips page

5
WSAD TestCase Wizard
  • To add junit.jar
  • Properties Java Build Path Libraries
  • Add External JAR
  • To generate stubs
  • File New Other
  • Java JUnit TestCase
  • Browse Test Class
  • Next
  • Check test class checkbox (e.g., User)
  • Finish

6
Sample Test Method
  • Add the assertions
  • public void testName()
  • String name "EllWood"
  • target.setName(name)
  • assertEquals(name, target.getName())

7
Run a JUnit Test
  • Select the test case class
  • Run Run As JUnit Test
  • Press JUnit tab
  • Runs number of test cases
  • Errors unexpected error (e.g.,
    ClassCastException)
  • Failures assertion that fails

8
Build a Test Suite
  • Reference Pages 161 - 163
  • To run multiple test cases as a batch
  • Can add each test case method or add entire test
    case class
  • WSAD TestSuite Wizard
  • main() method runs all the tests
  • junit.textui.TestRunner.run(suite())
  • See Advances Tips page

9
WSAD TestSuite Wizard
  • To generate test suite
  • File New Other
  • Java JUnit TestSuite
  • Enter Test suite name
  • Select All
  • Check main() checkbox
  • Check Add TestRunner
  • Select text ui
  • Finish

10
Run a Test Suite
  • Reference Pages 163 164
  • Textual TestRunner
  • WSAD Generate Test Suite as text ui
  • Select AllTests.java
  • Run Run As Java Application
  • Results are displayed in the Console output.
  • Alternative
  • GUI TestRunner
Write a Comment
User Comments (0)
About PowerShow.com