I have a file in my java project called HelloWorldServiceTest.java. Its under com.dummy.test under the src folder.
Below is a snippet of my Ant build.xml file
<target name="junit" depends="compile">
<junit printsummary="yes">
<classpath>
<fileset dir="lib"/>
</classpath>
<batchtest fork="yes">
<fileset dir="${src.dir}" includes="com/dummy/test/HelloWorldServiceTest.java/>
</batchtest>
</junit>
</target>
Output is always as follows:
junit:
[junit] Running com.dummy.test.HelloWorldServiceTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed 0 sec
[junit] Test com.dummy.test.HelloWorldServiceTest FAILED
Can anyone help me on this?
Manually running HelloWorldServiceTest generates no error and always a success. Running it through ant always shows FAILED and there is even no stack trace why it was failing.
lib is where the junit jar is placed
When I added junit -debug as arguments, I saw the ff.
Finding class com.dummy.test.HelloWorldServiceTest
Class java.lang.System loaded from parent loader (parentfirst)
[junit] Test com.dummy.test.HelloWorldServiceTest FAILED
and
<error message="com.dummy.test.HelloWorldServiceTest" type="java.lang.ClassNotFoundException />
at java.lang.ClassLoader.loadClass(Unknown source)
...
...
...
Seems it cant find HelloWorldServiceTest.class
Related
I am developing a Java application in IntelliJ IDE. In order to be able to connect my repo to Travis CI, I came through generating ant build.xml using my IDE. However IntelliJ haven't created test target in the build file. I have manually edited it by adding the following:
<target name="test" depends="compile.module.pearplanner.tests">
<junit>
<classpath>
<pathelement location="lib/junit-4.12.jar"/>
</classpath>
<batchtest>
<fileset dir="./Test/">
<include name="**/*Test*" />
</fileset>
</batchtest>
<formatter type="brief" usefile="false"/>
</junit>
</target>
When I run
ant test
I am getting the following errors:
test:
[junit] Testsuite: Model.AccountTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
[junit]
[junit] Null Test: Caused an ERROR
[junit] Model.AccountTest
[junit] java.lang.ClassNotFoundException: Model.AccountTest
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:348)
[junit]
[junit]
[junit] Test Model.AccountTest FAILED
[junit] Testsuite: Model.PersonTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
[junit]
[junit] Null Test: Caused an ERROR
[junit] Model.PersonTest
[junit] java.lang.ClassNotFoundException: Model.PersonTest
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:348)
[junit]
[junit]
[junit] Test Model.PersonTest FAILED
BUILD SUCCESSFUL
Total time: 0 seconds
My project structure
I have tested multiple approaches I could find on the web, but all of them were giving me the same output. What am I doing wrong here?
I managed to fix it. Turned out that I had to specify class output directory and test class output directory in the classpath. Also hamcrest-core-1.3.jar wasn't there either. Correct version:
<target name="test" depends="build.modules" >
<junit haltonerror="yes" haltonfailure="yes">
<classpath>
<pathelement location="${basedir}/lib/junit-4.12.jar"/>
<pathelement location="${basedir}/lib/hamcrest-core-1.3.jar"/>
<pathelement location="${pearplanner.output.dir}/"/>
<pathelement location="${pearplanner.testoutput.dir}/"/>
</classpath>
<batchtest>
<fileset dir="${pearplanner.testoutput.dir}">
<include name="**/*Test*" />
</fileset>
</batchtest>
<formatter type="brief" usefile="false"/>
</junit>
</target>
I have a problem with my unit test, and I don't have a clue why. When I run my unit test normally in eclipse it runs perfectly fine. But when I try to build my project using ant, there is a unit test that fails.
The only information that I get from it is that the test failed, I don't know why or on what line.
Is there a way for me to let ant tell me what went wrong (and on which line)?
This is my unit test target:
<target name="unit.test"
depends="jar"
description="'jar', 'compile', and run unit tests">
<junit printsummary="on" failureproperty="unit.test.failed" timeout="300000">
<jvmarg value="-Dtest.resource.dir=${resource.dir}/test"/>
<jvmarg value="-Dresource.dir=${resource.dir}"/>
<jvmarg value="-Djava.awt.headless=true"/>
<classpath >
<pathelement location="${debug.class.dir}"/>
<pathelement location="${resource.test.dir}"/>
<pathelement location="${resource.dir}"/>
<pathelement location="${library.release.dir}/${junit.jar}"/>
<filelist refid="test.imports"/>
<filelist refid="imports"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="yes" todir="${release.test.dir}">
<fileset dir="${source.dir}">
<include name="test/unit/**/*Test.java" unless="test.unit"/>
<include name="test/unit/**/${test.unit}Test.java" if="test.unit"/>
<exclude name="test/unit/**/Abstract*.java"/>
</fileset>
</batchtest>
</junit>
<fail message="one or more unit test failed" if="unit.test.failed"/>
</target>
This is what it looks like in my console:
unit.test:
[junit] Running test.unit.com.bazinga.sensormonitor.IBoxSensorDeviceTest
[junit] Tests run: 9, Failures: 0, Errors: 0, Time elapsed: 4.049 sec
[junit] Running test.unit.com.bazinga.sensormonitor.M2MSensorDeviceTest
[junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 1.586 sec
[junit] Running test.unit.com.bazinga.sensormonitor.RMoniSensorDeviceTest
[junit] Tests run: 21, Failures: 0, Errors: 1, Time elapsed: 5.385 sec
[junit] Test test.unit.com.bazinga.sensormonitor.RMoniSensorDeviceTest FAILED
[junit] Running test.unit.com.bazinga.sensormonitor.SensiteSensorDeviceTest
[junit] Tests run: 7, Failures: 0, Errors: 0, Time elapsed: 1.303 sec
[junit] Running test.unit.com.bazinga.sensormonitor.TBoxSensorDeviceTest
[junit] Tests run: 10, Failures: 0, Errors: 0, Time elapsed: 2.055 sec
BUILD FAILED
/home/user/projects/bazinga/admin/build/ant-library.xml:150: one or more unit test failed
Thanks for your help.
Normally one will not look at console for failure details. The standard junit task collect all information in result files ( under ${release.test.dir} in your case). Junit report task provides a html view of test result.
Most CI has
Maybe showoutput=true is what you are searching for? This would sned the test output to the console in addition to the logging files.
http://ant.apache.org/manual/Tasks/junit.html
I am trying to compile and run a simple java class within eclipse. The compile task works fine, and since I do not specify a destination folder the build files are in the same directory as the source. Which is alright, at the moment all I need is to learn how I can run the class with the main() method.
I have tried using the fully qualified name of the class (with package name, etc) and the classname alone, but always I get a java.lang.ClassNotFoundException
Buildfile: C:\Users....\build.xml
run:
[java] java.lang.NoClassDefFoundError: code/control/MyClass
[java] Caused by: java.lang.ClassNotFoundException: code.control.MyClass
[java] at java.net.URLClassLoader$1.run(Unknown Source)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(Unknown Source)
[java] at java.lang.ClassLoader.loadClass(Unknown Source)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
[java] at java.lang.ClassLoader.loadClass(Unknown Source)
[java] at java.lang.ClassLoader.loadClassInternal(Unknown Source)
[java] Could not find the main class: code.control.MyClass. Program will exit.
[java] Exception in thread "main"
[java] Java Result: 1
compile:
default:
BUILD SUCCESSFUL
Total time: 234 milliseconds
Below, are the targets taken from my build.xml file:
<target name="default" depends="compile" description="learn">
</target>
<target name="compile" depends="run">
<javac srcdir="src/" />
</target>
<target name="run">
<java classname="code.control.MyClass" fork="true"/>
</target>
I can't figure out why the class is not found. MyClass contains the main() method and since i specify no classpath it should look at the current directory, which is the src/ right?
The development directory is the usual eclipse file structure:
projectName/src/code/control/MyClass
If it is a classpath problem how could I resolve it? I always had problem grasping the concept "put it on your classpath" ... If someone could provide a little bit of explanation with the classpath in the ant context, I would be very thankful.
Thanks for any help on this. The version of ant is 1.7.0
The classpath is where the Java runtime looks for your .class files, similar to how your OS uses the PATH variable to find executables.
Try this in your build script:
<target name="run">
<java fork="true" classname="code.control.MyClass">
<classpath>
<path location="src/"/>
</classpath>
</java>
There's a HelloWorld version for ant that walks through building a Java program with ant.
you should include classpath, e.g.
<java classpath="${bin}" classname="code.control.MyClass">
where ${bin} is your output folder.
change your build.xml as below and try out:
<target name="default" depends="run" description="learn">
</target>
<target name="compile" >
<javac srcdir="src/" />
</target>
<target name="run" depends="compile">
<java classname="code.control.MyClass" fork="true"/>
</target>
I can't figure out why I am getting this exception from my ant build.xml file. I checked and everything is in the classpath. Why must this be so complicated?!
I had trouble with Ant in the past and it seems it always is something related to the classpath. I am pointing to junit.jar using both ways: within eclipse: window->preferences->ant->runtime->Ant Home->Add External Jars, and also within the build.xml script. This time Ant is not able to locate my test class in the junit task. Is there something wrong with the way I am pointing to this class?
<target name="init">
<property name="sourceDir" value="src"/>
<property name="outputDir" value="build" />
<property name="junitLocation" value="C:\...\org.junit4_4.3.1\junit.jar" />
</target>
<target name="clean" depends="init">
<delete dir="${outputDir}" />
</target>
<target name="prepare" depends="clean">
<mkdir dir="${outputDir}" />
</target>
<target name="compile" depends="prepare">
<javac srcdir="${sourceDir}" destdir="${outputDir}" classpath="${junitLocation}"/>
</target>
<path id="classpath">
<pathelement location="${outputDir}" />
<pathelement location="${junitLocation}" />
</path>
<target name="testApplication" depends="compile">
<echo>Running the junit tests...</echo>
<junit fork="yes" haltonfailure="yes">
<test name="my.package.MyTest" />
<formatter type="plain" usefile="false" />
<classpath refid="classpath" />
</junit>
</target>
I am always getting:
[junit] Testsuite: my.package.MyTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] Caused an ERROR
[junit] my.package.MyTest
[junit] java.lang.ClassNotFoundException: my.package.MyTest
[junit] at java.net.URLClassLoader$1.run(Unknown Source)
[junit] at java.security.AccessController.doPrivileged(Native Method)
[junit] at java.net.URLClassLoader.findClass(Unknown Source)
[junit] at java.lang.ClassLoader.loadClass(Unknown Source)
[junit] at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
[junit] at java.lang.ClassLoader.loadClass(Unknown Source)
[junit] at java.lang.ClassLoader.loadClassInternal(Unknown Source)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Unknown Source)
BUILD FAILED
Apparently, Ant finds junit.jar and attempts to start the test, but why can't it find my test class? I point to the folder with compiled test class. So I know that junit is on Ant's classpath at least, but the ClassNotFound puzzles me.
Any ideas perhaps? Many thanks!
Are you sure your test class is in the build folder? You're invoking junit in a separate JVM (fork=true) so it's possible that working folder would change during that invocation and with build being relative, that may cause a problem.
Run ant from command line (not from Eclipse) with -verbose or -debug switch to see the detailed classpath / working dir junit is being invoked with and post the results back here if you're still can't resolve this issue.
You don't need to add the JUnit jar to the classpath. If ant can't find it, the <junit> task won't run.
I suggest trying different ways of specifying the classpath as described in the ant doc at http://ant.apache.org/manual/using.html#path; specifically <classpath path="${srcdir}" /> might help.
If not, ChssPly76's suggestion of running ant -debug from the command line is the best bet. You'll want to bump up the buffer on your command prompt window though, because ant's debugger is extremely verbose.
I'm trying to run my junit tests using ant. The tests are kicked off using a JUnit 4 test suite. If I run this direct from Eclipse the tests complete without error. However if I run it from ant then many of the tests fail with this error repeated over and over until the junit task crashes.
[junit] java.util.zip.ZipException: error in opening zip file
[junit] at java.util.zip.ZipFile.open(Native Method)
[junit] at java.util.zip.ZipFile.(ZipFile.java:114)
[junit] at java.util.zip.ZipFile.(ZipFile.java:131)
[junit] at org.apache.tools.ant.AntClassLoader.getResourceURL(AntClassLoader.java:1028)
[junit] at org.apache.tools.ant.AntClassLoader$ResourceEnumeration.findNextResource(AntClassLoader.java:147)
[junit] at org.apache.tools.ant.AntClassLoader$ResourceEnumeration.nextElement(AntClassLoader.java:130)
[junit] at org.apache.tools.ant.util.CollectionUtils$CompoundEnumeration.nextElement(CollectionUtils.java:198)
[junit] at sun.misc.CompoundEnumeration.nextElement(CompoundEnumeration.java:43)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.checkForkedPath(JUnitTask.java:1128)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeAsForked(JUnitTask.java:1013)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:834)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:1785)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:785)
[junit] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
[junit] at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[junit] at java.lang.reflect.Method.invoke(Method.java:597)
[junit] at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
[junit] at org.apache.tools.ant.Task.perform(Task.java:348)
[junit] at org.apache.tools.ant.Target.execute(Target.java:357)
[junit] at org.apache.tools.ant.Target.performTasks(Target.java:385)
[junit] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
[junit] at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
[junit] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
[junit] at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
[junit] at org.apache.tools.ant.Main.runBuild(Main.java:758)
[junit] at org.apache.tools.ant.Main.startAnt(Main.java:217)
[junit] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
[junit] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)
my test running task is as follows:
<target name="run-junit-tests" depends="compile-tests,clean-results">
<mkdir dir="${test.results.dir}"/>
<junit failureproperty="tests.failed" fork="true" showoutput="yes" includeantruntime="false">
<classpath refid="test.run.path" />
<formatter type="xml" />
<test name="project.AllTests" todir="${basedir}/test-results" />
</junit>
<fail if="tests.failed" message="Unit tests failed"/>
</target>
I've verified that the classpath contains the following as well as all of the program code and libraries:
ant-junit.jar
ant-launcher.jar
ant.jar
easymock.jar
easymockclassextension.jar
junit-4.4.jar
I've tried debugging to find out which ZipFile it is trying to open with no luck, I've tried toggling includeantruntime and fork and i've tried running ant with ant -lib test/libs where test/libs contains the ant and junit libraries.
Any info about what causes this exception or how you've configured ant to successfully run unit tests is gratefully received.
ant 1.7.1 (ubuntu), java 1.6.0_10, junit 4.4
Thanks.
Update - Fixed
Found my problem. I had included my classes directory in my path using a fileset as opposed to a pathelement this was causing .class files to be opened as ZipFiles which of course threw an exception.
Found my problem. I had included my classes directory in my path using a fileset as opposed to a pathelement this was causing .class files to be opened as ZipFiles which of course threw an exception.
This error is caused specifically because the class path contains explicit references to one or more [files] that are not JAR's. The reference to "error in opening zip file" is of course that a JAR is in effect a ZIP file where as other files [JUNIT] has found like class files are not and as such do not have a zip format. So the class path should contain only explicit references to JAR [files] and/or the names of the [directories] where other resources like class files are to be found.
So when building up your class path (in ANT) use:
<path id="proj.class.path">
<pathelement location="c:/my/project/root" /> :one for each of the [directories] where class files, log4j property files and other resources etc are to be found
<fileset refid="my.file.set"> :to describe all the explicit JAR [files] that need to be on the class path.
</path>
where
<fileset id="my.file.set" dir="c:/where/i/keep/my/jars">
<filename name="myjar1.jar" />
<filename name="myjar2.jar" />
<filename name="myjar3.jar" />
</fileset>
or
NOTE: When using wild cards like [**/*] in you need to make sure the wild card is not matching files that are not JAR files
<fileset id="my.file.set" dir="c:/where/i/keep/my/jars">
<include name="**/*.jar" />
</fileset>
It sounds like there is an issue with paths.
Check following error source:
classpath: print out the classpath variable in a junit test, run it from eclipse and ant, so you can compare them
Check your project for absolute paths. Probably, ant uses other path prefixes than eclipse.
Some more information would help to help :)
Good luck!
Thanks guys for this information. I just want to add some tip from my experience. I have the same problem with junit as you when junit tryes to open license*.txt files in lib folder where *.jar resides.(Ivy resolve process puts them here) So
<path id="lib.path.id">
<fileset dir="${lib.dir}" includes="**.jar"/>
</path>
helps too.
If you are using Ubuntu or Debian, this will make JUnit (and some other libs) always available for Ant:
sudo apt-get install ant-optional