JUnit

From Wikipedia, the free encyclopedia
  (Redirected from Junit)
Jump to: navigation, search
JUnit
Developer(s) Kent Beck, Erich Gamma, David Saff, Mike Clark (University of Calgary)
Stable release 4.11[1] / November 14, 2012 (2012-11-14)
Written in Java
Operating system Cross-platform
Type Unit testing tool
License Common Public License
Website junit.org

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 framework resides under packages junit.framework for JUnit 3.8 and earlier and under org.junit for JUnit 4 and later.

Contents

Example of JUnit test fixture [edit]

A JUnit test fixture is a Java object. With older versions of JUnit, fixtures had to inherit from junit.framework.TestCase, but the new tests using JUnit 4 should not do this.[2] Test methods must be annotated by the @Test annotation. If the situation requires it,[3] it is also possible to define a method to execute before (or after) each (or all) of the test methods with the @Before (or @After) and @BeforeClass (or @AfterClass) annotations.[4]

import org.junit.*;
 
public class TestFoobar{
    @BeforeClass
    public static void setUpClass() throws Exception {
        // Code executed before the first test method       
    }
 
    @Before
    public void setUp() throws Exception {
        // Code executed before each test    
    }
 
    @Test
    public void testOneThing() {
        // Code that tests one thing
    }
 
    @Test
    public void testAnotherThing() {
        // Code that tests another thing
    }
 
    @Test
    public void testSomethingElse() {
        // Code that tests something else
    }
 
    @After
    public void tearDown() throws Exception {
        // Code executed after each test   
    }
 
    @AfterClass
    public static void tearDownClass() throws Exception {
        // Code executed after the last test method 
    }
}

Ports [edit]

JUnit alternatives have been written in other languages including:

See also [edit]

References [edit]

  1. ^ JUnit Releases
  2. ^ Kent Beck, Erich Gamma. "JUnit Cookbook". junit.sourceforge.net. Retrieved 2011-05-21. 
  3. ^ Kent Beck. "Expensive Setup Smell". C2 Wiki. Retrieved 2011-11-28. 
  4. ^ Kent Beck, Erich Gamma. "JUnit Cookbook". junit.sourceforge.net. Retrieved 2011-05-21. 

External links [edit]