Overview

Appendix B. Brief JUnit 3.8 tutorial

This appendix gives a compact introduction to the classic JUnit 3.8 style of writing tests in Java. It explains the basic structure of a test class, emphasizing that test classes are typically named to show their purpose, inherit from TestCase, and use a shared fixture to support multiple test methods. The overall idea is to make tests easy to recognize, easy to run, and easy to maintain.

A central theme is how JUnit 3.8 discovers and executes tests. Any public void method whose name starts with test is treated as a test case, and those methods may throw exceptions because JUnit handles them. The chapter also describes the role of setUp() for preparing a known starting state before each test and tearDown() for cleaning up afterward, with the recommendation to use @Override in Java 5 and later to reduce mistakes. Helper methods are allowed as long as they do not match the test method naming pattern.

It also highlights the conveniences that come with extending TestCase, especially the built-in assertion methods such as assertEquals(). Taken together, these conventions show how JUnit 3.8 supports a simple and disciplined testing workflow: define a test class, prepare reusable test data, write clearly named test methods, and rely on the framework to run the tests and report failures.

FAQ

What naming convention does JUnit 3.8 use to recognize a test class?JUnit 3.8 test classes should have names that indicate they are tests, typically by starting with Test.
How do you define a JUnit 3.8 test class?A JUnit 3.8 test class is defined by extending JUnit's TestCase class.
What is the purpose of the setUp() method in JUnit 3.8?setUp() prepares a known starting state for each test and provides the common fixture used by the test methods.
Why would you override setUp() in a JUnit 3.8 test?You override setUp() to initialize objects and establish a consistent, known state before each test runs.
Should setUp() and tearDown() use @Override in Java 5?Yes. If you're using Java 5, adding @Override to setUp() and tearDown() is a good idea because it helps catch typos and signature mistakes.
What is the purpose of the tearDown() method in JUnit 3.8?tearDown() is used to clean up after a test has run, such as releasing resources or resetting state.
How does JUnit 3.8 identify test methods?JUnit 3.8 recognizes all public void methods whose names start with test as test cases when run with the TestRunner.
Can a JUnit 3.8 test method throw exceptions?Yes. Test methods may declare any exceptions, and JUnit will catch them.
Can a JUnit 3.8 test class contain helper methods?Yes. You can add any number of helper methods as long as they do not look like test methods—for example, they should not be public void methods starting with test.
What assertion support do you get by extending TestCase?Extending TestCase gives you built-in assertion methods such as assertEquals() and other common JUnit assertions.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Test Driven Test Driven ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Test Driven Test Driven ebook for free