ITWissen.info - Tech know how online

JUnit

JUnit is a framework for automated testing ofJava classes, which belongs to the group of so-called regression testing frameworks. The framework, which is available as open source, was conceived by Kent Beck and Erich Gamma, is subject to constant further development and is available in December 2009 in the version 4.8.1. JUnit is used for unit, component and white box test, and is thus an integral component of the quality assurance ofsoftware. For this JUnit offers apart from a digital decision - " green" for the successful and " red" for a failed test - a set of static methods, which can be integrated into the tests.

JUnit as a test framework

Especially in the environment of agile software development (e.g. XP, Extreme Programming) and in the context of test-driven software development, Test Driven Development( TDD), the use of test frameworks such as JUnit is a fundamental requirement for the subsequent, actual coding.

JUnit realizes the basic idea of unit tests, which is based on the atomic test of individual parts of an application (units, classes, methods, functions) independent of the integration into a specific environment. JUnit is a regression testing framework that is executed after every change, thus ensuring that modifications to the program code do not result in unwanted side effects.

To test a class, a so-called test case - written completely in Java - must be created for the corresponding class. Several test classes can be combined to test suites. Since version 4 of JUnit, the annotations defined with Java 1.5 - a means of expression of metaprogramming - are used when creating test cases. This allows the program code to be annotated with freely selectable annotations, and thus any method of any class can be marked as an executable test case with the @Test annotation. In addition, other useful annotations like e.g

  • @Before and @After mark tasks that are repeated for each test case,
  • @BeforeClass and @AfterClass mark tasks to be executed only once per test class and
  • @Ignore marks temporary test cases not to be executed,
are provided. JUnit provides a number of methods that can be called in the tests to make so-called assertions about the behavior of a program. The following methods are available here:

assertEquals(), assertNotEquals() Checks if two objects or elementary types have the same value.

assertSame(), assertNotSame() Checks if two objects are the same object.

assertTrue(), assertFalse() Tests a boolean expression.

assertNull(), asserNotNull() Tests for a null object.

The textual TestRunner tool JUnitCore then executes the test and documents its results. In doing so, the result of a test can only be "successful" or "unsuccessful". Since version 4 of JUnit, the graphical integration of JUnit as well as the organization of test cases is consistently the task of the manufacturers of the development environment.

So that the dependencies of objects can be tested altogether, the test environment must be able to simulate these. Here one uses so-called Mock objects, which make interfaces available for the real dependencies and which return the results fitting to the test case. Mock frameworks such as EasyMock add this aspect to JUnit, and provide additional services such as checking the number of method calls.

Due to the parallel activities of Erich Gamma in connection with the development environment Eclipse, JUnit is already comfortably integrated into Eclipse, and should thus contribute to increasing the quality of software modules. But also other common development environments offer the integration of JUnit, see the mentioned link.

In connection with JUnit, it is also worth mentioning its implementation for Java EE - Cactus - as well as extensions such as HTTPUnit (a test framework for web development) and DBUnit (a test framework for database applications).

An alternative to JUnit is the testing framework TestNG, which provides functionality beyond the scope of JUnit -including integration testing.

Informations:
Englisch: JUnit
Updated at: 29.10.2013
#Words: 628
Links: framework, constant, unit (U), box, test
Translations: DE
Sharing:    

All rights reserved DATACOM Buchverlag GmbH © 2024