Every JUnit test class usually has several test cases. These test cases are subject to the test life cycle. The full JUnit Lifecycle has three major phases:
Apache Ant, also known as Ant, is one of the build tools with the highest degree of versatility, and has the longest history out of the three build tools listed above. Ant centers around the build.xml file, used for configuring the tasks necessary to run a project. Ant also has an extension called Apache Ivy, which helps deal with dependency resolution. The project dependencies can be declared in the ivy.xml file. Ant can integrate with JUnit 5 by configuring the Java code coverage tools (JaCoCo), for the ivy.xml file. The ivy.xml can then be configured with the java-platform-console and junit-platform-runner dependencies to integrate with JUnit 5.
In contrast to Ant, Apache Maven, also known as Maven, uses a standardized and unified approach to the build process. Maven follows the paradigm of "convention over configuration" for managing its dependencies. The Java source code (or "src") can be found under the src/main/java directory, and the test files can be found under the src/test/java directory. Maven can be used for any Java Project. It uses the Project Object Model (POM), which is an XML-based approach to configuring the build steps for the project. The minimal Maven with the pom.xml build file must contain a list of dependencies and a unique project identifier. Maven must be available on the build path to work. Maven can integrate with JUnit 5 using the jacoco-maven-plugin plugin which supports out-of-box functionality for JUnit 5 tests. Different Maven goals can be specified to achieve these tasks.
Gradle is a build tool that borrows many concepts from its predecessors, Ant and Maven. It uses the build.gradle file to declare the steps required for the project build. Unlike Ant and Maven, which are XML-based, Gradle requires the use of Apache Groovy, which is a Java-based programming language. Unlike Ant and Maven, Gradle does not require the use of XML. Gradle still adheres to Maven's "convention over configuration" approach, and follows the same structure for src/main/java and src/test/java directories. Gradle can integrate with JUnit 5 by configuring a plugin jacoco alongside the junit-platform plug-in given by the JUnit 5 team in the build file.
JUnit follows the paradigm of preferring extension points over features. The JUnit team decided not to put all features within the JUnit core, and instead decided to give an extensible way for developers to address their concerns.
In JUnit 4, there are two extension mechanisms: the Runner API and Rule API. There were some disadvantages to both the Runner API and the Rule API.
A major limitation of the Runner API is that developers must implement the entire test lifecycle, even if only a specific phase is required. This is too complicated and heavyweight for most use cases. Another major limitation is that only one runner class can be used per test case, which makes runners non-composable. For example, Mockito and Parameterized runners cannot be used together within the same test class.
A major limitation of the Rule API is that it cannot control the entire test lifecycle and is therefore unsuitable for certain use cases. Rules are only suitable for operations that need to run before or after test execution. Another limitation is that class-level and method-level rules must be defined separately.
In JUnit 5, the extension API is found within the JUnit Jupiter Engine. The JUnit Team wants to allow the developer to hook to separate stages of a test life cycle by providing a single unified extension API. Upon reaching a certain life cycle phase, the Jupiter Engine will invoke all registered extensions for that phase. The developer can hook into five major extension points:
As a side effect of its wide use, previous versions of JUnit remain popular, with JUnit 4 having over 100,000 usages by other software components on the Maven Central repository.
In JUnit 4, the annotations for test execution callbacks were @BeforeClass, @Before, @After, and @AfterClass, as opposed to JUnit 5's @BeforeAll, @BeforeEach, @AfterEach, and @AfterAll.
In JUnit 3, test fixtures had to inherit from junit.framework.TestCase. Additionally, test methods had to be prefixed with 'test'.
Gulati & Sharma 2017, p. 144, §Chapter 8 Dynamic Tests and Migration from Junit 4. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, p. 144, §Chapter 8 Dynamic Tests and Migration from Junit 4. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
"We Analyzed 30,000 GitHub Projects – Here Are The Top 100 Libraries in Java, JS and Ruby". Archived from the original on 2014-07-09. Retrieved 2014-02-09. https://web.archive.org/web/20140709221250/http://www.takipiblog.com/2013/11/20/we-analyzed-30000-github-projects-here-are-the-top-100-libraries-in-java-js-and-ruby/
Gulati & Sharma 2017, pp. 37–40, Chapter §2 JUnit LifeCycle API. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 37–40, Chapter §2 JUnit LifeCycle API. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 37–40, Chapter §2 JUnit LifeCycle API. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 37–40, Chapter §2 JUnit LifeCycle API. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, p. 99, Chapter §6 Integrating Tools. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 99–117, Chapter §6 Build Tools. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, p. 99, Chapter §6 Integrating Tools. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 108–112, Chapter §6 Integrating Tools - Build Tools - Ant. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 108–112, Chapter §6 Integrating Tools - Build Tools - Ant. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 108–112, Chapter §6 Integrating Tools - Build Tools - Ant. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 116–117, Chapter §6 Integrating Tools - Build Tools - Ant Extension. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 104–108, Chapter §6 Integrating Tools - Build Tools - Maven. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 99–103, Chapter §6 Integrating Tools - Build Tools - Gradle. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 99–103, Chapter §6 Integrating Tools - Build Tools - Gradle. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 104–108, Chapter §6 Integrating Tools - Build Tools - Maven. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 104–108, Chapter §6 Integrating Tools - Build Tools - Maven. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 104–108, Chapter §6 Integrating Tools - Build Tools - Maven. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 104–108, Chapter §6 Integrating Tools - Build Tools - Maven. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, p. 115, Chapter §6 Integrating Tools - Build Tools - Maven Extension. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, p. 115, Chapter §6 Integrating Tools - Build Tools - Maven Extension. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 99–103, Chapter §6 Integrating Tools - Build Tools - Gradle. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 99–103, Chapter §6 Integrating Tools - Build Tools - Gradle. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 99–103, Chapter §6 Integrating Tools - Build Tools - Gradle. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 99–103, Chapter §6 Integrating Tools - Build Tools - Gradle. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 99–103, Chapter §6 Integrating Tools - Build Tools - Gradle. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 113–114, Chapter §6 Integrating Tools - Build Tools - Gradle Extension. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, p. 121, Chapter §7 JUnit 5 Extension Model. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, p. 121, Chapter §7 JUnit 5 Extension Model. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 121–122, Chapter §7 JUnit 4 Extension Model. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 121–122, Chapter §7 JUnit 4 Extension Model. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 121–122, Chapter §7 JUnit 4 Extension Model. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 121–122, Chapter §7 JUnit 4 Extension Model. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 121–122, Chapter §7 JUnit 4 Extension Model. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 121–122, Chapter §7 JUnit 4 Extension Model. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 121–122, Chapter §7 JUnit 4 Extension Model. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 121–122, Chapter §7 JUnit 4 Extension Model. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 122–124, Chapter §7 JUnit 5 Extension Model - JUnit 5 Extension Model. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 122–124, Chapter §7 JUnit 5 Extension Model - JUnit 5 Extension Model. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 122–124, Chapter §7 JUnit 5 Extension Model - JUnit 5 Extension Model. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 122–124, Chapter §7 JUnit 5 Extension Model - JUnit 5 Extension Model. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 124–126, Chapter §7 JUnit 5 Extension Model - Test Life Cycle Callbacks. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, pp. 126–127, Chapter §7 JUnit 5 Extension Model - Test Instance Post-Processing. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, p. 127, Chapter §7 JUnit 5 Extension Model - Conditional Test Execution. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Gulati & Sharma 2017, p. 129, Chapter §7 JUnit 5 Extension Model - Exception Handling. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Kent Beck. "Expensive Setup Smell". C2 Wiki. Retrieved 2011-11-28. /wiki/Kent_Beck
"Writing Tests". junit.org. Retrieved 2021-02-04. https://junit.org/junit5/docs/current/user-guide/#writing-tests
Gulati & Sharma 2017, p. 37-40, Chapter §2 Understanding CoreJunit 5. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
"bliki: Xunit". martinfowler.com. Retrieved 2022-03-07. https://martinfowler.com/bliki/Xunit.html
"JUnit". mvnrepository.com. Retrieved 29 October 2021. https://mvnrepository.com/artifact/junit/junit/usages
"Writing Tests". junit.org. Retrieved 2021-02-04. https://junit.org/junit5/docs/current/user-guide/#writing-tests
Gulati & Sharma 2017, p. 37-40, Chapter §2 Understanding CoreJunit 5. - Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Berkeley, CA: Apress. doi:10.1007/978-1-4842-3015-2. ISBN 978-1-4842-3014-5. https://doi.org/10.1007%2F978-1-4842-3015-2
Kent Beck; Erich Gamma. "JUnit Cookbook". junit.sourceforge.net. Archived from the original on 2020-06-15. Retrieved 2011-05-21. /wiki/Kent_Beck
Charles A. Sharp (August 2007). "Migrating from JUnit 3 to JUnit 4: Nothing But Good News". Object Computing, Inc. Retrieved 2021-02-04. https://objectcomputing.com/resources/publications/sett/august-2007-migrating-from-junit-3-to-junit-4-nothing-but-good-news