Unable to Find Test Class in Classpath

503 views
Skip to first unread message

neilac333

unread,
Oct 23, 2007, 4:54:55 PM10/23/07
to testng-users
I am fairly new to TestNG, and I seem to be getting the hang of it.
However, I am running TestNG in Ant and finding the following:

[ERROR]:
===============================================
Cannot find class in classpath: persistence.test.UpdatableEntityTest
PersistenceTests
Total tests run: 0, Failures: 0, Skips: 0
===============================================


However, I know that class is in the classpath! Here is how I know
(from Ant in verbose mode):

testng
Executing 'C:\Program Files\Java\jdk1.5.0_12\jre\bin\java.exe' with
arguments:
'-ea'
'-classpath'
'[Bunch of standard jars];
C:\persistence\classes\persistence\UpdatableEntity.class;
C:\persistence\classes\persistence\admin\User.class;
C:\persistence\classes\persistence\test\UpdatableEntityTest.class'
'org.testng.TestNG'

Here is the Ant task:

<testng classpathref="task.class.path.libs"
outputDir="${testng.report.dir}"
haltOnfailure="true">
<xmlfileset dir="${testng.suite.dir}"
includes="persistenceTests.xml" />
</testng>

What am I missing here?

Thanks for any insight.

Cédric Beust ♔

unread,
Oct 23, 2007, 5:39:30 PM10/23/07
to testng...@googlegroups.com
Your classpath contains .class files, it should only contain jars or directories.

--
Cédric

neilac333

unread,
Oct 23, 2007, 7:54:56 PM10/23/07
to testng-users
Hmmm...I don't think I had any references to class files. Still, I
did make some changes. The question now I suppose is how does TestNG
figure out what I am trying to test. I have tried numerous
permutations of things like adding and removing any reference to my
build directory, adding workingdir, adding sourcedir, seeing what
happens with classfileset, and so on. I continue to get the same
error.

Here is my classpath:

<path id="testng.class.path.lib">
<fileset dir="${testng.lib.dir}"/>
</path>
<path id="dbunit.class.path.lib">
<fileset dir="${dbunit.lib.dir}"/>
</path>
<path id="task.class.path.libs">
<path refid="testng.class.path.lib"/>
<path refid="dbunit.class.path.lib"/>
<fileset dir="${seam-hibernate.lib.dir}" includes="jboss-
seam.jar"/>
<fileset dir="${oracle.lib.dir}" />
<fileset dir="${build.dir}" /> <!-- This is the top level
directory below which my compiled classes reside. I get the same
error whether I have this entry here or not. -->
</path>

----------------------------------------
Just for kicks, here is my XML file:

<suite name="PersistenceTests">
<test name="UpdatableEntityTest">
<groups>
<run>
<include name="updatableEntityTest"/>
</run>
</groups>
<classes>
<class name="persistence.test.UpdatableEntityTest" />
</classes>
</test>
</suite>

---------------------------

I know this all seems rudimentary, particulalry for you as the father
of TestNG, but I really would appreciate some guidance.

Thanks again.

Cédric Beust ♔

unread,
Oct 23, 2007, 8:23:19 PM10/23/07
to testng...@googlegroups.com
It's hard to help you with just fragments of your build.xml, especially since they all contain variables (and we have no idea where your classes are located on your hard drive).

Try this:  find the right classpath so that

java -classpath directory persistence.test.UpdatableEntityTest

will find your class.

Then put this directory verbatim in your build.xml, and everything should work right away.

--
Cedric
--
Cédric

Mark Derricutt

unread,
Oct 23, 2007, 8:41:39 PM10/23/07
to testng...@googlegroups.com
SIlly question I know - but you are actually annotation your test methods with @Test right?

neilac333

unread,
Oct 23, 2007, 9:59:29 PM10/23/07
to testng-users
Not a silly question, Mark...although that would be profoundly silly
of me to have done, and I would owe the entire forum lollipops. I am
happy to report though that I do indeed have a @Test annotation on my
single test method. Moreover, it even has a meta-annotation for its
group. Besides, that kind of issue would generate a different error
message, right?

I will try your approach, Cedric, and see what I find.

Thank you both for your help. Meanwhile, if anyone else sees
something silly I might be doing, please feel free to point it out.

On Oct 23, 8:41 pm, "Mark Derricutt" <m...@talios.com> wrote:
> SIlly question I know - but you are actually annotation your test methods
> with @Test right?
>

neilac333

unread,
Oct 24, 2007, 2:05:42 PM10/24/07
to testng-users
By the way, to answer Cedric's question, all my Ant properties in the
classpath shown above point to directories with JAR files (such as
TestNG itself or my Oracle DB driver). The one property that might be
interesting is ${build.dir}. Basically, that directory looks like
this:

build
--persistence
-----test
--------UpdatableEntityTest.class

So I was doing things like this in the classpath

<fileset dir="${build.dir}" includes="**/*.class"/>

or

<fileset dir="${build.dir}" />

or

no entry at all.

Hopefully, that at least clears up the directory content issue.

Now on to try Cedric's command line suggestion.

Thanks.

neilac333

unread,
Oct 24, 2007, 2:38:31 PM10/24/07
to testng-users
OK, so far this isn't producing anything different. I should mention
I tried putting my test class in a JAR file and putting the JAR file
in the classpath like all my other JARs, but that doesn't work
either.

I did so this way:

java -cp C:\persistence\dist\persistence.jar -jar testng-5.6-
jdk15.jar C:\persistence\test\suites\persistenceTests.xml

I also tried without a JAR:

java -cp C:\persistence\build -jar testng-5.6-jdk15.jar C:\persistence
\test\suites\persistenceTests.xml

and

java -cp C:\persistence\build\persistence -jar testng-5.6-jdk15.jar C:
\persistence\test\suites\persistenceTests.xml

and

java -cp C:\persistence\build\persistence\test -jar testng-5.6-
jdk15.jar C:\persistence\test\suites\persistenceTests.xml

----------------

Any hints?

Thanks.

neilac333

unread,
Oct 24, 2007, 5:07:56 PM10/24/07
to testng-users
I tried this on the command line:

java -cp C:\persistence\build -jar testng-5.6-jdk15.jar -testclass
persistence.test.UpdatableEntityTest


I got a slightly different error: Cannot load class from file:
persistence.test.UpdatableEntityTest

Incidentally, the directory "build" mentioned above is a directory
below which all my packages and .class files, including my test class,
are located.

Is this simply another way of saying the same error I received
before? Or am I getting any closer?


Cédric Beust ♔

unread,
Oct 24, 2007, 5:19:19 PM10/24/07
to testng...@googlegroups.com
Looks like there might be something wrong with your .class file.  Try recompiling.

--
Cedric








--
Cédric

Cédric Beust ♔

unread,
Oct 24, 2007, 5:19:51 PM10/24/07
to testng...@googlegroups.com
And try again without the -jar option.


--
Cédric

neilac333

unread,
Oct 24, 2007, 6:12:32 PM10/24/07
to testng-users
I did a clean with Ant and recompiled with Ant. I then tried the
following:

java -cp testng-5.6-jdk15.jar;C:\persistence\build org.testng.TestNG -
testclass persistence.test.UpdatableEntityTest

I still get this:

Exception in thread "main" org.testng.TestNGException:


Cannot load class from file: persistence.test.UpdatableEntityTest

at
org.testng.TestNGCommandLineArgs.fileToClass(TestNGCommandLineArgs.ja
va:603)
at
org.testng.TestNGCommandLineArgs.parseCommandLine(TestNGCommandLineAr
gs.java:225)
at org.testng.TestNG.privateMain(TestNG.java:815)
at org.testng.TestNG.main(TestNG.java:802)

Did you have something else in mind for me to try?

--------------------------------------------

Going back to Ant, I did some restructuring:

<path id="testng.class.path.lib">
<fileset dir="${testng.lib.dir}"/> contains testng.jar
</path>
<path id="dbunit.class.path.lib">
<fileset dir="${dbunit.lib.dir}"/> contains dbunit.jar
(irrelevant to this issue)
</path>
<path id="database.class.path.libs">
<path refid="dbunit.class.path.lib"/>
<fileset dir="${oracle.lib.dir}" /> contains Oracle
driver (irrelevant to this issue)
</path>
<path id="tests.class.path.libs">
<path refid="testng.class.path.lib"/>
<path refid="database.class.path.libs"/>


<fileset dir="${seam-hibernate.lib.dir}" includes="jboss-

seam.jar"/> contains jboss-seam.jar for SeamTest class
(irrelevant to this issue)


<fileset dir="${build.dir}" /
>

contains .class file hierarchy compiled from source
</path>


And here is the Ant task

<testng classpathref="tests.class.path.libs" outputDir="$
{testng.report.dir}" failureProperty="test.failed">


<xmlfileset dir="${testng.suite.dir}"
includes="persistenceTests.xml" />
</testng>


Anything jump out here?

Thanks.

Cédric Beust ♔

unread,
Oct 24, 2007, 7:57:34 PM10/24/07
to testng...@googlegroups.com
You never tried the javap command I suggested (and stop using -jar, only use -cp).

At any rate, this is a basic Java question that is not related to TestNG, so we can't really help you until you figure out the correct classpath to use in your build...

--
Cedric


On 10/24/07, neilac333 <screec...@gmail.com> wrote:



--
Cédric

neilac333

unread,
Oct 24, 2007, 9:46:49 PM10/24/07
to testng-users
Maybe it wasn't obvious from the formatting, but I followed directions
and didn't use -jar. I only used -cp.

Here is the statement again:

java -cp testng-5.6-jdk15.jar;C:\persistence\build org.testng.TestNG -
testclass persistence.test.UpdatableEntityTest

Let me see if this is clearer:

java
-cp testng-5.6-jdk15.jar;C:\persistence\build
org.testng.TestNG
-testclass persistence.test.UpdatableEntityTest

As you can see here, the only "jar" in the line was where I place
"testng-5.6-jdk15.jar" in the classpath with -cp. I can see how it
might be hard to catch that given how the command appears on here.
The other item in the classpath is the top level directory below which
my .class files reside.

Seemed reasonable to me.

Also, you said this

"Try this: find the right classpath so that

java -classpath directory persistence.test.UpdatableEntityTest

will find your class. "

But in your last message you said to use "javap." I know that is an
actual command, but I imagine that was a typo since I am not sure how
the command would be relevant here. Is that correct?

I can understand your point that this is more Java classpath stuff
than it is TestNG stuff, but I have managed to get things like DBUnit
and Hibernate Tools (which require way more configuration) going in
Ant. I am not suggesting Al Gore should give me his Nobel Prize for
those achievements, but it is really troubling that I can't get
something so basic right with TestNG. That is why I am curious if
there is something peculiar about how it handles things. Particularly
given that I think that everything I have tried in my Ant script is
really quite reasonable.

If anyone has any working examples of getting TestNG to work in Ant, I
would really appreciate some Ant target samples and maybe a brief
explanation of how you handled the test classes themselves.

Thanks.


neilac333

unread,
Oct 25, 2007, 12:19:53 AM10/25/07
to testng-users
By the way, Cedric, I am looking forward to checking out Next
Generation Java Testing: TestNG and Advanced Concepts. It looks like
a book I can really use as I move from JUnit to TestNG.

neilac333

unread,
Oct 26, 2007, 2:22:14 AM10/26/07
to testng-users
Problem solved! Turns out I had the classpath right all along. The
issue was that jboss-seam.jar had other dependencies it needed for me
to use the SeamTest class. Yet rather than give me a
NoClassDefFoundError, TestNG reported all the errors I described
earlier. Once I got the dependencies in the classpath, everything
worked as expected.

Oh well, cryptic and misleading error messages aside, I seem to be
moving along nicely.

Thanks to all.

Steve Loughran

unread,
Oct 26, 2007, 4:02:20 AM10/26/07
to testng...@googlegroups.com
On 24/10/2007, neilac333 <screec...@gmail.com> wrote:
>
> I tried this on the command line:
>
> java -cp C:\persistence\build -jar testng-5.6-jdk15.jar -testclass
> persistence.test.UpdatableEntityTest
>
>

When you use the -jar param on the command line, java.exe ignores the
classpath; the only classpath it uses is what is specified in the
Classpath: entry in the JAR's manifest. Ant's <java> task warns of
this, because we make the same mistake ourselves, but java.exe doesnt
bother, because it should be so obvious.

Options
-Add the testng jar to the CP, give the main entry point by hand.
-create a manifest in your test jar, point to testng

> I got a slightly different error: Cannot load class from file:
> persistence.test.UpdatableEntityTest
>
> Incidentally, the directory "build" mentioned above is a directory
> below which all my packages and .class files, including my test class,
> are located.
>
> Is this simply another way of saying the same error I received
> before? Or am I getting any closer?

The error has moved

Mark Derricutt

unread,
Oct 26, 2007, 2:26:23 PM10/26/07
to testng...@googlegroups.com
Ahhh - I actually have a patch for TestNG to report such dependency problems (and optionally ignore them when trying to find classes), I must tidy that up and send it over to Cedric/Alex to consider.

Mark Derricutt

unread,
Oct 26, 2007, 2:27:10 PM10/26/07
to testng...@googlegroups.com
doh - stupid gmail - quoted the wrong email :(


On 10/27/07, Mark Derricutt <ma...@talios.com> wrote:
Ahhh - I actually have a patch for TestNG to report such dependency problems (and optionally ignore them when trying to find classes), I must tidy that up and send it over to Cedric/Alex to consider.




--
It`s not the tree that forsakes the flower, but the flower that forsakes the tree
Someday I`ll learn to love these scars - Bye Bye Beautiful
Reply all
Reply to author
Forward
0 new messages