Home Tutorials Javatesting HttpUnit Test

Related Tutorials


 
 

Share on Google+Share on Google+

HttpUnit Test

Advertisement
This example illustrates about how to write a Black Box test using HttpUnit.

HttpUnit Test

     

This example illustrates about how to write a Black Box test using HttpUnit. The Block Box test is useful in evaluating the overall health of the application and its ability to provide value to users where an application is tested using its outward-facing interface (usually a graphical user interface). In this example we are showing whether url is present or not. In this example assertNotNull("url: ", url, response) check whether the given object (url) is present or not, assertNotNull("logo", logo) check whether the given object (logo) is present or not and assertNotNull("links", links) checks whether link is available or not. The main method run the test suit. 

Assertion Statement Reference: There is the list of the different types of assertion statements that are used to test your code. These assertions are taken from the Junit API. 
  • assertEquals(expected, actual)
  • assertEquals(message, expected, actual)
  • assertEquals(expected, actual, delta) - used on doubles or floats, where delta is the difference in precision
  • assertEquals(message, expected, actual, delta) - used on doubles or floats, where delta is the difference in precision
  • assertFalse(condition)
  • assertFalse(message, condition)
  • assertNotNull(object)
  • assertNotNull(message, object)
  • assertNotSame(expected, actual)
  • assertNotSame(message, expected, actual)
  • assertNull(object)
  • assertNull(message, object)
  • assertSame(expected, actual)
  • assertSame(message, expected, actual)
  • assertTrue(condition)
  • assertTrue(message, condition)
  • fail()
  • fail(message)
  • failNotEquals(message, expected, actual)
  • failNotSame(message, expected, actual)
  • failSame(message)

 

HttpUnitTestExample.java

 

package HttpUnit;

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;
import junit.framework.*;
import com.meterware.httpunit.*;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebClient;
import com.meterware.httpunit.WebConversation;

public class HttpUnitTestExample extends TestCase{
  String url = "http://localhost:8080/developers-1/";
  
  public void text(){
  try{
  URL serverUrl = new URL(url);
  WebConversation conversation = new WebConversation();
  WebRequest request = new GetMethodWebRequest(serverUrl,"");
  System.out.println("request: "+request);
  WebResponse response = conversation.getResponse(request);
  System.out.println("response: "+response);
  assertNotNull("url: " + url, response);
  System.out.println("url: "+url);

  WebImage logo = response.getImageWithSource("images/logo.gif");
  assertNotNull("logo",logo);
  System.out.println("image: ");

  WebLink links[] = response.getLinks();
  assertNotNull("links ", links);
  System.out.println("links");

  int counter = 0;
  for(int i=0; i < links.length; i++){
  String url =  links[i].getURLString();
  if(url.indexOf("/developers/developers/diary/listall/") != -1){
  counter++;
  System.out.println("servlet: " + links[i].asText());
  }
  }
  }
  catch (Exception ex){
  ex.printStackTrace();
  super.fail(ex.getMessage());
  }
  }

  public HttpUnitTestExample(String s){
  super(s);
  }
  public static Test suite() {
  TestSuite suite = new TestSuite();
  TestCase test = new HttpUnitTestExample("text");
  suite.addTest(test);
  return suite;
  }
  public static void main(String args[]){
  junit.textui.TestRunner.run( suite() );
  }
}

build.xml:
 

<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="HttpUnitTest" default="run" basedir=".">
  <property file="build.properties"/>
  <property name="src.dir" value="source"/>
  <property name="class.dir" value="./classes"/>

  <path id="proj.class.path">
  <fileset dir="lib">
  <include name="**/*.zip"/>
  </fileset>
  <fileset dir="lib">
  <include name="**/*.jar"/>
  </fileset>
 <pathelement location="${class.dir}"/>
  </path>

 <target name="clean">
  <delete dir="${class.dir}" failonerror="true" />
  <mkdir dir="${class.dir}"/>
  </target>

<target name="compile" depends="clean">
  <javac destdir="${class.dir}" debug="on" deprecation="false">
  <src path="${src.dir}"/>
  <classpath refid="proj.class.path"/>
 </javac>
</target>

<target name="run" depends="compile">
  <java classname="HttpUnit.HttpUnitTestExample" classpathref=
  "proj.class.path" 
fork="no" />

</target>
</project>


output:

Download Source Code

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 27, 2008

Related Tutorials

Discuss: HttpUnit Test  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com