Meaningless Drivel is fun!*
The moose likes Testing and the fly likes JUnit Test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "JUnit Test" Watch "JUnit Test" New topic
Author

JUnit Test

Kalyani Nayak
Greenhorn

Joined: Jan 01, 2006
Posts: 25
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

Joined: Jul 11, 2001
Posts: 14112
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.)


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rich Reese
Greenhorn

Joined: Jan 20, 2006
Posts: 9
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

Joined: Jan 20, 2006
Posts: 9
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Jan 20, 2006
Posts: 9
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

Joined: Jan 20, 2006
Posts: 9
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

Joined: Feb 15, 2005
Posts: 91
right click on prject, and include junit as external jar
Rich Reese
Greenhorn

Joined: Jan 20, 2006
Posts: 9
That didn't work either.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Could you post the contents of the ".classpath" file from your project's root directory?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Rich Reese
Greenhorn

Joined: Jan 20, 2006
Posts: 9
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

Joined: Feb 15, 2005
Posts: 91
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

Joined: Oct 15, 2005
Posts: 163
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Jan 20, 2006
Posts: 9
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

Joined: Jan 20, 2006
Posts: 9
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

Joined: Feb 15, 2005
Posts: 91
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Jan 20, 2006
Posts: 9
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

Joined: Jul 11, 2001
Posts: 14112
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JUnit Test
 
Similar Threads
Simple program for URL
Junit test: cannot find test Method although that it is already defined in the test class
Class not found test.TestIndexBean error in Eclipse
URL and Severs(very Urgent)
NULL Pointer Exception