Unit test fails only when running from with Ant task - java

I run the same tests from both eclipse and from an Ant task. When running from eclipse all tests pass. When I run Ant junit task, one, single test fails with the following strange error:
junit.framework.AssertionFailedError
at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:423)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:137)
at unitests.mypackage.MyTestClass.myTestCase(Unknown Source)
What can be the cause?
I read a little and found out that it might be because eclipse and Ant use different versions of junit. In my project, junit is located at libs/junit-4.10.jar and referenced both in eclipse's .classpath file and in junit task classpath.
You can see Ant's task here:
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
<fileset dir="${src.dir}" includes="**/*.jar"/>
</path>
...
<target name="run-unit-tests" depends="compile,compile-unit-tests">
<mkdir dir="${junit.output.dir}"/>
<junit fork="yes" printsummary="yes" haltonfailure="no">
<classpath>
<path refid="classpath"/>
<fileset dir="${unit.tests.classes.dir}" includes="**/*.class"/>
</classpath>
<formatter type="xml"/>
<batchtest todir="${junit.output.dir}">
<fileset dir="${unit.tests.dir}">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
<mkdir dir="${junit.report.dir}"/>
<junitreport todir="${junit.report.dir}">
<fileset dir="${junit.output.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${junit.report.dir}/html"/>
</junitreport>
</target>
The version of Ant is 1.7.1 and it came with eclipse.
Edit:
Eventually solved it by adding fork="yes" to junit's task. Found it by generating build file using eclipse's export option, and then looking at the differences between the generated file and mine. No idea why forking solves the problem though.

Different versions of Ant sound like a likely cause. To test that, set which Ant version you want Eclipse to use.
It's probably also a good idea to set the same version of JUnit and the same JDK that you use to run the test outside the IDE.

As the stack trace shows org.eclipse entries, I strongly suspect you are running ant via Eclipse. If you run ant from the console you will very likely not have this problem.
The cause, therefore, is a bug in Eclipse. This should be no surprise, Eclipse is full of bugs.
fork="true" helps because it causes ant to spawn a new process for execution. This new process does not have any Eclipse classes on the classpath.

Related

I am able to compile a JUnit program through ant build.xml but while executing it I am getting NoClassFounder exception

I am new to JUnit and ant. I doubt whether my build.xml is correct. I am able to generate .class file through compile tag in target. But I am not able to execute the file. When I try to execute the test tag in the below script. I got java.lang.ClassNotFoundException, this is the where I got stuck.
Same program works in eclipse IDE fine.. But not able to execute through ant only
I have verified classpath for java and ant multiple times. It is fine too
I am trying to sort this out from past three days..But still problem persists..
Someone please help me
<target name="compile" description = "Compiling java code">
<javac srcdir="D:/AntBuilder/src" destdir="D:/AntBuilder/build" classpath= "D:/AntBuilder/dist/lib" includeantruntime="true" />
</target>
<target name="test" depends="compile" description="Execute Unit Tests" >
<junit printsummary="yes" fork="yes" haltonfailure="yes">
<formatter type="xml" />
<test name="automation.LogInTest" />
<classpath>
<pathelement location="D:/AntBuilder/dist/lib"/>
<pathelement location="D:/AntBuilder/build"/>
</classpath>
</junit>
</target>
Vamshi G
As per the stacktrace, it is not able to locate WEbDriver.exe. Download the file and place it where all jars are located.
Sorted out the issue now.
Problem lies with adding jar files in lib folder. Along with selenium-java-2.41.0.jar file we need to download one more jar file.. selenium-server-standalone-2.41.0.jar

Travis CI With Ant Build, Not Finding JUnit

I'm trying to figure out how to get Travis CI working with my little Java library on Github.
The problem seems to be that whenever the build process gets to the compilation stage, it won't compile the unit tests because it can't seem to find the JUnit jar file. Of course the Ant script works beautifully on my own computer, but I can't get the classpath right on Travis. How am I supposed to know where (or even if) they installed JUnit?
Here's my Ant script:
<project>
<target name="test">
<delete dir="build" />
<mkdir dir="build" />
<javac includeantruntime="false" srcdir="src" destdir="build" />
<javac includeantruntime="false" srcdir="tests" destdir="build" classpath="/usr/share/java/junit.jar" />
<junit printsummary="on">
<classpath>
<pathelement location="build" />
<pathelement path="/usr/share/java" />
</classpath>
<test name="FactorizeTest" />
</junit>
</target>
</project>
Here's the project link, notice the pretty "build failing" icon. Yay.
https://github.com/The-Craw/PrimeFactorizer
And finally here's the link to the build output. You can also get this from clicking the build icon.
https://travis-ci.org/The-Craw/PrimeFactorizer
You need the junit.jar on your classpath. (I think that is in Ant's lib directory on your locale machine).
You may have a look at the project template https://github.com/mplacona/java-junit-template-project

Why does Cobertura seem to switch my Eclipse's Java version?

These are the steps that create the error:
Run my unit tests within Eclipse (Cntrl-F11). All 67 succeed and report as such.
Using an Ant build.xml file, run a Cobertura task to generate a coverage report. The task fails stating that a unit test has failed.
When I try to run the unit tests using Eclipse again, I receive the following error:
Unsupported major.minor version 51.0
(These symptoms persist until I use Project -> Clean within Eclipse.)
My Cobertura ant task is:
<target name="report-test" description="Generate a test coverage report." depends="clean, compile">
<taskdef resource="tasks.properties">
<classpath>
<pathelement location="${lib.dir}/cobertura-1.9.4.1/cobertura.jar" />
<pathelement location="${lib.dir}/cobertura-1.9.4.1/lib/asm-3.0.jar" />
<pathelement location="${lib.dir}/cobertura-1.9.4.1/lib/asm-tree-3.0.jar" />
<pathelement location="${lib.dir}/cobertura-1.9.4.1/lib/log4j-1.2.9.jar" />
<pathelement location="${lib.dir}/cobertura-1.9.4.1/lib/jakarta-oro-2.0.8.jar" />
</classpath>
</taskdef>
<cobertura-instrument todir="${build.dir}/cobertura-instrument">
<fileset dir="${build.dir}">
<include name="**/*.class"/>
</fileset>
</cobertura-instrument>
<junit printsummary="yes" fork="true" haltonfailure="yes" showoutput="yes">
<classpath location="${build.dir}/cobertura-instrument"/>
<classpath location="${build.dir}"/>
<classpath refid="classpath.test" />
<sysproperty key="net.sourceforge.cobertura.datafile" file="cobertura.ser"/>
<formatter type="xml" />
<batchtest todir="doc/junit">
<fileset dir="${test.dir}" />
</batchtest>
</junit>
<cobertura-report srcdir="${src.dir}" destdir="doc/coverage" format="xml" />
<delete file="cobertura.ser"/>
</target>
Coberatura hasn't switched Eclipse's Java version.
What it has actually done is recompile the classes using a version of Java that is more recent than the one you are using to run Eclipse. Eclipse can't load those .class files.
The short term solution is to get Eclipse to clean and rebuild the project(s) after running Coberatura ... as you are currently doing.
In the long term, you should either change Eclipse to run using the same Java version as your Ant builds, or change the Ant build file so that it doesn't write the ".class" files into the Eclipse workspace. Or both ... 'cos having something else writing stuff into the Eclipse workspace is going to cause other problems too.

Using Cobertura in unix to generate the code coverage reports

I m using cobertura-1.9.4.1 to generate code coverage reports.First I set the classpath to cobertura.jar and to other jars in the lib folder. Then I execute cobertura-instrument.sh.
But on executing I get the error loaded information on 0 classes . I m giving the complete path to the compiled classes still it is unable to instrument the classes .
So, what am I missing or what could be the possible reasons for this.
Do you mean the error is during the instrumentation, or that after running your tests, the coverage still shows zero?
Here's an example of instrumentation (with Ant):
<target name="--coverage.instrument">
<delete file="cobertura.ser"/>
<mkdir dir="${coverage.instrumented.dir}"/>
<cobertura-instrument todir="${coverage.instrumented.dir}">
<fileset dir="${classes.main.dir}">
<include name="**/*.class"/>
<exclude name="**/*Test.class"/>
</fileset>
</cobertura-instrument>
</target>
Don't forget that you need this sysproperty when testing (eg in Ant Junit task):
<sysproperty key="net.sourceforge.cobertura.datafile" file="cobertura.ser"/>
Once Cobertura is set up an instrumentation has happened, an example of execution:
<target name="--test.unit">
<mkdir dir="${temp.dir}/unit-tests"/>
<junit forkmode="perBatch" printsummary="yes" haltonfailure="no" haltonerror="no"
failureproperty="unit.tests.failed">
<sysproperty key="net.sourceforge.cobertura.datafile" file="cobertura.ser"/>
<classpath refid="classpath.test.utest"/>
<formatter type="xml"/>
<batchtest fork="yes" todir="${temp.dir}/unit-tests">
<fileset dir="${java.src.utest.dir}" includes="**/*Test.java"/>
</batchtest>
</junit>
</target>
I believe that recent versions of Cobertura don't work well with JDK5. Strongly suggest upgrading the JDK.

How do I set the working directory for the Ant 'junit' task?

My Ant build includes a junit task that runs some tests. In order for the tests to work, the value of the property that specifies the current working directory (user.dir) must be changed, but I am unsure how to achieve this.
The task in question currently looks like this:
<junit printsummary="withOutAndErr" fork="true"
haltonfailure="yes" showoutput="true"
filtertrace="false" dir="C:/workspace/obp-web">
<jvmarg value="-Duser.dir=C:/workspace/obp-web"/>
<classpath>
<fileset dir="${web.lib.dir}" includes="**/*.jar"/>
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</classpath>
<batchtest fork="no" todir="${web.build.dir}/testresults">
<formatter type="xml"/>
<zipfileset src="${web.build.dir}/test-obp-web.jar">
<include name="**/*Test.class"/>
</zipfileset>
</batchtest>
</junit>
Notice that I've tried to use both the "dir" attribute and the "jvmarg" task to change the working directory to C:/workspace/obp-web. However when I run Ant with verbose output turned on, I see the following output, which indicates that the working dir has not been set correctly:
[junit] dir attribute ignored if running in the same VM
[junit] Using System properties
{java.runtime.name=Java(TM) SE Runtime
Environment,
sun.boot.library.path=c:\jdk6\jre\bin,
java.vm.version=10.0-b23, ant.lib
rary.dir=C:\java\apache-ant-1.7.0\lib,
java.vm.vendor=Sun Microsystems Inc.,
java.vendor.url=http://java.sun.com/,
path.separator=;, java.vm.name=Java
HotSpot(T M) Client VM,
file.encoding.pkg=sun.io,
user.country=CA,
sun.java.launcher=SUN_STANDARD,
sun.os.patch.level=Service Pack 1,
java.vm.specification.name=Java Virtual Machine Specification,
user.dir=c:\workspace\obp-ear, java.runtime.version=1.6.0_07-b06,
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment,
java.endorse
d.dirs=c:\jdk6\jre\lib\endorsed,
os.arch=x86,
java.io.tmpdir=C:\Users\donal\AppData\Local\Temp\,
line.separator=
Use the attribute "dir" (must also fork the vm):
http://ant.apache.org/manual/Tasks/junit.html
Try using a jvmarg:
<junit fork="yes">
<jvmarg value="-Duser.dir=somedir"/>
...
</junit>
Note that fork must be true on both the junit tag and the batchtest tag as the batchtest tag overrides the value from junit. Jvmargs only work if junit forks a new JVM.
Same problem as you.
I resolved it by making the batchtest fork to true :
batchtest fork="no" ..
to
batchtest fork="yes" ..
Have you tried pathelement location? This worked for me.
<classpath>
<!-- filesets, etc. -->
<pathelement location="C:/workspace/obp-web" />
</classpath>

Categories