JUnit

From Wikipedia, the free encyclopedia

Jump to: navigation, search
JUnit
Developed by Kent Beck, Erich Gamma, David Saff
Latest release 4.5 / 2008-08-08; 238 days ago
Written in Java
Operating system 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), Delphi (DUnit), Free Pascal (FPCUnit), 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.

JUnit is linked as a JAR at compile-time; the framework resides under packages junit.framework for JUnit 3.8 and earlier and under org.junit for JUnit 4 and later.

Contents

[edit] Examples

[edit] JUnit 3.8

A simple example for a test-case in JUnit 3.8 and earlier could be as follows:

import junit.framework.*;
 
public class MultiplicationTest extends TestCase {
  /** Test whether 3 * 2 = 6, according to the JVM. */
  public void testMultiplication() {
    assertEquals("Multiplication", 6, 3 * 2);
  }
}

(Compare with the similar example for Mauve.)

The method testMultiplication will be discovered automatically by reflection.

[edit] JUnit 4.0

Translating this above example into JUnit 4.0 results in:

import org.junit.*;
 
public class MultiplicationTest {
  /** Test whether 3 * 2 = 6, according to the JVM. */
  @Test
  public void testMultiplication() {
    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 JUnit framework and the core of the JVM and language.

There are, however, several issues to consider here. JUnit is not a programming language; this trivial example does not demonstrate the power of JUnit. It is conventional to see test case classes named as the class being tested, appended with "Test". Also, something more meaningful is usually printed in the assertion message, as in the following:

Assert.assertEquals("Test whether 2 * 2 = 4", 4, Multiplier.multiply(2, 2));

[edit] See also

[edit] External links

Personal tools