JUnit
Encyclopedia
JUnit is a unit testing framework
Software framework
In computer programming, a software framework is an abstraction in which software providing generic functionality can be selectively changed by user code, thus providing application specific software...

 for the Java programming language
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

. JUnit has been important in the development of test-driven development
Test-driven development
Test-driven development is a software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new...

, and is one of a family of unit testing frameworks collectively known as xUnit
XUnit
Various code-driven testing frameworks have come to be known collectively as xUnit. These frameworks allow testing of different elements of software, such as functions and classes...

 that originated with SUnit
SUnit
SUnit is a unit testing framework for the programming language Smalltalk. It is the original source of the xUnit design, originally written by the creator of Extreme Programming, Kent Beck. SUnit allows writing tests and checking results in Smalltalk...

.

JUnit is linked as a JAR
JAR (file format)
In software, JAR is an archive file format typically used to aggregate many Java class files and associated metadata and resources into one file to distribute application software or libraries on the Java platform.JAR files are built on the ZIP file format and have the .jar file extension...

 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.

Example of JUnit test fixture

A JUnit Test fixture
Test fixture
A test fixture is something used to consistently test some item, device, or piece of software.-Electronics:Circuit boards, electronic components, and chips are held in place and subjected to controlled electronic test signals. One example is a bed of nails tester.-Software:Test fixture refers to...

 is a Java object. With older versions of JUnit, fixtures had to inherit from junit.framework.TestCase, but new tests using JUnit 4 should not do this. Test methods must be annotated by the @Test annotation
Java annotation
An annotation, in the Java computer programming language, is a special form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated...

. 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 @Before (or @After) and @BeforeClass (or @AfterClass) annotations.


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

JUnit has been ported to other languages including:
  • Actionscript
    ActionScript
    ActionScript is an object-oriented language originally developed by Macromedia Inc. . It is a dialect of ECMAScript , and is used primarily for the development of websites and software targeting the Adobe Flash Player platform, used on Web pages in the form of...

     (FlexUnit)
  • Ada
    Ada (programming language)
    Ada is a structured, statically typed, imperative, wide-spectrum, and object-oriented high-level computer programming language, extended from Pascal and other languages...

     (AUnit)
  • C
    C
    Ĉ or ĉ is a consonant in Esperanto orthography, representing the sound .Esperanto orthography uses a diacritic for all four of its postalveolar consonants, as do the Latin-based Slavic alphabets...

     (CUnit)
  • C# (NUnit
    NUnit
    NUnit is an open source unit testing framework for Microsoft .NET. It serves the same purpose as JUnit does in the Java world, and is one of many in the xUnit family.- Example :Example of an NUnit test fixture:using NUnit.Framework; [TestFixture]...

    )
  • C++
    C++
    C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

     (CPPUnit
    CPPUnit
    CppUnit is a unit testing framework module for the C++ programming language, described as a C++ port of JUnit. The library is released under the GNU Lesser General Public License. The library can be compiled for a variety of POSIX platforms, allowing unit-testing of 'C' sources as well as C++ with...

    )
  • Fortran
    Fortran
    Fortran is a general-purpose, procedural, imperative programming language that is especially suited to numeric computation and scientific computing...

     (fUnit
    FUnit
    fUnit is a unit testing framework for Fortran, in the flavor of other xUnit testing frameworks.-External links:*.*....

    )
  • Delphi (DUnit
    DUnit
    DUnit is an automated unit testing framework for Borland Delphi. DUnit allows Delphi developers to use test-driven development.DUnit's original code was written by Juanco Anez and is based on JUnit by Kent Beck and Erich Gamma....

    )
  • Free Pascal
    Free Pascal
    Free Pascal Compiler is a free Pascal and Object Pascal compiler.In addition to its own Object Pascal dialect, Free Pascal supports, to varying degrees, the dialects of several other compilers, including those of Turbo Pascal, Delphi, and some historical Macintosh compilers...

     (FPCUnit)
  • JavaScript
    JavaScript
    JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....

     (JSUnit
    JSUnit
    (NB: there are two different testing frameworks going by the name of JsUnit, both essentially JUnit ports. This article is currently devoted to the more widely used version created by Edward Hieatt, which solely operates in browser; the other, which in primary contrast has solely headless...

    )
  • Objective-C
    Objective-C
    Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it...

     (OCUnit)
  • Perl
    Perl
    Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

     (Test::Class and Test::Unit)
  • PHP
    PHP
    PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...

     (PHPUnit
    PHPUnit
    PHPUnit is a unit testing software framework for the programming language PHP . Created by Sebastian Bergmann, PHPUnit is one of the xUnit family of frameworks that originated with Kent Beck's SUnit. PHPUnit is hosted at GitHub .- Purpose :...

    )
  • Python
    Python (programming language)
    Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

     (PyUnit
    PyUnit
    PyUnit is the standard unit testing framework module for Python, described as a Python version of JUnit. It is now part of Python's standard library . The author is Steve Purcell.-External links:...

    )
  • R
    R (programming language)
    R is a programming language and software environment for statistical computing and graphics. The R language is widely used among statisticians for developing statistical software, and R is widely used for statistical software development and data analysis....

     (RUnit)
  • Haskell
    Haskell (programming language)
    Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...

     (HUnit)
  • Qt (QTestLib)

See also

  • Mock object
    Mock object
    In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways. A programmer typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to simulate the...

  • TestNG
    TestNG
    - Features :TestNG features include:* Flexible test configuration.* Support for data-driven testing .* Support for multiple instances of the same test class * Support for parameters....

    , an alternative to JUnit
  • List of unit testing frameworks
  • Test fixture
    Test fixture
    A test fixture is something used to consistently test some item, device, or piece of software.-Electronics:Circuit boards, electronic components, and chips are held in place and subjected to controlled electronic test signals. One example is a bed of nails tester.-Software:Test fixture refers to...

  • Mockito
    Mockito
    Mockito is an open source testing framework for Java released under the MIT License. The framework allows the creation of Test Double objects called, "Mock Objects" in automated unit tests for the purpose of Test-driven Development or Behavior Driven Development .-Distinguishing Features:Mockito...

     and PowerMock
    PowerMock
    PowerMock is a Java based framework that allows you to unit test code normally regarded as untestable. It is an extension to other mocking frameworks like Mockito or EasyMock. These frameworks are not able to mock certain language features like static methods or constructors. PowerMock enables...

    , mocking extensions to JUnit

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK