JUnit

Short description: Testing software

JUnitDeveloper(s)| Kent Beck, Erich Gamma, David Saff, Kris Vasudevan

|

Stable release|

5.9.3 / April 26, 2023; 6 months ago (2023-04-26)[1]

Written in| Java
Operating system| Cross-platform
Type| Unit testing tool
License| Eclipse Public License 2.0[2] (relicensed previously)
Website| junit.org

JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks which is collectively known as xUnit that originated with SUnit.

JUnit is linked as a JAR at compile-time. The latest version of the framework, JUnit 5, resides under package `org.junit.jupiter`.[3] Previous versions JUnit 4[3] and JUnit 3 were under packages `org.junit` and `junit.framework`, respectively.

A research survey performed in 2013 across 10,000 Java projects hosted on GitHub found that JUnit (in a tie with slf4j-api) was the most commonly included external library. Each library was used by 30.7% of projects.[4]

## Contents

* 1 JUnit Life Cycle
* 2 Integration with other tools
* 2.1 Build Tools
* 2.1.1 Ant Extension
* 2.1.2 Maven Extension
* 2.1.3 Gradle Extension
* 3 JUnit Extension Model
* 4 Example of a JUnit test fixture
* 5 Previous versions of JUnit
* 6 See also
* 7 Citations
* 8 References
* 9 External links

## JUnit Life Cycle

Every JUnit test class usually has several test cases. These test cases are subject to the test life cycle. The full JUnit Life Cycle has three major phases:[5]

1. Setup phase - This phase is where the test infrastructure is prepared. Two levels of setup are available. The first type of setup is class-level setup in which a computationally expensive object, such as a database connection, is created and reused, with minimal side effects. Class-level setup is implemented using the `@BeforeAll` annotation. The other type is setup before running each test case, which uses the `@BeforeEach` annotation.[5]
2. Test execution - This phase is responsible for running the test and verifying the result. The test result will indicate if the test result is a success or a failure. The `@Test` annotation is used here.[5]
3. Clean up phase - After all posttest executions are performed, the system may need to perform cleanup. Similar to class-level setup, there is a corresponding class-level clean up. The `@AfterAll` annotation is used to support class-level clean up. The `@AfterEach` annotation allows for cleanup after test execution.[5]

## Integration with other tools

JUnit 5 integrates a number of tools, such as build tools, integrated development environments (IDE), continuous integration (CI) tools and many more.[6]

### Build Tools

JUnit supports Apache Ant, Apache Maven and Gradle build tools, which are the most widely used project build tools. [7] Build tools are vital for automating the process of building the project. [6]

#### Ant Extension

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.[8] Ant centers around the `build.xml` file, used for configuring the tasks necessary to run a project.[8] 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.[8] The `ivy.xml` can then be configured with the `java-platform-console` and `junit-platform-runner` dependencies to integrate with JUnit 5. [9]

#### Maven Extension

In contrast to Ant, Apache Maven, also known as Maven, uses a standardized and unified approach to the build process.[10] Maven follows the paradigm of "convention over configuration" for managing its dependencies.[11] 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.[11] Maven can be used for any Java Project.[10] It uses the Project Object Model (POM), which is an XML-based approach to configuring the build steps for the project.[10] The minimal Maven with the `pom.xml` build file must contain a list of dependencies and a unique project identifier.[10] Maven must be available on the build path to work.[10] Maven can integrate with JUnit 5 using the `jacoco-maven-plugin` plugin which supports out-of-box functionality for JUnit 5 tests.[12] Different Maven goals can be specified to achieve these tasks.[12]

#### Gradle Extension

Gradle is a build tool that borrows many concepts from its predecessors, Ant and Maven.[11] It uses the `build.gradle` file to declare the steps required for the project build.[11] Unlike Ant and Gradle, which are XML-based, Gradle requires the use of Apache Groovy, which is a Java-based programming language.[11] Unlike Ant and Maven, Gradle does not require the use of XML.[11] Gradle still adheres to Maven's "convention over configuration" approach, and follows the same structure for `src/main/java` and `src/test/java` directories.[11] 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.[13]

## JUnit Extension Model

JUnit follows the paradigm of preferring extension points over features.[14] 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.[14] In JUnit 4, this was achieved using the Runner API and Rule API.[15] In JUnit 5, the extension API is found within the JUnit Jupiter Engine.[16] The JUnit Team wants to grant the developer the power to hook to separate stages of a test life cycle.[16] In particular, this occurs when the Jupiter Engine is able to invoke all registered extension when a certain life cycle phase is reached.[16] The developer can hook into five major extension points:[16]

1. Test life cycle callbacks - This allows the developer to hook to certain phases of a test life cycle[17]
2. Test instance post-processing - this enables the developer to hook after test instance creation by implementing the TestInstancePostProcessor interface.[18]
3. Conditional test execution - this enables the developer to execute the test case only after meeting certain criteria.[19]
4. Parameter resolution - This enables the developer to resolve a parameter after receiving it from a test method or constructor.
5. Exception handling - A use case for Exception handling is to change the testing behavior instead of throwing an exception.[20]

## Example of a JUnit test fixture

A JUnit test fixture is a Java object. Test methods must be annotated by the `@Test` annotation. If the situation requires it,[21] it is also possible to define a method to execute before (or after) each (or all) of the test methods with the `@BeforeEach` (or `@AfterEach`) and `@BeforeAll` (or `@AfterAll`) annotations.[22][23]

import org.junit.jupiter.api.*;

class FoobarTests {
@BeforeAll
static void setUpClass() throws Exception {
// Code executed before the first test method

@BeforeEach
void setUp() throws Exception {
// Code executed before each test

@Test
void oneThing() {
// Code that tests one thing

@Test
void anotherThing() {
// Code that tests another thing

@Test
void somethingElse() {
// Code that tests something else

@AfterEach
void tearDown() throws Exception {
// Code executed after each test

@AfterAll
static void tearDownClass() throws Exception {
// Code executed after the last test method

## Previous versions of JUnit

According to Martin Fowler, one of the early adopters of JUnit:[24]

> JUnit was born on a flight from Zurich to the 1997 OOPSLA in Atlanta. Kent was flying with Erich Gamma, and what else were two geeks to do on a long flight but program? The first version of JUnit was built there, pair programmed, and done test first (a pleasing form of meta-circular geekery).

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.[25]

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`.[22][23]

In JUnit 3, test fixtures had to inherit from `junit.framework.TestCase`.[26] Additionally, test methods had to be prefixed with 'test'.[27]

## See also

* xUnit, the family name given to testing frameworks including JUnit
* SUnit, the original Smalltalk version written by Kent Beck based on which JUnit was written
* TestNG, another test framework for Java
* Mock object, a technique used during unit testing
* Mockito, a mocking library to assist in writing tests
* EvoSuite, a tool to automatically generate JUnit tests
* List of Java Frameworks

## Citations

1. ↑ "JUnit Releases". https://github.com/junit-team/junit5/releases.
2. ↑ "Change license to EPL v2.0". 7 September 2017. https://github.com/junit-team/junit5/issues/1045.
3. ↑ 3.0 3.1 Gulati & Sharma 2017, p. 144, §Chapter 8 Dynamic Tests and Migration from Junit 4.
4. ↑ "We Analyzed 30,000 GitHub Projects – Here Are The Top 100 Libraries in Java, JS and Ruby". http://www.takipiblog.com/2013/11/20/we-analyzed-30000-github-projects-here-are-the-top-100-libraries-in-java-js-and-ruby/.
5. ↑ 5.0 5.1 5.2 5.3 Gulati & Sharma 2017, pp. 37-40, Chapter §2 JUnit LifeCycle API.
6. ↑ 6.0 6.1 Gulati & Sharma 2017, p. 99, Chapter §6 Integrating Tools.
7. ↑ Gulati & Sharma 2017, pp. 99-117, Chapter §6 Build Tools.
8. ↑ 8.0 8.1 8.2 Gulati & Sharma 2017, pp. 108-112, Chapter §6 Integrating Tools - Build Tools - Ant.
9. ↑ Gulati & Sharma 2017, pp. 116-117, Chapter §6 Integrating Tools - Build Tools - Ant Extension.
10. ↑ 10.0 10.1 10.2 10.3 10.4 Gulati & Sharma 2017, pp. 104-108, Chapter §6 Integrating Tools - Build Tools - Maven.
11. ↑ 11.0 11.1 11.2 11.3 11.4 11.5 11.6 Gulati & Sharma 2017, pp. 99-103, Chapter §6 Integrating Tools - Build Tools - Gradle.
12. ↑ 12.0 12.1 Gulati & Sharma 2017, p. 115, Chapter §6 Integrating Tools - Build Tools - Maven Extension.
13. ↑ Gulati & Sharma 2017, pp. 113-114, Chapter §6 Integrating Tools - Build Tools - Gradle Extension.
14. ↑ 14.0 14.1 Gulati & Sharma 2017, p. 121, Chapter §7 JUnit 5 Extension Model.
15. ↑ Gulati & Sharma 2017, pp. 121-122, Chapter §7 JUnit 4 Extension Model.
16. ↑ 16.0 16.1 16.2 16.3 Gulati & Sharma 2017, pp. 122-124, Chapter §7 JUnit 5 Extension Model - JUnit 5 Extension Model.
17. ↑ Gulati & Sharma 2017, pp. 124-126, Chapter §7 JUnit 5 Extension Model - Test Life Cycle Callbacks.
18. ↑ Gulati & Sharma 2017, pp. 126-127, Chapter §7 JUnit 5 Extension Model - Test Instance Post-Processing.
19. ↑ Gulati & Sharma 2017, p. 127, Chapter §7 JUnit 5 Extension Model - Conditional Test Execution.
20. ↑ Gulati & Sharma 2017, p. 129, Chapter §7 JUnit 5 Extension Model - Exception Handling.
21. ↑ Kent Beck. "Expensive Setup Smell". C2 Wiki. http://c2.com/cgi/wiki?ExpensiveSetUpSmell.
22. ↑ 22.0 22.1 "Writing Tests". https://junit.org/junit5/docs/current/user-guide/#writing-tests.
23. ↑ 23.0 23.1 Gulati & Sharma 2017, p. 37-40, Chapter §2 Understanding CoreJunit 5.
24. ↑ "bliki: Xunit". https://martinfowler.com/bliki/Xunit.html.
25. ↑ "JUnit". https://mvnrepository.com/artifact/junit/junit/usages.
26. ↑ "JUnit Cookbook". junit.sourceforge.net. http://junit.sourceforge.net/doc/cookbook/cookbook.htm.
27. ↑ "Migrating from JUnit 3 to JUnit 4: Nothing But Good News". Object Computing, Inc.. August 2007. https://objectcomputing.com/resources/publications/sett/august-2007-migrating-from-junit-3-to-junit-4-nothing-but-good-news.

## References

* 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.

## External links

* Irriger, Axel. "JUnit - Open Source Java Unit Testing Tool". http://www.methodsandtools.com/tools/tools.php?junit.
* "JUnit". http://memorynotfound.com/category/testing/junit.

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/JUnit. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Software:JUnit&oldid=3057754"