| Developed by | Kent Beck, Erich Gamma, David Saff |
|---|---|
| Latest release | 4.5 / 08 August 2008 |
| Written in | Java |
| OS | Cross-platform |
| Type | Unit testing tool |
| License | Common Public License |
| Website | http://junit.org |
JUnit is a unit testing framework for the Java programming language. Created by Kent Beck and Erich Gamma, JUnit is one of the xUnit family of frameworks that originated with Kent Beck's SUnit. JUnit has spawned its own ecosystem of JUnit extensions.
Experience gained with JUnit has been important in the development of test-driven development, and as a result, some knowledge of JUnit is often presumed in discussions of test-driven development.[citation needed]
JUnit has been ported to other languages, including PHP (PHPUnit), C# (NUnit), Python (PyUnit), Fortran (fUnit), Perl (Test::Class and Test::Unit), C++ (CPPUnit) and JavaScript (JSUnit) (see full list). This family of unit testing frameworks is referred to collectively as xUnit. TestNG has many of the same goals as JUnit.
Contents |
Examples
JUnit 3.8
A simple "Hello world" example in JUnit 3.8 and earlier:
public class HelloWorld extends TestCase { public void testMultiplication() { // Testing if 3*2=6: assertEquals ("Multiplication", 6, 3*2); } }
(compare with the similar example for Mauve.)
The method testMultiplication will be discovered automatically by reflection.
JUnit 4.0
Translating this above "Hello world" example into JUnit 4.0 results in:
public class HelloWorld { @Test public void testMultiplication() { // Testing if 3*2=6: Assert.assertEquals ("Multiplication", 6, 3*2); } }
The method testMultiplication will be discovered automatically by its Test Annotation (a feature of Java 5). It offers a fundamental test using only the framework and the core of the Java virtual machine and language specifications.
But there are several issues to consider here: JUnit is not a programming language, so this trivial example does not demonstrate the power of JUnit. It is conventional to see test case class names end with "Test", as well as the prefix name specified as the name of the class being tested. Also, something more meaningful is usually printed in the assertion message. The unit test subclasses the base class TestCase as in the following example:
public class MultiplierTest extends TestCase { public void testMultiplication() { // Testing if the Multiplier class agrees that, 2*2=4: assertEquals ("Multiplication basic test case 2*2", 4, Multiplier.multiply (2, 2) ); } }
See also
- JTiger, an alternative to JUnit
- Mock object
External links
- JUnit home page
- Unit tests with JUnit
- JUnit antipatterns (developerWorks) and JUnit antipatterns (Exubero)
- An early look at JUnit 4
- Get Acquainted with the New Advanced Features of JUnit 4
- JUnitFactory - Free web-based generation of JUnit characterization tests
- JUnitDoclet - Test Suite Generator for JUnit-Tests
- merobase - the first code search engine worldwide that supports JUnit test cases for test-driven component retrieval
No comments have been added.





