Wednesday, January 5, 2011

Complete JUnit

Installing Selenium IDE

Installing Selenium IDE:

1. Open Firefox browser.

2. Goto http://seleniumhq.org/download/ site.

3. Click on "Download" link for Selenium IDE

Figure: Download Link for Selenium IDE
4. Firefox browser opens with a "Software Installation" page as shown in the following figure. Click on "Install" button

Figure: Selenium IDE Installation Page


5. Restart the Firefox browser.

6. After Firefox reboots you will find the Selenium-IDE listed under the Firefox Tools menu.

Opening the IDE

To open the Selenium IDE, click on "Tools" menu of the Firefox browser, click on "Selenium IDE"

It opens as follows with an empty script-editing window and a menu for loading or creating new test cases.

Figure: Selenium IDE


Tuesday, January 4, 2011

JUnit Annotations

JUnit Annotations:

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.

Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate.

Annotations have a number of uses, among them:
  • Information for the compiler - Annotations can be used by the compiler to detect errors or suppress warnings.
  • Compiler-time and deployment-time processing - Software tools can process annotation information to generate code, XML files and so forth.
  • Runtime processing - Some annotations are available to be examined at runtime.
Followings are the some of JUnit annotations which are quite useful:

1. Test
2. Ignore
3. Before
4. After
5. BeforeClass
6. AfterClass
7. RunWith

<<Previous  Next>>

Monday, January 3, 2011

JUnit Test Case with Eclipse IDE

Using JUnit With Eclipse IDE:

1. Run Eclipse IDE.

2. Create a new workspace project, click File -> New -> Project, then choose Java and click "Next" button. Type in a project name -- for example, ProjectWithJUnit

3. Click Finish. The new project will be generated in your Eclipse IDE.

4. Let's configure our Eclipse IDE, so it will add JUnit library to the build path. Click on Project->Properties, select Java Build Path, Libraries, clcik Add External JARs and browse to directory where your JUnit is stored. Pick junit.jar and click Open.

You will see that JUnit will appear on your screen in the list of libraries. By clicking Okay you will force Eclipse to rebuild all build paths.

We are ready to start developing our first Junit test "Hello World" in Eclipse IDE. We will write a Junit test cases for "HelloWorld" class.

5. Create a package called "com.blogshot.atozselenium". Right click on the src (source folder), select New -> Package. Name it as "com.blogshot.atozselenium"
6. To create a "HelloWorld" class, right click on the project "ProjectWithJunit" title, slect New -> Class. Enter the class name as HelloWorld. Click on "Finish" button.

7. Create a method called say() and save the java file.

public class HelloWorld{
    public String say(){
        return("Hello World");      
    }
}


Your Eclipse IDE HelloWorld class should look like this:

                      Figure: "HelloWorld" class in Eclipse IDE.

8.Create a JUnit Test Case.

a. Right click on the project
b. Select New -> JUnit Test Case.

Figure: Creating JUnit Test Case
c. In JUnit Test Case form, select Package, by clicking on the "Browse" button.
d. Enter name as TestHelloWorld
e. Click on "Finish" button

Figure: JUnit Test Case

9. Modify the JUnit Test Case as follows

package com.blogshot.atozselenium;

import junit.framework.TestCase;

public class TestHelloWorld extends TestCase{
    public void testSay(){
            HelloWorld hi = new HelloWorld();
            assertEquals("Hello World!", hi.say());
    }
}

10. Now run this test case. 

Click on "Run" menu
Select "Run As" -> JUnit Test

Figure: Running JUnit Test Case

In the left side, you will see JUnit window showing a green bar. The green bar means that out test was successful.

Figure: Successful Test in JUnit

Now we want to try to make it fail. This will help show how JUnit test covers and reports different errors. Edit the assertEquals() to change the expected returm value from "Hello World!" to "Hello You!". 
When you run this JUnit test again, the bar will be red, and at the bottom of the left JUnit window you will see an explanation of what failed as illustrated in the following figure.

Figure: Failed testr in JUnit


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>>