JUnit 4 is a unit testing framework for Java that provides a structured approach to writing and running automated tests. At its core, it uses annotation-based test discovery to automatically identify test methods, and a pluggable runner architecture that controls how test classes are discovered, instantiated, and executed. The framework builds test execution around a chain of Statement objects, each wrapping the next to layer behaviors such as timeouts and retries, and uses Java reflection to dynamically invoke test methods and access private fields for setup and teardown operations.
The framework distinguishes itself through several extension and customization capabilities. It supports a rule-based extension model where reusable Rule objects intercept lifecycle events to add setup, cleanup, or behavior modification. Tests can be parameterized to run the same logic against multiple data sets, and categorized using annotations for selective inclusion or exclusion during test runs. The framework integrates Hamcrest matchers as assertion primitives, producing descriptive failure messages through a type-safe DSL, and supports theory-based test writing where test methods are automatically run against many generated data points.
JUnit 4 provides comprehensive testing capabilities including assertion testing, expected exception testing, test timeout enforcement, test condition assumptions, test disabling, and test execution order control. It manages test fixtures through setup and teardown methods that run before and after each test or before and after all tests in a class, and supports organizing multiple test classes into suites for batch execution. The framework also offers guidance for testing multithreaded code and provides repeatable test execution patterns that produce consistent, verifiable results.