Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java using httpunit and junit

 
Daniel Hoffman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am trying to write a little application using httpunit and junit. I am trying to develop various test cases to test the continued functionality of our websites/applications. The problem I am having is in setting up httpunit and junit. I have followed all the instructions posted on both websites but I am encountering an error when I try and implement the code samples posted. The following is the error message I am receiving:
Rhino classes (js.jar) not found - Javascript disabled java.lang.RuntimeException: No HTML parser found. Make sure that either nekoHTML.jar or Tidy.jar is in the in classpath
I have placed both these files in my classpath and I am able to successfully import com.meterware.httpunit.*; but I cannot import junit.framework.TestCase.*;
I was hoping someone has had some success with these 2 packages and would be able to provide some assitance. I have posted links to both httpunit and junit below:
http://httpunit.sourceforge.net/index.html
http://www.junit.org/index.htm
If you require code to better understand what I am trying to do, I can provide what I have so far.
Thanks
Daniel Hoffman
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JUnit is in junit.jar, not in either of the other files you mention. So you're going to need to add that as well.
 
Daniel Hoffman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have added the junit.jar file to the classpath with no success. I have added all the required files according to the documentation but I still cannot import junit.framework.TestCase.*; and I still receive the no HTML parser found error. Perhaps you have some other suggestions?
Thanks
Daniel
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, I didn't look carefully enough at your original post.
TestCase is a class, not a package; to use it, you'd say
import junit.framework.TestCase;
or, since you'll likely use a few other JUnit classes as well,
import junit.framework.*;
 
Daniel Hoffman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I tried using that line ( import junit.framework.*; ) in my code but I receive the following error when I compile:
package junit.framework does not exist
I have added junit.jar to the classpath and the path with no success. The entry I am using for the classpath\path is: c:\junit3\junit.jar; I am not sure if this error ( package junit.framework does not exist ) is linked to the other issue I am having with the HTML parser (nekohtml.jar or Tidy.jar).
Any other thoughts on this matter would be appreciated.
Thanks
Daniel
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the Testing forum.
 
Christian Hargraves
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to your original post, it seems like you got the code compiled or else you wouldn't have gotten to the part that says "javascript disabled" and "nekohtml or jtidy not found".
For starters, you don't need to add ANY class files or jar files to your PATH, only to your CLASSPATH. I am guessing that the classpath you are using to compile the files is different from the classpath you are using to execute the files or else, like I said above, you wouldn't even be at the point where HttpUnit is complaining about not being to find files.
How are you executing this files and how are you compiling them?
 
Daniel Hoffman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use a java development tool called JCreator to develop, compile and execute my code. When I use the command line javac I get the following errors:
D:\Documents and Settings\Administrator\Desktop\Broker\broker>javac Broker.java
Broker.java:18: Package javax.swing not found in import.
import javax.swing.*;
^
Broker.java:19: Package com.meterware.httpunit not found in import.
import com.meterware.httpunit.*;
^
2 errors
However, when I compile the file using JCreator I get a class file and no complaints. The classpath variable I set are the Windows Envronment Variables for both the user(i.e. me) and the system. The classpath statement I am using looks like this: D:\junit3.8.1\junit.jar; this represent the exact path to this jar file. I have similar entries for httpunit: D:\httpunit-1.5.4\jars\Tidy.jar; and D:\httpunit-1.5.4\jars\nekohtml.jar;
Here is the code I have developed so far, any further assistance would be appreciated.
import java.awt.*;
import java.awt.event.*;
import java.awt.Container.*;
import java.awt.BorderLayout;
import java.io.*;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.lang.Exception.*;
import java.lang.Object.*;
import java.lang.Exception.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import com.meterware.httpunit.*;
import org.xml.sax.*;
//import junit.framework.TestCase.*;
public class Broker extends JFrame implements ActionListener
{
// Variable declaration
private Calendar cal;
private String dateFormat = "yyyy-MM-dd HH:mm:ss";
private JButton execute;
private JButton exit;
private JLabel currentTest;
private JPanel panel, buttonPanel;
private DataOutputStream logFile;
private DataOutputStream dataFile;
private static WebConversation wc = new WebConversation();

// Default constructor
public Broker()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
setupGUI();
setupPanel();
}

// Function to setup GUI components
public void setupGUI()
{
exit = new JButton( "Exit Test" );
exit.addActionListener( this );
execute = new JButton( "Execute Test" );
execute.addActionListener( this );
currentTest = new JLabel( "Please click Execute Test to begin" );
}

// Function to setup GUI Panels
public void setupPanel()
{
buttonPanel = new JPanel();
buttonPanel.setLayout( new GridLayout( 1, 2 ) );
buttonPanel.add( execute );
buttonPanel.add( exit );

panel = new JPanel();
panel.setLayout( new BorderLayout() );
panel.add( buttonPanel, BorderLayout.SOUTH );
panel.add( currentTest, BorderLayout.CENTER );

panel.setBorder( (BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Logon Test Application"),
BorderFactory.createEmptyBorder(5,5,5,5) ) ) );

Container c = new Container();
c = getContentPane();
c.add( panel, BorderLayout.CENTER );
setVisible( true );
}

// Function to write log entries
public void writeLog (String app, String entry)
{
//open file for writing request
try
{
logFile = new DataOutputStream( new FileOutputStream( "Test.log", true ) );
dataFile = new DataOutputStream(logFile);
}
catch (IOException ioException)
{
System.out.println( "Error opening the file" );
System.exit(1);
}

//write request to file
cal = Calendar.getInstance( TimeZone.getDefault() );
java.text.SimpleDateFormat simple = new java.text.SimpleDateFormat(dateFormat);
System.out.println( simple.format( cal.getTime() ) );
try
{
dataFile.writeBytes( simple.format( cal.getTime() ) + " " + app + "\r\n");
dataFile.writeBytes( entry + "\r\n\r\n" );
}
catch ( IOException e )
{
System.out.println( "Error opening the file" );
}
}

public void actionPerformed( ActionEvent e )
{
if ( e.getSource() == execute )
{
currentTest.setText( "Starting the Web Broker Test" );
try
{
WebResponse resp = wc.getResponse( "http://www.google.com" ); // read this page
WebLink link = resp.getLinkWith( "response" ); // find the link
link.click(); // follow it
WebResponse jdoc = wc.getCurrentPage();
}
catch ( SAXException s )
{
System.out.println( "There has been a SAX error" );
writeLog( "Connection", "There has been a SAX error" );
}
catch ( MalformedURLException m )
{
System.out.println( "Cannot reconcile website" );
writeLog( "Connection", "There has been a conneciton error" );
}
catch ( IOException i )
{
System.out.println( "Input/Output error" );
writeLog( "Connection", "There has been an I/O error" );
}
}
else
{
System.out.println( "Error connecting to Web Broker" );
writeLog( "Web Broker", "Error connecting to Web Broker");
}
if ( e.getSource() == exit )
{
System.out.println( "Test program closed" );
writeLog( "Test Exit","Test program closed" );
dispose();
System.exit(0);
}
}
// Main
public static void main(String args[]) throws Exception
{
Broker WB = new Broker();
WB.setSize( 400, 100 );
WB.setTitle( "Web Broker Logon Test" );
WB.setVisible( true );
WB.addWindowListener(new WindowAdapter()
{
public void windowClosing( WindowEvent event )
{ System.exit(0); }
}
);
}
}
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should add D:\httpunit-1.5.4\lib\httpunit.jar into your classpath (that's where the com.meterware packages are).
 
Daniel Hoffman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have already added that line to my classpath. When I use JCreator to compile/execute my code I get no complaints about the httpunit package. I only get that error when I compile via the command line javac. As far as I can see I have all the required classpath statements for both httpunit and junit setup. The only thing that I can think of is when I use the command line javac it is using a different classpath then when I compile via JCreator. However, I believe that I have set all the classpath variables that I can find. Any further suggestions would be appreciated.
Thanks
Daniel
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) On Windows, type "SET CLASSPATH" at the command prompt. Post the result here, if it doesn't look strange to you.
2) Try the -cp option of the java command (which overrides the classpath environment variable).
 
Daniel Hoffman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Here is the result of the SET CLASSPATH command:
CLASSPATH=C:\httpunit-1.5.5-d-19Sep\jars;C:\httpunit-1.5.5-d19Sep\jars\nekohtml.jar;C:\httpunit-1.5.5-d-19Sep\jars\Tidy.jar;C:\httpunit-1.5.5-d-19Sep\jars\js.j
ar;c:\junit3\junit.jar;c:\junit3;C:\junit3\junit\tests;C:\junit3\junit.jar;C:\junit3\junit;
I am currently developing at work so my root dir is c:\. When I develop at home the root is d:\
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CLASSPATH=C:\httpunit-1.5.5-d-19Sep\jars;C:\httpunit-1.5.5-d19Sep\jars\nekohtml.jar;C:\httpunit-1.5.5-d-19Sep\jars\Tidy.jar;C:\httpunit-1.5.5-d-19Sep\jars\js.j
ar;c:\junit3\junit.jar;c:\junit3;C:\junit3\junit\tests;C:\junit3\junit.jar;C:\junit3\junit;

Is this a typo in the post or in the classpath?
 
Daniel Hoffman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what the path is on my home system:
D:\>set classpath
CLASSPATH=D:\Program Files\Sybase\Shared\Sun\jdk117\lib\classes.zip;D:\Program F
iles\Sybase\Shared\PowerBuilder\classes.zip;D:\httpunit\jars\junit.jar;D:\junit\
junit.jar;D:\httpunit\jars\nekohtml.jar;D:\httpunit\jars\Tidy.jar;D:\httpunit\ja
rs\js.jar
I still have the same issue. I changed all my dirs to really simple paths.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...\jdk117\lib\classes.zip;...

Could this be a reason? For example, it could be that JDK 1.1.7 doesn't include Swing (I can't remember that far ), which would explain the compilation error regarding import javax.swing.*
 
Christian Hargraves
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a reason you are using jdk 1.1.7? Swing is not included in anything below jdk 1.2. The "Program Files" can also mess up your classpath and your path if that part of the CLASSPATH/PATH is not quoted.
For #2, your imports are all messed up.
import java.awt.*;//Ok
import java.awt.event.*;//Ok
import java.awt.Container.*; // NOT OKAY
//This is a class and should be "import java.awt.Container;"
import java.awt.BorderLayout; // Ok
import java.io.*;//Ok
import java.io.File;//Ok
import java.io.FileReader;//Ok
import java.io.BufferedReader;//Ok
import java.lang.Exception.*;//NOT OKAY
//No reason to import this as the java.lang package is already imported.
//If you really want to import this, then it should be
//import java.lang.Exception;
import java.lang.Object.*;//NOT OKAY -- Same as the above.
import java.lang.Exception.*;//NOT OKAY -- Second time to import same thing.
import java.net.*;//Ok
import java.util.*;//Ok
import javax.swing.*;//Ok -- but jdk 1.1.7 doesn't come with Swing.
//you'll need swing.jar in your classpath -- try using jdk >= 1.2
import com.meterware.httpunit.*;//Ok
import org.xml.sax.*;//Ok
//import junit.framework.TestCase.*;// NOT OKAY -- same as above
//TestCase is a class -- import junit.framework.TestCase;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic