ClassNotFoundException on a JUNIT ant script - ant

I have written few test scripts using JUnit 4 and Selenium.
I have added the jar files for JUnit and Selenium to eclipse and if I run my tests through eclipse IDE everything is working as expected.
I am now trying to run these tests through the ant script below:
<project name="JUnit" default="test">
<property name="src" value="./src" />
<property name="classes" value="./classes" />
<property name="test.class.name" value="AllTests" />
<path id="test.classpath">
<pathelement location="${classes}" />
<pathelement location="C:/Program Files/eclipse 3.5/plugins/org.junit4_4.5.0.v20090824/junit.jar" />
<pathelement location="C:/selenium/selenium-server-standalone-2.0b2.jar" />
<pathelement location="C:/Program Files/eclipse 3.5/plugins/org.hamcrest.core_1.1.0.v20090501071000.jar" />
</path>
<target name="test">
<junit fork="yes" haltonfailure="yes">
<test name="${test.class.name}" />
<formatter type="plain" usefile="false" />
<classpath refid="test.classpath" />
</junit>
</target>
</project>
The problem is that when I run this ant script I am getting the following Exception:
[junit] java.lang.ClassNotFoundException: AllTests
[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.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Unknown Source)
I have added all the jar files that I see in eclipse .classpath file so a bit confused of which files I am missing.

Found the problem, stupid one but I guess it was lack of concentration...
My .class files are in the ./bin folder and the ant script was pointing to ./classes.
I copied it from an old ant script I've been using and didn't take notice of the properties configured.

Related

Jacoco coverage giving ERROR for org/apache/log4j/Category in java class

I am trying to do the code coverage using Jacoco. I have written the Jacoco configuration in ANT script.Jacoco working fine without logs in code.
But it is error out for loggers (apache log4j) added in my code.
I tried to exclude the logs by adding below code in my ant script as <jacoco:coverage destfile="${result.exec.file}" excludes="org.apache.log4j.*">
still it is giving me same error.
I am getting below exception:
15:35:16 [junit] Testcase: testUncoveredMethod took 0.009 sec
15:35:16 [junit] Caused an ERROR
15:35:16 [junit] org/apache/log4j/Category
15:35:16 [junit] java.lang.NoClassDefFoundError: org/apache/log4j/Category
15:35:16 [junit] at com.ant.sonar.jacoco.junit.code.coverage.JacacoCoverage.<clinit>(JacacoCoverage.java:6)
15:35:16 [junit] at com.ant.sonar.jacoco.junit.code.coverage.JacacoCoverageTest.testUncoveredMethod(JacacoCoverageTest.java:23)
15:35:16 [junit] Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Category
15:35:16 [junit] at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
15:35:16 [junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
15:35:16 [junit] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
15:35:16 [junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
Jacoco configuration
<target name="test" depends="compile">
<jacoco:coverage destfile="${result.exec.file}" excludes="org.apache.log4j.*">
<junit showoutput="true" printsummary="on" enabletestlistenerevents="true" fork="true" haltonfailure="no" forkmode="once">
<classpath path="${result.classes.dir}"/>
<classpath path="${junit.jar.path}"/>
<classpath path="${hamcrest.jar.path}"/>
<classpath location="../../ant-junit-1.9.5.jar"/>
<formatter type="plain" usefile="false"/>
<test name="com.ant.sonar.jacoco.junit.code.coverage.JacacoCoverageTest"/>
</junit>
</jacoco:coverage>
<!-- Step 3: Create coverage report -->
<jacoco:report>
<!--
This task needs the collected execution data and ...
-->
<executiondata>
<file file="${result.exec.file}"/>
</executiondata>
<!-- the class files and optional source files ... -->
<structure name="JaCoCo Ant Example">
<classfiles>
<fileset dir="${result.classes.dir}"/>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}"/>
</sourcefiles>
</structure>
<!-- to produce reports in different formats. -->
<html destdir="${result.report.dir}"/>
<csv destfile="${result.report.dir}/report.csv"/>
<xml destfile="${result.report.dir}/report.xml"/>
</jacoco:report>
</target>
I got the fix for this issue. Need to add log4j jar in junit classpath

Ant: java.sql.SQLException: unable to open database file

I try to build my java project by Ant.
Windows 7 Professional (64 bit), Java 1.8.
I run my custom ant's target:
ant create-dist-win-lite -Denvironment=development
Here build.xml
<target name="create-dist-win-lite" description="Create WIN Lite dinstribute">
<ac:var name="var.distribute.os.family" value="${prop.distribute.os.family.win}" />
<ac:var name="var.distribute.version.type" value="${prop.distribute.version.lite}" />
<ac:runtarget target="create-dist-os" />
</target>
<target name="db:sqlite-exec-queries" depends="db:sqlite-set-variables,db:set-db-url">
<echo message="var.data.dir.name=${var.data.dir.name}" />
<echo message="var.db.driver=${var.db.driver}" />
<echo message="var.db.current.url=${var.db.current.url}" />
<echo message="var.db.lib.file.name=${var.db.lib.file.name}" />
<delete dir="${var.data.dir.name}" />
<mkdir dir="${var.data.dir.name}" />
<sql driver="${var.db.driver}" url="${var.db.current.url}" userid="${var.db.user}" password="${var.db.pwd}" classpath="${lib.dir.name}/${var.db.lib.file.name}" delimiter="${var.db.delimiter}">
<transaction src="${db.dir}/${var.db.type}/create-tables.sql" />
<transaction src="${db.dir}/${var.db.type}/insert-data.sql" />
</sql>
</target>
As result, success created empty sqlite db file in c:\dev\ca\dist\data\ca.db.
But the folder c:\dev\ca\dist\data is READ only.
But target not success finish. When execute target db:sqlite-exec-queries I get error in this line:
<sql driver="${var.db.driver}" url="${var.db.current.url}" userid="${var.db.user}" password="${var.db.pwd}" classpath="${lib.dir.name}/${var.db.lib.file.name}" delimiter="${var.db.delimiter}">
Here full ant log:
db:sqlite-exec-queries:
[echo] var.data.dir.name=dist/data
[echo] var.db.driver=org.sqlite.JDBC
[echo] var.db.current.url=jdbc:sqlite://c:\dev\ca\dist\data/ca.db
[echo] var.db.lib.file.name=sqlitejdbc-v056.jar
[delete] Deleting directory c:\dev\ca\dist\data
[mkdir] Created dir: c:\dev\ca\dist\data
[sql] Executing resource: c:\dev\ca\db\sqlite\create-tables.sql
[sql] Failed to execute: CREATE UNIQUE INDEX [AccountStatuses_Index] ON [AccountStatuses]( [CODE] ASC )
BUILD FAILED
c:\dev\ca\build.xml:1396: java.sql.SQLException: unable to open database file
at org.sqlite.DB.execute(DB.java:275)
at org.sqlite.Stmt.exec(Stmt.java:56)
at org.sqlite.Stmt.execute(Stmt.java:83)
at org.apache.tools.ant.taskdefs.SQLExec.execSQL(SQLExec.java:775)
at org.apache.tools.ant.taskdefs.SQLExec.runStatements(SQLExec.java:745)
at org.apache.tools.ant.taskdefs.SQLExec$Transaction.runTransaction(SQLExec.java:1055)
at org.apache.tools.ant.taskdefs.SQLExec$Transaction.access$000(SQLExec.java:985)
at org.apache.tools.ant.taskdefs.SQLExec.execute(SQLExec.java:653)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at net.sf.antcontrib.logic.RunTargetTask.execute(RunTargetTask.java:48)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)

Source and Test "source" directories - how can I run tests from Test against Source

I've got a Java project which has a 'src' directory for code directly related to my project, and a 'test' directory which is JUnit tests that run against code in the 'src' directory. The directory structures are like this:
ROOT/src/com/mycompany/decoders/qDecoder/.....
ROOT/test/com/mycompany/decoders/qDecoder/.....
Both directories have the exact same package structure.
The following is the target and related JUnit info:
<property name="src_classes_dir" value="./bin"/>
<property name="test_classes_dir" value="./binTest"/>
<path id="junit.classpath">
<pathelement location="${src_classes_dir}"/>
<pathelement location="${test_classes_dir}"/>
<pathelement location="${CommonJarPath}/JUnit/junit.jar"/>
<pathelement location="${CommonJarPath}/JavaMail/mailapi.jar"/>
<pathelement location="${CommonJarPath}/JavaMail/smtp.jar"/>
<pathelement location="${CommonJarPath}/CommonsLang3/commons-lang3.jar"/>
<pathelement location="${CommonJarPath}/JDBC/inetsoftware/merlia/8.0.2/Merlia.jar"/>
<pathelement location="${CommonJarPath}/JodaTime/joda-time.jar"/>
</path>
<property name="test_dir" value="./test"/>
<target name="junit" description="Run JUnit tests" depends="compile_source, compile_test">
<junit fork="yes" haltonfailure="yes" haltonerror="yes" showoutput="yes">
<formatter type="plain" usefile="no"/>
<classpath refid="junit.classpath"/>
<batchtest>
<fileset dir="${test_dir}" />
</batchtest>
</junit>
</target>
So, my test files (*.java) are in ${test_dir}. The test classes are in the ${test_classes_dir} and the tested classes are in ${src_classes_dir}. Both are listed in the junit.classpath reference.
All of the targets are completed, 'clean', 'prep', 'src_compile', and 'test_compile' up to the 'junit' target, then I get a ClassNotFoundException on the very first test AMCiBitsTest. So it knows about the test, but for some reason cannot find its class.
Here's the exception trace:
junit:
[junit] Testsuite: com.amci.util.AMCiBitsTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit]
[junit] Caused an ERROR
[junit] com.amci.util.AMCiBitsTest
[junit] java.lang.ClassNotFoundException: com.amci.util.AMCiBitsTest
[junit] at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
[junit] at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
[junit] at java.security.AccessController.doPrivileged(Native Method)
[junit] at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[junit] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
[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:190)
[junit]
I'm stumped. What am I doing wrong? Why can't this run the AMCiBitsTest test and all of the other tests?
I've fixed it. Suffice it to say that I made a syntax error in my build file, and what is posted above actually DOES WORK. My apologies, and thanks for your questions and your time.

Exception thrown while running JMockit tests through ant script

I am trying to run JMcokit tests using ant script. But I am getting exceptions. I have tried to include java agent using 'jvmargs' as mentioned in other posts, also tried to add jmockit jar before junit jar in the classpath, but its of no use.
If I add java agent argument in my eclipse, then I am able to run them through eclipse, but not through ant script.
The only way I am able to run them through ant script is by adding attach.dll in my java path. But that is not a feasible solution and need a work around.
Ant script:
<!-- Run the TestSuite -->
<junit fork="yes" description="Runs the Junit testcase." printsummary="yes"
haltonfailure="${halt.on.junit.failure}" showoutput="yes">
<sysproperty key="java.library.path" path="C:/Program Files/Java/jdk1.6.0_12/jre/bin/attach.dll"/>
<jvmarg value="-javaagent:${lib.dir}/jmockit.jar" />
<jvmarg value=" -Dcom.sun.management.jmxremote"/>
<!-- <classpath path="${lib.dir}/jmockit-0.999.8.jar" />
<classpath path="${lib.dir}/junit-4.7.jar" />-->
<classpath>
<pathelement location="${lib.dir}/jmockit-0.999.8.jar" />
<pathelement location="${lib.dir}/junit-4.7.jar" />
<pathelement location="${lib.dir}/activemq-all-5.5.1.jar" />
<pathelement location="${classes.dir}" />
<!--<fileset dir="${lib.dir}">
<include name="**/*.jar" />
<exclude name="**/jmockit-0.999.8.jar, **/junit-4.7.jar" />
</fileset>-->
</classpath>
<!-- <test name="com.quest.shareplex.hadoopconnector.junits.AllTests"
fork="no" todir="${junit.output.dir}"> <formatter type="xml" /> </test> -->
<batchtest fork="no" todir="${junit.output.dir}">
<formatter type="xml" />
<fileset dir="${connector.src}">
<include name="**/*Test.java" />
<exclude name="**/*HDFSUtilsTest*.java" />
</fileset>
</batchtest>
</junit>
Please let me know, if there is any other way to work it, or if I am doing something incorrectly.
I have tried all the ways that are mentioned on stack overflow:
-Tried to change java lib path
-Tried to change java class path
-Added java agent
But nothing is working.
Exception:
<error type="java.lang.reflect.InvocationTargetException">java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.eclipse.ant.internal.launching.remote.EclipseSingleCheckExecutor.executeTargets(EclipseSingleCheckExecutor.java:30)
at org.eclipse.ant.internal.launching.remote.EclipseSingleCheckExecutor.executeTargets(EclipseSingleCheckExecutor.java:30)
at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138)
</error>
<system-out><![CDATA[]]></system-out>
<system-err><![CDATA[java.lang.UnsatisfiedLinkError: no attach in java.library.path
java.lang.IllegalStateException: Unable to load Java agent; please add lib/tools.jar from your JDK to the classpath
at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:82)
at mockit.internal.startup.JDK6AgentLoader.loadAgent(JDK6AgentLoader.java:54)
at mockit.internal.startup.AgentInitialization.initializeAccordingToJDKVersion(AgentInitialization.java:21)
at mockit.internal.startup.Startup.initializeIfNeeded(Startup.java:203)
at org.junit.runner.Runner.<clinit>(Runner.java:25)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at junit.framework.JUnit4TestAdapter.<init>(JUnit4TestAdapter.java:31)
at junit.framework.JUnit4TestAdapter.<init>(JUnit4TestAdapter.java:24)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:473)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeInVM(JUnitTask.java:1420)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:848)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:1899)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:800)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:38)
at org.eclipse.ant.internal.launching.remote.EclipseSingleCheckExecutor.executeTargets(EclipseSingleCheckExecutor.java:30)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:442)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:38)
at org.eclipse.ant.internal.launching.remote.EclipseSingleCheckExecutor.executeTargets(EclipseSingleCheckExecutor.java:30)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:442)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138)
Thanks,
Mayank
It looks like your java is plain jre.
java.library.path java.lang.IllegalStateException: Unable to load Java
agent; please add lib/tools.jar from your JDK to the classpath
at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:82)
at mockit.internal.startup.JDK6AgentLoader.loadAgent(JDK6AgentLoader.java:54
Jmockit needs JDK to work.
If you are on a Mac, this is happening because the forked JVM doesn't have tools.jar in the classpath. I encountered this today and blogged about it.

Ant build Error: when I try to run This error will generate

root#ubuntu:/home/dj/workspace/TestProject# ant test
Buildfile: /home/dj/workspace/TestProject/build.xml
test:
[junit] Testsuite: TestProject
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit]
[junit] Caused an ERROR
[junit] TestProject
[junit] java.lang.ClassNotFoundException: TestProject
[junit] at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
[junit] at java.security.AccessController.doPrivileged(Native Method)
[junit] at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
[junit] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:169)
[junit]
BUILD FAILED
/home/dj/workspace/TestProject/build.xml:17: Test TestProject failed
Total time: 0 seconds
<project name="TestProject" basedir="." default="" >
<property name="src" value="./src" />
<property name="lib" value="./lib" />
<property name="classes" value="./classes" />
<property name="ant.project.name" value="com.megacorp.projx.util" />
<path id="test.classpath">
<pathelement location="${classes}" />
<pathelement location="usr/junit.jar" />
<fileset dir="${lib}">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="test">
<junit fork="yes" haltonfailure="yes">
<test name="${ant.project.name}" />
<formatter type="plain" usefile="false" />
<classpath refid="test.classpath" />
</junit>
</target>
This is wrong:
<test name="${ant.project.name}" />
Here you should set the name of the junit-class you want to test, and not the name of the ant project.
E.g.:
<test name="myorg.MyTest" />
Links:
Documentation for junit task: have a look at the examples at the Bottom

Resources