HOME

TheInfoList



OR:

JUnit is a
test automation In software testing, test automation is the use of software separate from the software being tested to control the execution of tests and the comparison of actual outcomes with predicted outcomes. Test automation can automate some repetitive bu ...
framework for the
Java programming language Java is a high-level, general-purpose, memory-safe, object-oriented programming language. It is intended to let programmers ''write once, run anywhere'' ( WORA), meaning that compiled Java code can run on all platforms that support Jav ...
. JUnit is often used for
unit testing Unit testing, component or module testing, is a form of software testing by which isolated source code is tested to validate expected behavior. Unit testing describes tests that are run at the unit-level to contrast testing at the Integration ...
, 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 . Previous versions JUnit 4 and JUnit 3 were under packages and , 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.


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: # 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 annotation. The other type is setup before running each test case, which uses the annotation. # 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 annotation is used here. # 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 annotation is used to support class-level clean up. The annotation allows for cleanup after test execution.


Integration with other tools

JUnit 5 integrates a number of tools, such as build tools,
integrated development environment An integrated development environment (IDE) is a Application software, software application that provides comprehensive facilities for software development. An IDE normally consists of at least a source-code editor, build automation tools, an ...
s (IDE),
continuous integration Continuous integration (CI) is the practice of integrating source code changes frequently and ensuring that the integrated codebase is in a workable state. Typically, developers Merge (version control), merge changes to an Branching (revisio ...
(CI) tools and many more.


Build Tools

JUnit supports
Apache Ant 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 implement ...
,
Apache Maven 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 ...
and
Gradle Gradle is a build automation tool for multi-language software development. It manages tasks like compilation, packaging, testing, deployment, and publishing. Supported languages include Java (as well as JDK-based languages Kotlin, Groovy, Sc ...
build tools, which are the most widely used project build tools. Build tools are vital for automating the process of building the project.


Ant Extension

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. 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. The ivy.xml can then be configured with the java-platform-console and junit-platform-runner dependencies to integrate with JUnit 5.


Maven Extension

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. 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. Maven can be used for any Java Project. It uses the Project Object Model (POM), which is an XML-based approach to configuring the build steps for the project. The minimal Maven with the pom.xml build file must contain a list of dependencies and a unique project identifier. Maven must be available on the build path to work. Maven can integrate with JUnit 5 using the jacoco-maven-plugin plugin which supports out-of-box functionality for JUnit 5 tests. Different Maven goals can be specified to achieve these tasks.


Gradle Extension

Gradle is a build tool that borrows many concepts from its predecessors, Ant and Maven. It uses the file to declare the steps required for the project build. Unlike Ant and Maven, which are XML-based, Gradle requires the use of
Apache Groovy Apache Groovy is a Java-syntax-compatible object-oriented programming language for the Java platform. It is both a static and dynamic language with features similar to those of Python, Ruby, and Smalltalk. It can be used as both a programming l ...
, which is a Java-based programming language. Unlike Ant and Maven, Gradle does not require the use of XML. Gradle still adheres to Maven's "convention over configuration" approach, and follows the same structure for and directories. Gradle can integrate with JUnit 5 by configuring a plugin alongside the junit-platform plug-in given by the JUnit 5 team in the build file.


JUnit Extension Model

JUnit follows the paradigm of preferring extension points over features. 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. In JUnit 4, there are two extension mechanisms: the Runner API and Rule API. 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. This is too complicated and heavyweight for most use cases. Another major limitation is that only one runner class can be used per test case, which makes runners non-composable. For example, Mockito and Parameterized runners cannot be used together within the same test class. A major limitation of the Rule API is that it cannot control the entire test lifecycle and is therefore unsuitable for certain use cases. Rules are only suitable for operations that need to run before or after test execution. Another limitation is that class-level and method-level rules must be defined separately. In JUnit 5, the extension API is found within the JUnit Jupiter Engine. 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. Upon reaching a certain life cycle phase, the Jupiter Engine will invoke all registered extensions for that phase. The developer can hook into five major extension points: # Test lifecycle callbacks – allow developers to execute code at specific test lifecycle phases. # Test instance post-processing – enables developers to hook into the test instance creation phase using the TestInstancePostProcessor interface. # Conditional test execution – allows tests to run only if certain conditions are met. # 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.


Example of a JUnit test fixture

A JUnit
test fixture A test fixture is a device used to consistently test some item, device, or piece of software. Test fixtures are used in the testing of electronics, software and physical devices. Electronics In testing electronic equipment such as circuit boa ...
is a Java object. Test methods must be annotated by the
annotation An annotation is extra information associated with a particular point in a document or other piece of information. It can be a note that includes a comment or explanation. Annotations are sometimes presented Marginalia, in the margin of book page ...
. If the situation requires it, it is also possible to define a method to execute before (or after) each (or all) of the test methods with the (or ) and (or ) annotations. import org.junit.jupiter.api.*; class FoobarTests


Previous versions of JUnit

According to Martin Fowler, one of the early adopters of JUnit: 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. In JUnit 4, the annotations for test execution callbacks were , , , and , as opposed to JUnit 5's , , , and . In JUnit 3, test fixtures had to inherit from . Additionally, test methods had to be prefixed with 'test'.


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


References

*


External links

* * * {{cite web , url= http://memorynotfound.com/category/testing/junit , title= JUnit , series= Tutorials , website= Memory Not Found , url-status= live , archive-date= Jan 28, 2015 , archive-url= https://web.archive.org/web/20150128114403/http://memorynotfound.com/category/testing/junit Cross-platform software Extreme programming Free software programmed in Java (programming language) Java development tools Java platform Unit testing frameworks Software using the Eclipse Public License Articles with example Java code