I am getting following error while running testng.xml
ant run_testNG
Buildfile: build.xml
run_testNG:
[testng] Exception in thread "main" java.lang.NullPointerException
[testng] at org.testng.xml.Parser.parse(Parser.java:189)
[testng] at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:351)
[testng] at org.testng.TestNG.run(TestNG.java:854)
[testng] at org.testng.TestNG.privateMain(TestNG.java:1178)
[testng] at org.testng.TestNG.main(TestNG.java:1137)
[testng] The tests failed.
BUILD SUCCESSFUL
Total time: 0 seconds
my testng.xml looks like :
<project default="test">
<path id="cp">
<pathelement location="/softwares/lib/testng-5.13.1.jar"/>
<pathelement location="/softwares/cruisecontrol-bin-2.8.3/run_test/run_test/build"/>
</path>
<taskdef name="testng" classpathref="cp" classname="org.testng.TestNGAntTask" />
<target name="test">
<testng classpathref="cp" groups="fast">
<classfileset dir="build" includes="com/example/test/*.class"/>
</testng>
</target>
</project>
my build.xml file is:
<property name="lib.dir" value="/softwares/lib"/>
<property name="src.dir" value="/softwares/src" />
<target name="run_testNG" description="Run TestNG">
<testng classpathref="compile.classpath" haltOnfailure="false">
<xmlfileset dir="/softwares/cruisecontrol-bin-2.8.3/run_test/" includes="testng.xml" />
</testng>
</target>
<target name="start-server">
<java jar="/softwares/selenium/selenium-server-standalone-2.0b2.jar" fork="true" spawn="true">
<arg line="-timeout 30"/>
<jvmarg value="-Dhttp.proxyHost=proxy.corporate.com"/>
<jvmarg value="-Dhttp.proxyPort=3128"/>
</java>
</target>
<target name="stop-server">
<get taskname="selenium-shutdown"
src="http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer"
dest="result.txt" ignoreerrors="true" />
<echo taskname="selenium-shutdown" message="DGF Errors during shutdown are expected" />
</target>
<taskdef resource="testngtasks" classpath="/softwares/selenium/selenium-2.0b2/libs/testng-5.14.1.jar" />
Can anybody help me...why I am getting this error. Your help will be appreciated
It looks like the problem is that that is not a testng XML file. It is an Ant build file.
For information on what a real testng XML file should look like, see here. Alternatively, rename the file and use Ant to run the tests; e.g.
$ mv testng.xml build.xml
$ ant test
However, it is curious that testng fails in such a spectacular way when you give it an XML file that it doesn't grok. Insufficient testing perhaps? :-)
Looks to me as if it can't find your testng.xml file. That's one possible reason for a NPE thrown by a XML Parser: a null input. I see that ant defaults to build.xml. Do you have a build.xml file too?
Look at the run_testNG script, may be you can/have to add a full path to your file.
I had the same problem,
but found I was picking up a dbUnit dataset file that happend to match the pattern I had set up to pick up testng xml file (*test.xml).
Simply renamed the file and I was good to go.
Hope this helps, even though the question was asked a while ago.
Related
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
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
I have the following project structure:
src/com/dummy/abc.java
src_tests/come/dummy/abcTest.java
build.xml
I need to check the coverage of my code done by tests using emma.
From reading the emma + junit examples i came to a conclusion that to get a report i need to do the following:
compile 'src'
compile 'src_tests'
instrument compiled 'src_tests' => 'instrumented_src_tests'
run junit on 'instrumented_src_tests' with additional jvmarg
The problem is that the step 4 should return some kind of a file which then used with 'report' command should create a report. I'm getting
emma-report:
[report] processing input files ...
[report] 1 file(s) read and merged in 67 ms
[report] nothing to do: no runtime coverage data found in any of the data files
~ edit
i'm attaching my build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="HELL scream" default="all" basedir=".">
<property name="build.sources.dir" location="${basedir}/src"/>
<property name="build.sources.des" location="${basedir}/bin/classes"/>
<property name="test.sources.dir" location="${basedir}/src_test"/>
<property name="test.sources.des" location="${basedir}/bin/classes_test"/>
<property name="test.reports.des" location="${basedir}/reports-junit"/>
<property name="emma.sources.des" location="${basedir}/bin/classes_emma"/>
<property name="emma.reports.des" location="${basedir}/reports-emma"/>
<property name="emma.final.reports.des" location="${basedir}/reports-emma/final"/>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<path id="emma.lib" >
<fileset dir="/home/user1/Desktop/emma-2.0.5312/lib">
<include name="*.jar"/>
</fileset>
</path>
<taskdef resource="emma_ant.properties" classpathref="emma.lib" />
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<target name="clean-all">
<delete failonerror="false">
<fileset dir="${emma.final.reports.des}"/>
<fileset dir="${emma.reports.des}"/>
<fileset dir="${emma.sources.des}"/>
<fileset dir="${test.reports.des}"/>
<fileset dir="${test.sources.des}"/>
<fileset dir="${build.sources.des}"/>
</delete>
</target>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<target name="compile-sources">
<mkdir dir="${build.sources.des}"/>
<javac srcdir="${build.sources.dir}" includes="" excludes="" destdir="${build.sources.des}" listfiles="true" debug="true" includeantruntime="false"/>
</target>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<target name="compile-tests">
<mkdir dir="${test.sources.des}"/>
<javac srcdir="${test.sources.dir}" includes="" excludes="" destdir="${test.sources.des}" listfiles="true" debug="true" includeantruntime="false">
<classpath>
<pathelement location="/home/user1/Desktop/junit-4.10.jar"/>
<pathelement location="${build.sources.des}"/>
</classpath>
</javac>
</target>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<target name="compile-emma-tests">
<emma enabled="true" >
<instr instrpath="${test.sources.des}" destdir="${emma.sources.des}" metadatafile ="${emma.reports.des}/instrumentation.emma" merge ="true"/>
</emma>
</target>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<target name="run-tests">
<mkdir dir="${test.reports.des}"/>
<junit haltonfailure="no" showoutput="yes" printsummary="true">
<formatter type="plain" usefile="false" />
<formatter type="xml"/>
<classpath>
<pathelement location="/home/user1/Desktop/junit-4.10.jar"/>
<pathelement location="${build.sources.des}"/>
<pathelement location="${emma.sources.des}"/>
<path refid="emma.lib" />
</classpath>
<batchtest todir="${test.reports.des}" fork="true">
<fileset dir="${emma.sources.des}"/>
</batchtest>
<jvmarg value="-Demma.coverage.out.file=${emma.reports.des}/coverage.emma" />
<jvmarg value="-Demma.coverage.out.merge=false" />
</junit>
</target>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<target name="junit-tests-report">
<junitreport todir="${test.reports.des}">
<fileset dir="${test.reports.des}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${test.reports.des}/junit_reports"/>
</junitreport>
</target>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<target name="emma-tests-report">
<emma enabled="true" >
<report sourcepath="${build.sources.dir}">
<fileset dir="${emma.reports.des}" >
<include name="*.emma" />
</fileset>
<txt outfile="${emma.final.reports.des}/coverage.txt" depth="package" columns="class,method,block,line,name" />
<xml outfile="${emma.final.reports.des}/coverage.xml" depth="package" />
<html outfile="${emma.final.reports.des}/coverage.html" depth="method" columns="name,class,method,block,line" />
</report>
</emma>
</target>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<target name="all" depends="clean-all, compile-sources, compile-tests, compile-emma-tests, run-tests, junit-tests-report, emma-tests-report"/>
</project>
it is probably something trivial...
also, when using emma.sources.dest
i'm getting this on my (only) test
run-tests:
[junit] Running com.emma.test.MathTest
[junit] Testsuite: com.emma.test.MathTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit]
[junit] Caused an ERROR
[junit] Illegal local variable table length 5 in method com.emma.test.MathTest.<init>()V
[junit] java.lang.ClassFormatError: Illegal local variable table length 5 in method com.emma.test.MathTest.<init>()V
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:188)
[junit]
[junit] Test com.emma.test.MathTest FAILED
~ SOLVED
add this:
<jvmarg value="-XX:-UseSplitVerifier"/>
<jvmarg value="-Demma.coverage.out.file=${emma.reports.des}/coverage.emma" />
<jvmarg value="-Demma.coverage.out.merge=false" />
Emma hasn't had a stable release since 2005, and doesn't play well with newer versions of the JDKs. Cobertura hasn't done any new development since 2011.
Because of that, I no longer recommend to developers to use Emma or Cobertura.
I've switched to JaCoCo for code coverage. It was started by the Emma Eclipse team, and is actively being developed.
As stated in JaCoCo's mission statement.
[...]Two of the best and widely used available open source tools are EMMA and Cobertura. Both tools are not actively maintained by the original authors any more and do not support the current Java versions. Due to the lack of regression tests maintenance and feature additions is difficult.
Therefore we started the JaCoCo project to provide a new standard technology for code coverage analysis in Java VM based environments. [...]
The nice thing about JaCoCo is that it works in a wide variety of environments, and you don't have to instrument your code after compiling. The instrumentation takes place as the tests are being executed. It looks something like this:
<jacoco:coverage destfile="${target.dir}/jacoco.exec">
<junit>
[...]
</junit>
</jacoco>
You then use <jacoco:coverage> to print the coverage report.
Do you have to use Emma? If not, you might better luck getting everything working using JaCoCo.
Emma relies upon bytecode injection (if memory serves me well); but, with Java 7, the code hasn't been updated because the JVM now has a debugging interface.
JaCoCo tests code coverage by attaching to the debugging interface and listening as your code enters methods and passes over the bytecode without injecting checkpoints. It's clearly the superior way to do things (now that it exists) and I doubt that Emma will do the work to fully support Java 7.
I suggest that you "upgrade" to JaCoCo.
You get this error when you use wrong command to generate the report.
Most of the online tutorial is advocating the wrong(may by old) command, even I got this error when I used below command to generate the report:
{JAVA_HOME}\jre\lib\ext>java -cp emma.jar emma report -r html -in coverage.em, {ALFRESCO_HOME}\coverage.ec
EMMA: processing input files ...
EMMA: 1 file(s) read and merged in 60 ms
EMMA: nothing to do: no runtime coverage data found in any of the data files
Correct usage of the command is:
{JAVA_HOME}\jre\lib\ext>java -cp emma.jar emma report -r txt,html -in {JAVA_HOME}\jre\lib\ext\coverage.em -in C:\t1_tempSetup\Alfresco\coverage.ec
EMMA: processing input files ...
EMMA: 2 file(s) read and merged in 70 ms
EMMA: writing [txt] report to [{JAVA_HOME}\jre\lib\ext\coverage.txt] ...
EMMA: writing [html] report to [{JAVA_HOME}\jre\lib\ext\coverage\index.html] ...
I got a TestNG test, that works perfectly fine on it's own. Somewhere in it I got:
URL fileName = this.getClass().getClassLoader().getResource("config.properties");
I'm trying to run the same test using Ant:
<testng outputDir="${resultsFolder}" haltOnFailure="false">
<classpath>
<fileset dir="./src/">
<include name="**/*.properties"/>
</fileset>
-----------------------------...--------------------
</classpath>
<classfileset dir="correct path here" includes="**/*.class" />
</testng>
I can see in debug mode that the config.properties is in the classpath. But the line at the top can not find it, it is null.
EDIT: I solved it.The critical line actually does not search for the file in the classpath directly, it searches IN the files/folders. So, this solved my problem:
<pathelement location="src/"/>
Thanks for the help.
try to replace your <classpath>...</classpath> with this:
<classpath>
<pathelement path="./src"/>
</classpath>
In order for JVM to find the config.properties the parent directory of config.properties should be in classpath.
I created my first Ant script and it's working pretty well. It compiles all my java sources, creates a .jar file and runs the program without even any warning.
But when i try to run my .jar from command line i get NoClassDefFoundError exceptions.
So, how to translate this Ant snippet to work from command line?
<property name="main.class" value="de.bfs.radon.omsimulation.OMMainFrame"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<target name="run" depends="jar">
<java fork="true" classname="${main.class}">
<classpath>
<path refid="classpath"/>
<path location="${bin.dir}/omsimulation-${version}.jar"/>
</classpath>
</java>
</target>
This is the command line:
# java -classpath lib/ -jar bin/omsimulation-0.4.45-beta3.jar
Throws:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/toedter/calendar/JDateChooser
at de.bfs.radon.omsimulation.OMMainFrame.(OMMainFrame.java:133)
at de.bfs.radon.omsimulation.OMMainFrame$1.run(OMMainFrame.java:106)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
Why does my .jar file not work with the defined classpath? Further down the Ant script:
<target name="jar" depends="manifest">
<mkdir dir="${bin.dir}" />
<jar jarfile="${bin.dir}/omsimulation-${version}.jar" manifest="${src.dir}/omsimulation.manifest" compress="no" basedir="${build.dir}" includes="de/**" />
</target>
<target name="manifest" depends="compile">
<manifestclasspath property="manifest.cp" jarfile="${bin.dir}/omsimulation-${version}.jar">
<classpath refid="classpath" />
</manifestclasspath>
<manifest file="${src.dir}/omsimulation.manifest">
<attribute name="Built-By" value="${author}"/>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</target>
Again, running the Ant script works fine. I even tried adding the said libraries to my .jar but that only blows up the file size, the problem still persists.
<jar jarfile="${bin.dir}/omsimulation-${version}.jar" manifest="${src.dir}/omsimulation.manifest" compress="no" basedir="${build.dir}"> <!-- includes="de/**" /-->
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</jar>
Any ideas on this?
Thanks a lot,
donc_oe
SOLVED: Thanks to perception, the unix command line i was looking for is:
# java -cp lib/*:bin/myjarfile.jar my.package.MyMainClass
And for Windows (note the ; semicolon):
# javaw -cp lib/*;bin/myjarfile.jar my.package.MyMainClass
The relevant thing to note from your build script is this:
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
With that little snippet you have defined a path construct in Ant, which you then refer to in your run task:
<target name="run" depends="jar">
<java fork="true" classname="${main.class}">
<classpath>
<path refid="classpath"/>
<path location="${bin.dir}/omsimulation-${version}.jar"/>
</classpath>
</java>
</target>
This is in effect executing:
java -cp ${lib.dir}/*.jar:${bin.dir}/omsimulation-${version}.jar ${main.class}
Of course, it does so without the squigly lines and the path(s) fully substituted. The main point being that the command you are trying to run yourself is not equivalent at all. When attempting to invoke from the command line you will need to include in the classpath all the necessar JAR's containing your code and all third party libraries. Assuming everything is still bundled in the Ant created folders, something like:
java -cp <full-path-to-lib>/* -jar <full-path-to-bin>/omsimulation-0.4.45-beta3.jar
Or:
java -cp <full-path-to-lib?/*:<full-path-to-bin>/omsimulation-0.4.45-beta3.jar <MainClass>
ClassDefNotFoundException most likely occur when class is found in classpath, but it is loaded in different classloader, or in a wrong path etc.
From the build file, you appears to create jar that include other jars. This may not give a result you want.
Most likely you want a solution described in Easiest way to merge a release into one JAR file. My personal favorite is one-jar.