Sunday, January 2, 2011

Simple Example to Execute JUnit tests

Simple Example to Execute JUnit tests

This section will begin with a simple example which will illustrate the basic concepts involved in testing with JUnit.

Example: SimpleTest
import junit.framework.*; //1

public class SimpleTest extends TestCase{ //2
        

public void testSimpleTest() { //3
         int result = 2;
         assertEquals((1+1), result); //4
}
}

Explanations:

1 - This requires to execute our junit test case.
2 -  We are extending JUnit TestCase class to run our test cases.
3 - This is one of the tests included within this "TestCase" class. Here we have added only one test method, we can add as many methods as we need.
4 - This simple test tests whether 1+1 is equal to the value of "result". This is just an extremely simple example.

To execute this programe:

1. Create a folder called JUnitTests under any of the drive.
2. Copy the above example into your favorite test editor and save it as SimpleTest.java
3. Comple the SimpleTest.java file

Figure: Comiling SimpleTest.java
 4. Now we want to try this test out; there are three possible user interfaces for the JUnit TestRunner:

  • TestUI: Provides text-based output to stdout
  • AwtUI: Provides GUI-based output using AWT (Abstract Window Toolkit) from Java
  • SwingUI: Provides GUI-based output using the Swing graphical user interface component kit from Java.
To select an interface execute the following command sequence.

For example if we wanted text based output for our TestCase we would issue command sequence as follows:

java junit.textui.TestRunner SimpleTest

Compile the file and then try this, you should see some output similar to this:

Figure: JUnit Test Case - Pass

The above figure indicates that our test case has been passed, no failures have occurred.

Now, change the value of "result" in the example to something other than 2, recompile the file and run the test again, you should see output similar to this.

Figure: JUnit Test Case -Fail
If this was a real program we would have fix the error and recompile and re-test.

You should now begin to have an idea how to go about setting up a simple test in junit.

Installing JUnit

Installing JUnit


1. JUnit is available at http://www.junit.org/. Go ahead and click the "Download JUnit" link shown in the following figure.

Figure: Downlond Link for JUnit

2. For the purpose of this tutorial, download junit-4.8.1.zip file into C drive. Unzip the junit-<version-number>.zip file into the root of your C drive (C:\). This creates a folder called C:\\junit<version-number>. To keep the tutorial simple, go ahead and rename the C:\Junitx.x folder to C:\Junit.

Figure: JUnit Folder
3. Now you have extracted JUnit, you need to add Junit.jar to your classpath. This enables the Java compiler to resolve where and what JUnit is.

In Windows,
  • Right click on "My Computer", select "Properties"
  • In "System Properties" form, click on "Advanced" tab
  • Click on "Environment Variable"
  • Double-click CLASSPATH and then enter the new value Ex: C:\JUnit\junit.4.81.jar
  • If you already have a setting in your CLASSPATH, make sure that you append the junit path by separating the new path with a semicolon.
Figure: Setting your CLASSPATH in Windows
 That's it. Now you are ready to execute your junit test cases.

<<Previous Next>>

Intoduction to JUnit

Introduction:

JUnit is a simple open source Java testing framework used to write and run repeatable automated tests. It is an instance of the xUnit architechture for unit testing framework. Eclipse supports creating test cases and running test suites, so it easy to use for your Java applications.

JUnit features includes:

  • Assertions for testing expected results 
  • Test fixtures for sharing common test data
  • Test suites for easily organizing and running tests
  • Graphical and textual test runners
JUnit is program used to perform unit testing of virtually any sofware. JUnit testing is accomplished by writing test cases using Java, compiling these test cases and running the resultant classes with a JUnit Test runner.

Next>>