Appendix C. JUnit 5

C.1. Introduction

Unlike previous versions of JUnit, JUnit 5 is composed of several different modules from three different sub-projects.

  • JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

JUnit 5 requires Java 8 (or higher) at runtime. However, you can still test code that has been compiled with previous versions of the JDK

C.2. New annotations

JUnit 5 replaces JUnit 4 annotations

  • @Before >> @BeforeEach
  • @BeforeClass >> @BeforeAll
  • @After >> @AfterEach
  • @AfterClass >> @AfterAll
  • @Ignore >> @Disabled

JUnit 5 also introductes new annotations

  • @DisplayName
  • @Nested
  • @ParameterizedTest

For detailed information on these annotations, please see JUnit 5 Annotations

C.3. New Assertions

JUnit 5 introduces new assertions

  • assertThrows
  • assertTimeout
  • assertAll

For detailed information on these annotations, please see JUnit 5 Assertions

C.4. @ExtendWith in JUnit 5

  • JUnit 5 has an extensible architecture which can be taken advantage of via the @ExtendWith annotation

For detailed information on these annotations, please see JUnit 5 Extension Model

C.5. Using JUnit 4 to run JUnit 5 test code

  • @RunWith(JUnitPlatform.class)

For detailed information on these annotations, please see JUnit 5 Using JUnit 4 to run JUnit Platform

C.6. Spring Support for JUnit 5

  • All core Spring TestContext Framework features are supported
  • Constructor and method injection via @Autowired, @Qualifier, @Value
  • @ExtendWith(SpringExtension.class)
  • @SpringJUnitConfig = @ExtendWith(SpringExtension.class) + @ContextConfiguration

For detailed information on these annotations, please see Spring TestContext Framework Extension for JUnit 5