Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JUnit Test

 
Kalyani Nayak
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to JUnit testing.
I am using Eclipse3.1 IDE.
I have created a test java application.
I have included junit.jar in my class libraries of java project.
I followed all the steps to create a JUnit test program as-

right clicking my java application >new->other->JUnit->JUnit Test Case

I am trying to run a junit test case, but it is giving error.
It is popping up a error message as -

"Fatal Exception occured.Program will exit."

And on console of eclipse it is showing an error as-

java.lang.NoSuchMethodError: junit.framework.TestCase.getName()Ljava/lang/String;
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.getTestName(RemoteTestRunner.java:710)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.sendTree(RemoteTestRunner.java:655)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.sendTree(RemoteTestRunner.java:643)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:471)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Exception in thread "main"


Please can anybody give solution to it?
My Junit test program is like-

package myPackage;

import junit.framework.*;
import junit.textui.TestRunner;


public class MyTest1 extends TestCase {
public static void main(String[] args) {
junit.textui.TestRunner.run(MyTest1.class);
}
public MyTest1 (String name) {
super (name);
}
public void testFirst() {
System.out.println("Hello in Junit test");
}
public void testSecond(){
try{
System.out.println("In running test method:");
}catch(Exception e){

}
}
}
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you are using an old version of JUnit. Get the newest one and try again. (You won't need the constructor any more.)
 
Rich Reese
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having the same problem with Eclipse 3.1.1., and I am using Ant 3.8.1. Any other ideas?
 
Rich Reese
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I remove the contstructor I get the following error:

java.lang.AbstractMethodError: org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.addFailure(Ljunit/framework/Test;Ljava/lang/Throwable V
at junit.framework.TestResult.addFailure(TestResult.java:112)
at junit.framework.TestResult.runProtected(TestResult.java:251)
at junit.framework.TestResult.run(TestResult.java:221)
at junit.framework.BasicTestCase.run(BasicTestCase.java:234)
at junit.framework.TestSuite.run(TestSuite.java:304)
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rich Reese:
I am having the same problem with Eclipse 3.1.1., and I am using Ant 3.8.1. Any other ideas?


What do you mean by "other ideas"? Did you check the version of *JUnit* you are using? The AbstractMethodError points into the same direction: somewhere there is an incompatible version of the junit.jar on the classpath.
 
Rich Reese
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I meant I am using JUnit 3.8.1 and not Ant 3.8.1. I will check my classpath to see if I have another version on the class path.
 
Rich Reese
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked my classpath and I don't see anything of concern. Doesn't ecplise use th Junit version specified in the Eclipses Target Platform -> Plugins under the Window -> Preferences menu option?
 
Kamal Ahmed
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right click on prject, and include junit as external jar
 
Rich Reese
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That didn't work either.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post the contents of the ".classpath" file from your project's root directory?
 
Rich Reese
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go...

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="conf"/>
<classpathentry kind="src" path="doc"/>
<classpathentry kind="src" path="dtd"/>
<classpathentry kind="src" path="lib"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="webapp"/>
<classpathentry kind="lib" path="lib/1.4.1_ciber-arch.jar"/>
<classpathentry kind="lib" path="lib/castor-0.9.5-xml.jar"/>
<classpathentry kind="lib" path="lib/ciber-arch.jar"/>
<classpathentry kind="lib" path="lib/cos.jar"/>
<classpathentry kind="lib" path="lib/ftp.jar"/>
<classpathentry kind="lib" path="lib/jakarta-poi-1.5.1-final-20020615.jar"/>
<classpathentry kind="lib" path="lib/jxl.jar"/>
<classpathentry kind="lib" path="lib/Opta2000.jar"/>
<classpathentry kind="lib" path="lib/PopChartEmbedder.jar"/>
<classpathentry kind="lib" path="lib/sal-ipworks.jar"/>
<classpathentry kind="lib" path="lib/SetupServer.jar"/>
<classpathentry kind="lib" path="lib/third-party.jar"/>
<classpathentry kind="lib" path="webapp/market/plan/webhelp/webhelp.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="E:/jboss/server/default/lib/jboss-j2ee.jar"/>
<classpathentry kind="lib" path="E:/jboss/tomcat-4.1.x/common/lib/servlet.jar"/>
<classpathentry kind="lib" path="E:/jboss/server/default/lib/mail.jar"/>
<classpathentry kind="lib" path="E:/jboss/server/default/lib/activation.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
 
Kamal Ahmed
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My .classpath looks like:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="lib" path="C:/logging-log4j-1.2.13/dist/lib/log4j-1.2.13.jar"/>
<classpathentry kind="lib" path="C:/junit3.8.1/junit.jar"/>
<classpathentry kind="output" path="classes"/>
</classpath>

as compared to yours
 
Michael Duffy
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please can anybody give solution to it?
My Junit test program is like-




You don't need the main or the ctor, just the test methods. All that other stuff is old school, from previous versions.

Which version of JUnit are you running? Should be 3.8.1
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rich Reese:
<classpathentry kind="lib" path="lib/third-party.jar"/>


This one looks suspicious. What does it contain?
[ January 25, 2006: Message edited by: Ilja Preuss ]
 
Rich Reese
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added the Junit.jar to the class path but it still doesn't work. I have the other jars in there because my app needs them to compile.

<classpathentry kind="src" path="conf"/>
<classpathentry kind="src" path="doc"/>
<classpathentry kind="src" path="dtd"/>
<classpathentry kind="src" path="lib"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="webapp"/>
<classpathentry kind="lib" path="lib/1.4.1_ciber-arch.jar"/>
<classpathentry kind="lib" path="lib/castor-0.9.5-xml.jar"/>
<classpathentry kind="lib" path="lib/ciber-arch.jar"/>
<classpathentry kind="lib" path="lib/cos.jar"/>
<classpathentry kind="lib" path="lib/ftp.jar"/>
<classpathentry kind="lib" path="lib/jakarta-poi-1.5.1-final-20020615.jar"/>
<classpathentry kind="lib" path="lib/jxl.jar"/>
<classpathentry kind="lib" path="lib/Opta2000.jar"/>
<classpathentry kind="lib" path="lib/PopChartEmbedder.jar"/>
<classpathentry kind="lib" path="lib/sal-ipworks.jar"/>
<classpathentry kind="lib" path="lib/SetupServer.jar"/>
<classpathentry kind="lib" path="lib/third-party.jar"/>
<classpathentry kind="lib" path="webapp/market/plan/webhelp/webhelp.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="E:/jboss/server/default/lib/jboss-j2ee.jar"/>
<classpathentry kind="lib" path="E:/jboss/tomcat-4.1.x/common/lib/servlet.jar"/>
<classpathentry kind="lib" path="E:/jboss/server/default/lib/mail.jar"/>
<classpathentry kind="lib" path="E:/jboss/server/default/lib/activation.jar"/>
<classpathentry kind="lib" path="E:/dev_tools/junit3.8.1/junit.jar"/>
<classpathentry kind="output" path="bin"/>
 
Rich Reese
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remove the constructor, I get the following error message from eclipse.

Implicit super constructor TestCase() is undefined for default constructor. Must define an explicit constructor DateRangeFiltrationRuleTest.java
 
Kamal Ahmed
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to write asserts, like assertEquals(); and assertTrue(); etc
look at junit API for more information, also look at some junit examples, or buy the book "Unit Testing in Java" By Johannes Link

Hope that helps
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rich, I'd probably go through each of those .jar files in your .classpath file and grep them (or whatever you'd use for that on Windows if that's what you're running) for "TestCase.class". I suspect that one of those .jar files contains an old version of JUnit.

Alternatively, you might want to move up junit.jar on your project's classpath so that its JUnit classes are "preferred" over anything that's found within the other .jar files.
 
Rich Reese
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallelujah!!! It finally works. So the steps I took was the following:

1.) Added the junit.jar to my project classpath
2.) Moved the junit.jar to the top of my classpath before any other jar
files.

Thanks for all of your help. I was about to go crazy. One thing that confuses me though is that I didn't think the jre could evaluate the contents of a jar file if it is embedded within another jar file.

Thanks again,
Rich
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rich Reese:

Thanks for all of your help. I was about to go crazy. One thing that confuses me though is that I didn't think the jre could evaluate the contents of a jar file if it is embedded within another jar file.


That's true. The only possible explanation is that one of those other jar files actually contains the junit class files directly. I still suspect the third-party.jar.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic