Tuesday, March 3, 2009

Java JUnit

JUnit is an open source framework that has been designed for the purpose of writing and running tests in the Java programming language. JUnit was originally written by Erich Gamma and Kent Beck. There are many ways to write test cases. A test case is a code fragment that checks that another code unit (method) works as expected. So if we want an accurate and efficient testing process then using a good testing framework is recommended. JUnit has established a good reputation in this scenario.

JUnit is a regression-testing framework that developers can use to write unit tests as they develop systems. Unit testing belongs to test a single unit of code, which can be a single class for Java. This framework creates a relationship between development and testing. You start coding according to the specification and need and use the JUnit test runners to verify how much it deviates from the intended goal. Typically, in a unit testing, we start testing after completing a module but JUnit helps us to code and test both during the development. So it sets more focus on testing the fundamental building blocks of a system i.e. one block at a time rather than module level functional testing. This really helps to develop test suites that can be run any time when you make any changes in your code. This all process will make you sure that the modifications in the code will not break your system without your knowledge.

JUnit provides also a graphical user interface (GUI) which makes it possible to write and test source code quickly and easily. JUnit shows test progress in a bar that is green if testing is going fine and it turns red when a test fails. There is a lot of pleasure in seeing the green bars grow in the GUI output. A list of unsuccessful tests appears at the bottom of the display window. We can run multiple tests concurrently. The simplicity of JUnit makes it possible for the software developer
to easily correct bugs as they are found.

JUnit Installation

1. First, download the latest version of JUnit from http://www.junit.org/ in zip format.
2. Then install JUnit on your platform of choice:
Windows
To install JUnit on Windows, follow these steps:
1. Unzip junit distribution file to a directory referred to as %JUNIT_HOME%.
2. Add JUnit to the classpath:
set CLASSPATH=%CLASSPATH%;%JUNIT_HOME%\junit-4.x.x.jar

Unix (bash)
To install JUnit on Unix, follow these steps:
1. Unzip junit distribution file to a directory referred to as $JUNIT_HOME.
2. Add JUnit to the classpath:
export CLASSPATH=$CLASSPATH:$JUNIT_HOME/junit-4.x.x.jar
3. (Optional) Unzip the $JUNIT_HOME/src.jar file.
4. Test the installation by running the sample tests distributed with JUnit. Note that the sample tests are located in the installation directory directly, not the junit.jar file. Therefore, make sure that the JUnit installation directory is on your CLASSPATH. Then simply type:
java org.junit.runner.JUnitCore org.junit.tests.AllTests
All the tests should pass with an "OK" message.
If the tests don't pass, verify that junit.jar is in the CLASSPATH.
5. Finally, read the documentation.

Writing Tests

1. Create a class:

import org.junit.*;
import static org.junit.Assert.*;

import java.util.*;

public class SimpleTest {

2. Write a test method (annotated with @Test) that asserts expected results on the object under test:

@Test
public void testEmptyCollection() {
Collection collection = new ArrayList();
assertTrue(collection.isEmpty());
}

3. If you are running your JUnit 4 tests with a JUnit 3.x runner, write a suite() method that uses the JUnit4TestAdapter class to create a suite containing all of your test methods:

public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(SimpleTest.class);
}
4. Although writing a main() method to run the test is much less important with the advent of IDE runners, it's still possible:

public static void main(String args[]) {
org.junit.runner.JUnitCore.main("junitfaq.SimpleTest");
}
}

5. Run the test:
* To run the test from the console, type:
java org.junit.runner.JUnitCore SimpleTest

* To run the test with the test runner used in main(), type:
java SimpleTest

The passing test results in the following textual output:

Time: 0
OK (1 tests)

Run JUnit using Ant

1. Define any necessary Ant properties like source folder path, library folder path or class name etc.






2. Set up the CLASSPATH to be used by JUnit:









3. Define the Ant task for running JUnit:









4. Run the test:
ant test

1 comment:

  1. Nice blog on Selenium Testing!!! I agree with your points, Testing will help whole organization to handle the projects in efficient manner.
    Selenium Course in Chennai | Best Selenium Training Institute in Chennai

    ReplyDelete