JUnit
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.
Developer(s) | Kent Beck, Erich Gamma, David Saff, Kris Vasudevan |
---|---|
Stable release | 5.9.1
/ September 20, 2022[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 linked as a JAR at compile-time. The latest version of the framework, JUnit 5, resides under package org.junit.jupiter
. Previous versions JUnit 4 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]
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]
import org.junit.jupiter.api.*;
public class FoobarTest {
@BeforeAll
public static void setUpClass() throws Exception {
// Code executed before the first test method
}
@BeforeEach
public void setUp() throws Exception {
// Code executed before each test
}
@Test
public void oneThing() {
// Code that tests one thing
}
@Test
public void anotherThing() {
// Code that tests another thing
}
@Test
public void somethingElse() {
// Code that tests something else
}
@AfterEach
public void tearDown() throws Exception {
// Code executed after each test
}
@AfterAll
public 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]
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]
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
- 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
References
- "JUnit Releases". github.com. Retrieved 2022-09-22.
- "Change license to EPL v2.0". github.com. 7 September 2017. Retrieved 2021-02-04.
- "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.
- Kent Beck. "Expensive Setup Smell". C2 Wiki. Retrieved 2011-11-28.
- "Writing Tests". junit.org. Retrieved 2021-02-04.
- "bliki: Xunit". martinfowler.com. Retrieved 2022-03-07.
- "JUnit". mvnrepository.com. Retrieved 29 October 2021.
- Kent Beck; Erich Gamma. "JUnit Cookbook". junit.sourceforge.net. Archived from the original on 2020-06-15. Retrieved 2011-05-21.
- Charles A. Sharp (August 2007). "Migrating from JUnit 3 to JUnit 4: Nothing But Good News". Object Computing, Inc. Retrieved 2021-02-04.
External links
- Official website
- Irriger, Axel. "JUnit - Open Source Java Unit Testing Tool". Methods and Tools.
- "JUnit". Memory Not Found. Tutorials. Archived from the original on Jan 28, 2015.