Reputation: 2376
I have a currently private Java project which I'm trying to get into shape for use by other developers. One of the things I'm trying to do is to have Ant itself download the JARs needed for things like JUnit, PMD and FindBugs, so developers don't have to install it themselves. So I've deinstalled the RPM packages for all those things (to make sure I'm using the Ivy downloaded versions) and I've got Ivy working just fine to download the JUnit related JAR files. However, I'm having two (possibly related) problems:
1) If I put in a taskdef
for the junit
task, I get the warning Trying to override old definition of datatype junit
. However, if I don't do that, I get the error:
failed to create task or type junit
Cause: the class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask was not found.
2) When I do override the junit
taskdef, I get this error when trying to run JUnit tests:
junit.framework.AssertionFailedError: No tests found
Doing a search for that error says that it happens for tests that were written for JUnit 3.x when being run under 4.x. However, my JUnit tests all use the 4.x @Test
way of doing tests, the tests ran just fine when I was using the RPM package provided JUnit 4.11 JAR files, and the JUnit JAR files downloaded by Ivy are version 4.11 for JUnit and 1.9.4 for ant-junit. There is a mismatch between the RPM package installed Ant I'm using (1.9.2) and the Ant JAR files downloaded by Ivy (1.9.4), but I don't think that's the problem.
My junit
taskdef is:
<taskdef name="junit"
classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
<classpath location="lib/ant-junit.jar"/>
<classpath location="lib/junit.jar"/>
<classpath location="lib/hamcrest-core.jar"/>
</taskdef>
Running Ant with debugging turned on produces this output relevvant to the problem:
Finding class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask
Loaded from /BlahBlah/lib/ant-junit.jar org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.class
Class org.apache.tools.ant.Task loaded from parent loader (parentFirst)
Class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask loaded from ant loader (parentFirst)
[...]
Class org.apache.tools.ant.util.FileUtils loaded from parent loader (parentFirst)
Could not load class (org.apache.tools.ant.taskdefs.optional.junit.JUnitTask) for type junit
Could not load class (org.apache.tools.ant.taskdefs.optional.junit.JUnitTask) for type junit
Trying to override old definition of datatype junit
+Datatype junit org.apache.tools.ant.taskdefs.optional.junit.JUnitTask
So, is there some way to fix this? Or maybe I should take an entirely different approach?
Upvotes: 1
Views: 2007
Reputation: 77991
I suggest using ivy configurations to manage your test classpath. The following example contains a working example:
Your ivy file contains a dependency with a configuration mapping to "test":
<configurations>
<conf name="compile" description="Required to compile application"/>
<conf name="runtime" description="Additional run-time dependencies" extends="compile"/>
<conf name="test" description="Required for test only" extends="runtime"/>
</configurations>
<dependencies>
..
..
<!-- test dependencies -->
<dependency org="junit" name="junit" rev="4.11" conf="test->default"/>
</dependencies>
The build file creates a classpath populated using the ivy configuration:
<target name="resolve" depends="...." description="Use ivy to resolve classpaths">
..
..
<ivy:cachepath pathid="test.path" conf="test"/>
</target>
And finally the junit task uses this classpath
<target name="test" depends="compile-tests" description="Run unit tests">
..
..
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<path refid="test.path"/>
<pathelement path="${classes.dir}"/>
<pathelement path="${test.classes.dir}"/>
</classpath>
..
..
</junit>
</target>
Upvotes: 1