Overview

Appendix A. Brief JUnit 4 tutorial

This appendix gives a compact introduction to JUnit 4 by showing the basic structure of a test class and the conventions that make tests work smoothly. It explains how assertions are typically brought in with a static import from the Assert class, how test classes should be clearly named, and how shared setup data is usually prepared in instance variables initialized before each test. It also emphasizes the importance of having a predictable test fixture so each test starts from a known state.

Beyond the core structure, the chapter describes the annotations that control the test lifecycle. A method marked with @Before is used to prepare state before each test, while @After is used for cleanup afterward. Any public void method annotated with @Test is treated as a test case by the JUnit runner, and such methods may throw exceptions without needing to catch them inside the test because JUnit handles them. The annotation can also support additional testing needs such as checking for expected exceptions and measuring execution time.

The overall message is that JUnit 4 keeps tests simple, readable, and organized by relying on clear naming, annotations, and small helper methods. Test methods should be distinguished from supporting code, and helper methods are fine as long as they do not resemble tests. Taken together, these conventions help developers write tests that are easy to run, understand, and maintain while keeping setup, execution, and cleanup separate and explicit.

FAQ

What is the purpose of using a static import for JUnit’s Assert class?It allows you to call assertion methods such as assertEquals() directly, without qualifying them with the class name, making test code shorter and easier to read.
How should a JUnit 4 test class typically be named?The class name should clearly indicate that it is a test, commonly by starting with Test or following a similar test-oriented naming convention.
What is the role of instance variables initialized in a @Before method?They represent the common test fixture shared by the test methods, providing a known starting state before each test runs.
How do you prepare a known state before each test in JUnit 4?Define a public method and annotate it with @Before. JUnit runs this method before each test method.
How do you clean up after a test in JUnit 4?Define a public method and annotate it with @After. JUnit runs this method after each test method to perform cleanup.
Which methods does JUnit 4 treat as test cases?Any public void methods annotated with @Test are recognized as test cases by the JUnit 4 test runner.
Can a JUnit 4 test method throw exceptions?Yes. Test methods can declare exceptions, and JUnit will catch them during test execution.
What additional capabilities does the @Test annotation provide?The @Test annotation supports features such as timing tests and checking for expected exceptions.
Can a test class include helper methods?Yes. You can include any number of helper methods as long as they do not match the pattern of a test method, such as being annotated with @Test.
Why is the @Before annotation useful in JUnit 4?It helps ensure each test begins from a predictable, known state, which improves test isolation and reliability.

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