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 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 LifecycleEvery 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]
Integration with other toolsJUnit 5 integrates a number of tools, such as build tools, integrated development environments (IDE), continuous integration (CI) tools and many more.[6] Build ToolsJUnit 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] Ant ExtensionApache 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 Maven ExtensionIn 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 Gradle ExtensionGradle is a build tool that borrows many concepts from its predecessors, Ant and Maven.[11] It uses the JUnit Extension ModelJUnit 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 developers must implement the entire test lifecycle, even if only a specific phase is required.[15] This is too complicated and heavyweight for most use cases.[15] Another major limitation is that only one runner class can be used per test case, which makes runners non-composable.[15] For example, Mockito and Parameterized runners cannot be used together within the same test class.[15] A major limitation of the Rule API is that it cannot control the entire test lifecycle and is therefore unsuitable for certain use cases.[15] Rules are only suitable for operations that need to run before or after test execution.[15] Another limitation is that class-level and method-level rules must be defined 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]
Example of a JUnit test fixtureA JUnit test fixture is a Java object. Test methods must be annotated by the 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 JUnitAccording to Martin Fowler, one of the early adopters of JUnit:[24]
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 In JUnit 3, test fixtures had to inherit from See also
Citations
References
External links
|