Testing with Ant - Cannot test files in subdirectories - java

I have an android application in java. I want to test it using a xml script with ant. Now we have several packages and a standard directory structure for our source and test files.
There is a "tests" folder (root of all test code) which has 1 file say A.java and some sub-folders which again in-turn contain some test files or sub-sub-folders. Only file A.java gets tested and rest others are not tested. If i change the directory path in xml script from "tests" to say "tests/ui" then test files in folder "ui" are not able to test and i get some error - Class Not Found Error when i check out the files in "test-result" folder where i store the output of the test.
I have included the parent directory of compiled codes and required external jars in path with id = "test.classpath". I think i should every file of type .class rather then path to parent folder.
The code is :
<target name="test-run" description="Run Test">
<delete dir = "test_result" />
<mkdir dir = "test_result" />
<junit printsummary="yes" haltonfailure="yes" showoutput="yes" >
<classpath refid="test.classpath"/>
<batchtest fork="yes" todir="test_result">
<formatter type="xml"/>
<fileset dir="tests" includes="**/*" />
</batchtest>
</junit>
</target>
Can you please explain me how to achieve my motive assuming a generic file structure.
EDIT :
Following is the path used in above xml where folder out contains .class files of source and test files generated by idea ide :
<path id="test.classpath">
<pathelement path="testlibs/jmockit.jar"/>
<pathelement path="testlibs/jmockit-coverage.jar"/>
<pathelement path="testlibs/jmockit-coverage-htmlfull.jar"/>
<pathelement path="/opt/idea-IU-117.418/lib/junit-4.10.jar"/>
<pathelement path="out"/>
</path>

Try changing the filesetelement to this:
<fileset dir="tests" includes="**/*.java" />

Related

Using dirset with Ant + Jacoco

I'm trying to use jacoco to perform unit tests on files but it keeps popping with NoClassDefFoundError and keeps giving new class names every time I sort the previous error out. Till now I've been manually providing the pathelement location to the bin folders which contain the required .class files.
I'd like to fasten the process by using dirset if possible. I've compiled a dirset which contains the bin folders of all the main folders that are required.
Questions:
1. How do I include this dirset in the classpath such that jacoco looks for the .class files in these set of bin directories?
2. Is there any way in which I can echo the dirset to the cmd to check its contents?
3. Why does this not work?
<echo message="Build path is: ${toString:ALL.dirs}"/>
dirset:
<path id="ALL.dirs">
<dirset dir="${classes.dir}">
<include name="projects-framework/**/bin"/>
</dirset>
</path>
jacoco code coverage:
Code I've written till now. The "**" in the code are the full locations to the bin folders which I do not think is relevant here.
<target name="cov-test">
<mkdir dir="${report.dir}"/>
<jacoco:coverage>
<junit fork="true" forkmode="once" showoutput="true" printsummary="on" enabletestlistenerevents="true">
<test name="full.qualified.name.ABC"/>
<classpath>
<path refid="classpath"/>
<path refid="application"/>
<pathelement location="projects-framework/**/bin"/>
<pathelement location="projects-framework/**/bin"/>
<pathelement location="projects-framework/**/bin"/>
<pathelement location="projects-framework/**/bin"/>
</classpath>
<formatter type="plain"/>
<formatter type="plain" usefile="false" />
</junit>
</jacoco:coverage>
</target>

ant cannot identify my jar files at all

I have my jar files in Assignment2\lib folder and my build file is in Assignment2. The name of the jar file is Assignment1.jar The following is how I tried to compile my Assignment2 from build file through ant.
<project name="Assignment1" default="run" basedir=".">
<property name="classes" value="classes" />
<path id="project.class.path">
<pathelement location="src"/>
<pathelement path="${java.class.path}/"/>
<pathelement path="${additional.path}"/>
</path>
<path id="lib.jars">
<fileset dir="lib" includes="**/*.jar" />
</path>
......
<target name="compile" description="compaling java files with Assignment2">
<mkdir dir="build"/>
<javac srcdir="src" destdir="build" debug="on" failonerror="true">
<classpath refid="project.class.path"/>
<classpath refid="lib.jars"/>
</javac>
</target>
<Project/>
I am getting compile errors. I'm using windows. Is that the problem? Is there any way to compile?
Well,Everything looks good to me but, you can try the following to validate the same,
Check if the lib folder has all the necessary library jars required to compile you application
You can try assigning the relative path of the lib folder to a property and refer it in the classpath eg: Property lib value=".\Lib"
Try the following before you call the compile target to see the list of jars being included in the classpath
<pathconvert property="libjars" refid="lib.jars"/>
<echo>libjars is ${classpathProp}</echo>
hey guys thanx for the answers actually i found probelm. i was trying to use a jar file created by netbeans files. Apparently netbeans jar files can be used only by netbeans. at the end i made a new jar file of Assingment1 and used it. now everything is fine. Thanx again for the answers.

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

How to include external class files during build in ant

I have a package(with some outdated Java files) which i need to build using ant tool. I have some updated classes for which source is not available. So, I need to add the updated class to the jar during build and skip the outdated Java files from the build. Please let me know how to achieve this.
For example - I dont have the updated source java file for com.org.auth.ABC.Java, but i have the updated class file which i got from the another source ie com.org.auth.ABC.class. During build i need to point to the updated class(com.org.auth.ABC.class) for this class alone and create a new jar.
Please find how i am currently pointing to the class files below.
<target name="xxx.jar" depends="xjc,compile">
<jar destfile="${dist.dir}/xxx.jar">
<fileset dir="${classes.dir}" includes="**/*.class"/>
<fileset dir="${jaxb-classes.dir}" includes="**/*.class"/>
<fileset dir="${jaxb-source.dir}" includes="**/bgm.ser,**/jaxb.properties"/>
</jar>
</target>
If you want to leave some package from compilation you can use excludes attribute of fileset ant tag.
for example:
<fileset dir="src/">
<exclude name="**/dir_name_to_exclude/**" />
</fileset>
In order to include the specified class in compilation you can put the containing folder in your class-path using ant.
<path id="project.class.path">
<pathelement location="lib/"/>
<pathelement path="${java.class.path}/"/>
<pathelement path="path to your class file's base folder"/>
</path>
<javac srcdir="${src.dir}"
destdir="${classes.dir}"
classpathref="project.class.path">
... put source files here..
</javac>
And if you want to include that class-file in your jar then add it to fileset using include tag:
<fileset dir="classfiles">
<include name="your class file name"/>
</fileset>
Hope this helps

Ant-TestNG classpath issue: getResource can't find the file

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.

Categories