Developer(s) | Kent Beck, Erich Gamma, David Saff, Kris Vasudevan |
---|---|
Stable release | 5.10.0 / July 23, 2023 [1] |
Repository | |
Written in | Java |
Operating system | Cross-platform |
Type | Unit testing tool |
License | Eclipse Public License 2.0 [2] (relicensed previously) |
Website | junit |
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 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]
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: [5]
@BeforeAll
annotation. The other type is setup before running each test case, which uses the @BeforeEach
annotation. [5] @Test
annotation is used here. [5] @AfterAll
annotation is used to support class-level clean up. The @AfterEach
annotation allows for cleanup after test execution. [5] JUnit 5 integrates a number of tools, such as build tools, integrated development environments (IDE), continuous integration (CI) tools and many more. [6]
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]
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]
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 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 Maven, 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 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, there are two extension mechanisms: the Runner API and Rule API. [15] 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. [15] This is too complicated and heighweight for the majority of use cases. [15] Another major limitation is that only one runner class is used for each test case, and makes them uncomposable. [15] As an example, Mockito and Parameterized runners cannot exist within the same test class. [15]
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. [15] They are only appropriate when something needs to occur before or after test case execution. [15] Another major limitation is that rules for class-level and method-level callbacks must be made separately. [15]
In JUnit 5, the extension API is found within the JUnit Jupiter Engine. [16] 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. [16] Upon reaching a certain life cycle phase, the Jupiter Engine will invoke all registered extensions for that phase. [16] The developer can hook into five major extension points: [16]
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]
importorg.junit.jupiter.api.*;classFoobarTests{@BeforeAllstaticvoidsetUpClass()throwsException{// Code executed before the first test method}@BeforeEachvoidsetUp()throwsException{// Code executed before each test}@TestvoidoneThing(){// Code that tests one thing}@TestvoidanotherThing(){// Code that tests another thing}@TestvoidsomethingElse(){// Code that tests something else}@AfterEachvoidtearDown()throwsException{// Code executed after each test }@AfterAllstaticvoidtearDownClass()throwsException{// Code executed after the last test method }}
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]
Eclipse is an integrated development environment (IDE) used in computer programming. It contains a base workspace and an extensible plug-in system for customizing the environment. It is the second-most-popular IDE for Java development, and, until 2016, was the most popular. Eclipse is written mostly in Java and its primary use is for developing Java applications, but it may also be used to develop applications in other programming languages via plug-ins, including Ada, ABAP, C, C++, C#, Clojure, COBOL, D, Erlang, Fortran, Groovy, Haskell, JavaScript, Julia, Lasso, Lua, NATURAL, Perl, PHP, Prolog, Python, R, Ruby, Rust, Scala, and Scheme. It can also be used to develop documents with LaTeX and packages for the software Mathematica. Development environments include the Eclipse Java development tools (JDT) for Java and Scala, Eclipse CDT for C/C++, and Eclipse PDT for PHP, among others.
Unit testing, a.k.a. component or module testing, is a form of software testing by which isolated source code is tested to validate expected behavior.
Apache Ant is a software tool for automating software build processes for Java applications which originated from the Apache Tomcat project in early 2000 as a replacement for the Make build tool of Unix. It is similar to Make, but is implemented using the Java language and requires the Java platform. Unlike Make, which uses the Makefile format, Ant uses XML to describe the code build process and its dependencies.
Hibernate ORM is an object–relational mapping tool for the Java programming language. It provides a framework for mapping an object-oriented domain model to a relational database. Hibernate handles object–relational impedance mismatch problems by replacing direct, persistent database accesses with high-level object handling functions.
Maven is a build automation tool used primarily for Java projects. Maven can also be used to build and manage projects written in C#, Ruby, Scala, and other languages. The Maven project is hosted by The Apache Software Foundation, where it was formerly part of the Jakarta Project.
TestNG is a testing framework for the Java programming language created by Cedric_Beust and inspired by JUnit and NUnit. The design goal of TestNG is to cover a wider range of test categories: unit, functional, end-to-end, integration, etc., with more powerful and easy-to-use functionalities.
Apache Tapestry is an open-source component-oriented Java web application framework conceptually similar to JavaServer Faces and Apache Wicket. Tapestry was created by Howard Lewis Ship, and was adopted by the Apache Software Foundation as a top-level project in 2006.
XMLBeans is a Java-to-XML binding framework which is part of the Apache Software Foundation XML project.
The Spring Framework is an application framework and inversion of control container for the Java platform. The framework's core features can be used by any Java application, but there are extensions for building web applications on top of the Java EE platform. The framework does not impose any specific programming model.. The framework has become popular in the Java community as an addition to the Enterprise JavaBeans (EJB) model. The Spring Framework is free and open source software.
Apache Gump is an open source continuous integration system, which aims to build and test all the open source Java projects, every night. Its aim is to make sure that all the projects are compatible, at both the API level and in terms of functionality matching specifications. It is hosted at gump.apache.org
, and runs every night on the official Sun JVM.
Apache Ivy is a transitive package manager. It is a sub-project of the Apache Ant project, with which Ivy works to resolve project dependencies. An external XML file defines project dependencies and lists the resources necessary to build a project. Ivy then resolves and downloads resources from an artifact repository: either a private repository or one publicly available on the Internet.
Apache Empire-db is a Java library that provides a high level object-oriented API for accessing relational database management systems (RDBMS) through JDBC. Apache Empire-db is open source and provided under the Apache License 2.0 from the Apache Software Foundation.
Spring Roo is an open-source software tool that uses convention-over-configuration principles to provide rapid application development of Java-based enterprise software. The resulting applications use common Java technologies such as Spring Framework, Java Persistence API, Thymeleaf, Apache Maven and AspectJ. Spring Roo is a member of the Spring portfolio of projects.
Google Plugin for Eclipse (GPE) was a set of development tools that enabled Java developers to design, build, optimize, and deploy cloud computing applications. developers in creating complex user interfaces, generating Ajax code using the GWT Web Toolkit, and deploying applications to Google App Engine. GPE installed into the Eclipse integrated development environment (IDE) using the extensible plugin system. GPE was available under the Eclipse Public License 1.0.
Gradle is a build automation tool for multi-language software development. It controls the development process in the tasks of compilation and packaging to testing, deployment, and publishing. Supported languages include Java, C/C++, and JavaScript. Gradle builds on the concepts of Apache Ant and Apache Maven, and introduces a Groovy- and Kotlin-based domain-specific language contrasted with the XML-based project configuration used by Maven. Gradle uses a directed acyclic graph to determine the order in which tasks can be run, through providing dependency management. It runs on the Java Virtual Machine.
Java code coverage tools are of two types: first, tools that add statements to the Java source code and require its recompilation. Second, tools that instrument the bytecode, either before or during execution. The goal is to find out which parts of the code are tested by registering the lines of code executed when running a test.
MockServer is an open source mocking framework for HTTP and HTTPS released under the Apache License. MockServer is designed to simplify integration testing, by mocking HTTP and HTTPS system such as a web service or web site, and to decouple development teams, by allowing a team to develop against a service that is not complete or is unstable.
Spring Boot is an open-source Java framework used for programming standalone, production-grade Spring-based applications with minimal effort. Spring Boot is a convention-over-configuration extension for the Spring Java platform intended to help minimize configuration concerns while creating Spring-based applications. Most of the application can be preconfigured using Spring team's "opinionated view" of the best configuration and use of the Spring platform and third-party libraries.