JUnit: Difference between revisions
Add integrating tools for JUnit. |
→Build Tools: Add clarification. Fixed grammar. Add in-line citation |
||
Line 34: | Line 34: | ||
JUnit supports [[Apache Ant]], [[Apache Maven]] and [[Gradle]] build tools, which are the most widely used project build tools. {{sfn|Gulati|Sharma|2017|loc=Chapter §6 Build Tools|pp=99-117}} Build tools are vital for automating the process of building the project. {{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools|p=99}} |
JUnit supports [[Apache Ant]], [[Apache Maven]] and [[Gradle]] build tools, which are the most widely used project build tools. {{sfn|Gulati|Sharma|2017|loc=Chapter §6 Build Tools|pp=99-117}} Build tools are vital for automating the process of building the project. {{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools|p=99}} |
||
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. |
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.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Ant|pp=108-112}} Ant centers around the "build.xml" file, used for configuring the tasks necessary to run a project.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Ant|pp=108-112}} |
||
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.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Gradle|pp=99-103}} 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.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Gradle|pp=99-103}} Maven can be used for any Java Project.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Gradle|pp=104-108}} It uses the Project Object Model (POM), which is an XML-based approach to configuring the build steps for the project.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Maven|pp=104-106}} The minimal Maven with the "pom.xml" build file must contain a list of dependencies and a unique project identifier.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Gradle|pp=104-108}} Maven must be available on the build path to work.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Gradle|pp=104-108}} |
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.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Gradle|pp=99-103}} 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.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Gradle|pp=99-103}} Maven can be used for any Java Project.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Gradle|pp=104-108}} It uses the Project Object Model (POM), which is an XML-based approach to configuring the build steps for the project.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Maven|pp=104-106}} The minimal Maven with the "pom.xml" build file must contain a list of dependencies and a unique project identifier.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Gradle|pp=104-108}} Maven must be available on the build path to work.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Gradle|pp=104-108}} |
||
Gradle is a build tool that borrows many |
Gradle is a build tool that borrows many concepts from its predecessors, Ant and Maven.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Gradle|pp=99-103}} It uses the build.gradle file to declare the steps required for the project build.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Gradle|pp=99-103}} Unlike Ant and Gradle, which are XML-based, Gradle requires the use of [[Apache Groovy]], which is a Java-based programming language.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Gradle|pp=99-103}} Unlike Ant and Maven, Gradle does not require the use of XML.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Gradle|pp=99-103}} Gradle still adheres to Maven's "convention over configuration" approach, and follows the same structure for "src/main/java" and "src/test/java" directories.{{sfn|Gulati|Sharma|2017|loc=Chapter §6 Integrating Tools - Build Tools - Gradle|pp=99-103}} |
||
==Example of a JUnit test fixture== |
==Example of a JUnit test fixture== |
Revision as of 23:54, 9 June 2023
Developer(s) | Kent Beck, Erich Gamma, David Saff, Kris Vasudevan |
---|---|
Stable release | 5.9.3
/ April 26, 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 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]
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]
- Setup phase - This phase is where the test infrastrcture 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] - Text execution - This phase is responsible for running the test and verifying the result. The test result will indicate whether if the test result is a success or a failure. The
@Test
annotation is used here.[5] - Clean up phase - After all posttest executions are performed, the systm may need to perform cleanup. Simiar 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), continouous 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]
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]
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.[9] 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.[9] 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.[11] 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]
Gradle is a build tool that borrows many concepts from its predecessors, Ant and Maven.[9] It uses the build.gradle file to declare the steps required for the project build.[9] Unlike Ant and Gradle, which are XML-based, Gradle requires the use of Apache Groovy, which is a Java-based programming language.[9] Unlike Ant and Maven, Gradle does not require the use of XML.[9] Gradle still adheres to Maven's "convention over configuration" approach, and follows the same structure for "src/main/java" and "src/test/java" directories.[9]
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,[12] 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.[13][14]
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:[15]
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.[16]
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
.[13][14]
In JUnit 3, test fixtures had to inherit from junit.framework.TestCase
.[17] Additionally, test methods had to be prefixed with 'test'.[18]
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
- ^ "JUnit Releases". github.com. Retrieved 2023-04-26.
- ^ "Change license to EPL v2.0". github.com. 7 September 2017. Retrieved 2021-02-04.
- ^ a b Gulati & Sharma 2017, p. 144, §Chapter 8 Dynamic Tests and Migration from Junit 4.
- ^ "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.
- ^ a b c d Gulati & Sharma 2017, pp. 37–40, Chapter §2 JUnit LifeCycle API.
- ^ a b Gulati & Sharma 2017, p. 99, Chapter §6 Integrating Tools.
- ^ Gulati & Sharma 2017, pp. 99–117, Chapter §6 Build Tools.
- ^ a b Gulati & Sharma 2017, pp. 108–112, Chapter §6 Integrating Tools - Build Tools - Ant.
- ^ a b c d e f g Gulati & Sharma 2017, pp. 99–103, Chapter §6 Integrating Tools - Build Tools - Gradle.
- ^ a b c Gulati & Sharma 2017, pp. 104–108, Chapter §6 Integrating Tools - Build Tools - Gradle.
- ^ Gulati & Sharma 2017, pp. 104–106, Chapter §6 Integrating Tools - Build Tools - Maven.
- ^ Kent Beck. "Expensive Setup Smell". C2 Wiki. Retrieved 2011-11-28.
- ^ a b "Writing Tests". junit.org. Retrieved 2021-02-04.
- ^ a b Gulati & Sharma 2017, p. 37-40, Chapter §2 Understanding CoreJunit 5.
- ^ "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.
References
- Gulati, Shekhar; Sharma, Rahul (2017). Java Unit Testing with JUnit 5. Apress.
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.