JUnit explained

JUnit
Developer:Kent Beck, Erich Gamma, David Saff, Kris Vasudevan
Latest Release Version:5.10.0
Latest Release Date:[1]
Operating System:Cross-platform
Programming Language:Java
Genre:Unit testing tool
License:Eclipse Public License 2.0[2] (relicensed previously)

JUnit is a test automation framework for the Java programming language. JUnit is often used for unit testing, and is one of the xUnit frameworks.

JUnit is linked as a JAR at compile-time. The latest version of the framework, JUnit 5, resides under package . Previous versions JUnit 4 and JUnit 3 were under packages and, 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.[3]

JUnit Lifecycle

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:

  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 annotation. The other type is setup before running each test case, which uses the annotation.
  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 annotation is used here.
  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 annotation is used to support class-level clean up. The annotation allows for cleanup after test execution.

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.

Build Tools

See main article: article and Build tool. JUnit supports Apache Ant, Apache Maven and Gradle build tools, which are the most widely used project build tools. Build tools are vital for automating the process of building the project.

Ant Extension

See main article: article and Apache Ant. 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.

Maven Extension

See main article: article and Apache Maven.

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 Extension

See main article: article and Gradle.

Gradle is a build tool that borrows many concepts from its predecessors, Ant and Maven. It uses the 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 and directories. Gradle can integrate with JUnit 5 by configuring a plugin alongside the junit-platform plug-in given by the JUnit 5 team in the build file.

JUnit Extension Model

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 the developer has to implement the entire life cycle even if they only need a specific life cycle stage. This is too complicated and heighweight for the majority of use cases. Another major limitation is that only one runner class is used for each test case, and makes them uncomposable. As an example, Mockito and Parameterized runners cannot exist within the same test class.

A major limitation of the Rule API is that it cannot control the entire life cycle of the test, so they cannot be used for every single use case. They are only appropriate when something needs to occur before or after test case execution. Another major limitation is that rules for class-level and method-level callbacks must be made 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:

  1. Test life cycle callbacks - This allows the developer to hook to certain phases of a test life cycle
  2. Test instance post-processing - this enables the developer to hook after test instance creation by implementing the TestInstancePostProcessor interface.
  3. Conditional test execution - this enables the developer to execute the test case only after meeting certain criteria.
  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.

Example of a JUnit test fixture

A JUnit test fixture is a Java object. Test methods must be annotated by the annotation. If the situation requires it,[4] it is also possible to define a method to execute before (or after) each (or all) of the test methods with the (or) and (or) annotations.[5]

import org.junit.jupiter.api.*;

class FoobarTests

Previous versions of JUnit

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

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

In JUnit 4, the annotations for test execution callbacks were,,, and, as opposed to JUnit 5's,,, and .[5]

In JUnit 3, test fixtures had to inherit from .[8] Additionally, test methods had to be prefixed with 'test'.[9]

See also

References

External links

Notes and References

  1. Web site: JUnit Releases . github.com. 2023-07-23.
  2. Web site: Change license to EPL v2.0 . github.com . 7 September 2017 . 2021-02-04.
  3. Web site: We Analyzed 30,000 GitHub Projects – Here Are The Top 100 Libraries in Java, JS and Ruby . 2014-02-09 . 2014-07-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/ . dead .
  4. Web site: Expensive Setup Smell. C2 Wiki. Kent Beck. Kent Beck. 2011-11-28.
  5. Web site: Writing Tests . junit.org . 2021-02-04.
  6. Web site: bliki: Xunit . 2022-03-07 . martinfowler.com.
  7. Web site: JUnit . mvnrepository.com . 29 October 2021.
  8. Web site: JUnit Cookbook. junit.sourceforge.net. Kent Beck. Erich Gamma. Kent Beck. Erich Gamma. 2011-05-21. 2020-06-15. https://web.archive.org/web/20200615030913/http://junit.sourceforge.net/doc/cookbook/cookbook.htm. dead.
  9. Web site: Migrating from JUnit 3 to JUnit 4: Nothing But Good News . Object Computing, Inc. . Charles A. Sharp . August 2007 . 2021-02-04.