JUnit
Template:Short description Lua error in package.lua at line 80: module 'Module:Hatnote list' not found.
Page Module:Infobox/styles.css has no content.
| JUnit | |
|---|---|
| [[Programmer|DeveloperTemplate:Pluralize from text]] | Kent Beck, Erich Gamma, David Saff |
| Written in | Java |
| Engine | Page Template:Plainlist/styles.css has no content.Template:EditAtWikidata |
| Operating system | Cross-platform |
| Type | Unit testing tool |
| License | Eclipse Public License 2.0[1] (relicensed previously) |
| Website | junit |
| Repository | Page Template:Plainlist/styles.css has no content.
|
Script error: No such module "Check for conflicting parameters".
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 6, resides under package org.junit.jupiter.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. Previous versions JUnit 4Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. 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.[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:Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found.
- 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
@BeforeAllannotation. The other type is setup before running each test case, which uses the@BeforeEachannotation.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. - 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
@Testannotation is used here.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. - 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
@AfterAllannotation is used to support class-level clean up. The@AfterEachannotation allows for cleanup after test execution.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found.
Integration with other tools
JUnit 6 integrates a number of tools, such as build tools, integrated development environments (IDE), continuous integration (CI) tools and many more.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found.
Build Tools
Template:Main article JUnit supports Apache Ant, Apache Maven and Gradle build tools, which are the most widely used project build tools.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. Build tools are vital for automating the process of building the project. Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found.
Ant Extension
Template:Main article
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.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. Ant centers around the build.xml file, used for configuring the tasks necessary to run a project.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. 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 and later by configuring the Java code coverage tools (JaCoCo), for the ivy.xml file.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. The ivy.xml can then be configured with the java-platform-console and junit-platform-runner dependencies to integrate with JUnit 5 and later. Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found.
Maven Extension
In contrast to Ant, Apache Maven, also known as Maven, uses a standardized and unified approach to the build process.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. Maven follows the paradigm of "convention over configuration" for managing its dependencies.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. 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.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. Maven can be used for any Java Project.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. It uses the Project Object Model (POM), which is an XML-based approach to configuring the build steps for the project.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. The minimal Maven with the pom.xml build file must contain a list of dependencies and a unique project identifier.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. Maven must be available on the build path to work.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. Maven can integrate with JUnit 5 and later using the jacoco-maven-plugin plugin which supports out-of-box functionality for JUnit tests.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. Different Maven goals can be specified to achieve these tasks.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found.
Gradle Extension
Gradle is a build tool that borrows many concepts from its predecessors, Ant and Maven.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. It uses the build.gradle file to declare the steps required for the project build.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. Unlike Ant and Maven, which are XML-based, Gradle requires the use of Apache Groovy, which is a Java-based programming language.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. Unlike Ant and Maven, Gradle does not require the use of XML.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. Gradle still adheres to Maven's "convention over configuration" approach, and follows the same structure for src/main/java and src/test/java directories.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. Gradle can integrate with JUnit 5 and later by configuring a plugin jacoco alongside the junit-platform plug-in given by the JUnit team in the build file.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found.
JUnit Extension Model
JUnit follows the paradigm of preferring extension points over features.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. 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.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found.
In JUnit 4, there are two extension mechanisms: the Runner API and Rule API.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. 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.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. This is too complicated and heavyweight for most use cases.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. Another major limitation is that only one runner class can be used per test case, which makes runners non-composable.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. For example, Mockito and Parameterized runners cannot be used together within the same test class.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found.
A major limitation of the Rule API is that it cannot control the entire test lifecycle and is therefore unsuitable for certain use cases.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. Rules are only suitable for operations that need to run before or after test execution.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. Another limitation is that class-level and method-level rules must be defined separately.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found.
In JUnit 5 and later, the extension API is found within the JUnit Jupiter Engine.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. 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.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. Upon reaching a certain life cycle phase, the Jupiter Engine will invoke all registered extensions for that phase.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. The developer can hook into five major extension points:Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found.
- Test lifecycle callbacks – allow developers to execute code at specific test lifecycle phases.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found.
- Test instance post-processing – enables developers to hook into the test instance creation phase using the
TestInstancePostProcessorinterface.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found. - Conditional test execution – allows tests to run only if certain conditions are met.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found.
- Parameter resolution – allows parameters to be injected into test methods or constructors.
- Exception handling – allows developers to modify test behavior in response to exceptions instead of failing the test outright.Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found.
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,[4] 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.[5]Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found.
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:[6]
Page Template:Blockquote/styles.css has no content.
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.[7]
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.[5]Lua error in package.lua at line 80: module 'Module:Footnotes/anchor_id_list' not found.
In JUnit 3, test fixtures had to inherit from junit.framework.TestCase.[8] Additionally, test methods had to be prefixed with 'test'.[9]
See also
Lua error in mw.title.lua at line 404: bad argument #2 to 'title.new' (unrecognized namespace name 'Portal').
- 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
Page Template:Reflist/styles.css has no content.
- ^ Page Module:Citation/CS1/styles.css has no content."Change license to EPL v2.0". github.com. 7 September 2017. Retrieved 2021-02-04.
- ^ Page Module:Citation/CS1/styles.css has no content."JUnit Releases". github.com. Retrieved 2025-12-30.
- ^ Page Module:Citation/CS1/styles.css has no content."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.
- ^ Page Module:Citation/CS1/styles.css has no content.Kent Beck. "Expensive Setup Smell". C2 Wiki. Retrieved 2011-11-28.
- ^ a b Page Module:Citation/CS1/styles.css has no content."Writing Tests". junit.org. Retrieved 2021-02-04.
- ^ Page Module:Citation/CS1/styles.css has no content."bliki: Xunit". martinfowler.com. Retrieved 2022-03-07.
- ^ Page Module:Citation/CS1/styles.css has no content."JUnit". mvnrepository.com. Retrieved 29 October 2021.
- ^ Page Module:Citation/CS1/styles.css has no content.Kent Beck; Erich Gamma. "JUnit Cookbook". junit.sourceforge.net. Archived from the original on 2020-06-15. Retrieved 2011-05-21.
- ^ Page Module:Citation/CS1/styles.css has no content.Charles A. Sharp (August 2007). "Migrating from JUnit 3 to JUnit 4: Nothing But Good News". Object Computing, Inc. Retrieved 2021-02-04.
References
- Page Module:Citation/CS1/styles.css has no content.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
- Official website
- Page Module:Citation/CS1/styles.css has no content.Irriger, Axel. "JUnit - Open Source Java Unit Testing Tool". Methods and Tools.
- Page Module:Citation/CS1/styles.css has no content."JUnit". Memory Not Found. Tutorials. Archived from the original on Jan 28, 2015.