How to run the test classes packaged as Jar file, using TestNG - ant

I have created a REST API automation frame work using TestNG and ANT. Below is my project structure.
1) 'lib' folder (contains all the external JARs including TestNG.jar)
2) 'bin' (contains individual class files)
3) 'build.xml' file (contains the ANT script for running the testing using TestNG)
4) 'testng.xml' file (contains the class name to be run, referring to the 'bin' folder)
I currently run the above setup without any issues, But I wanted to JAR the class files in the 'bin' folder and run it.
I created a JAR using all the class files in the 'bin' folder and replaced the class files with the newly created JAR. But this gives me "class not found in the classpath" exception.
the only change I did was I created a JAR using all the class files in the 'bin' folder and used it instead of the class files.
I could not figure out how to call the class file present in the JAR using my 'testng.xml' file.
below is my tesng.xml file structure,
<suite name="StockFunctional-Story77" parallel="none">
<test name="Test">
<classes>
<class name="pack01.TestStock01"/>
</classes>
</test>
</suite> <!-- Suite -->
where 'pack01' is my package and 'TestStock01' is my test class. And they are part of the JAR file.

Related

how to Include a folder and files in META-INF folder when creating the jar using ant task

I need a way to include a folders and files inside the META-INF directory when creating a jar using ant task.
Currrently I am using <metainf> tag , which is only including the files but not the folders.
Can anyone please help me.
Add new folder TEMP-INF under src\main\java
Put all the files and folders, which you wish to include in your JAR's META-INF into this newly created TEMP-INF
Now open build.xml file and search for
<jar> </jar> tag
Add <metainf dir="src/main/java/TEMP-INF"/> into <jar></jar>
And its done.

executable jar from groovy sources

I could not find a proper answer to the following question : while creating an executable jar from groovy compiled sources, I cannot simply include groovy-all-version.jar in a lib directory (as I would do in java) but I need to extract all the classes from the jar and package them with my own classes as otherwise I get an exception. This actually is the case with all libraries I may want to use.
Not clear ? Let's see an example.
toto.groovy belonging to package titi is compiled in a build directory which looks like this :
build
lib
groovy-all-1.8.4.jar
titi
toto.class
and a MANIFEST.MF that will be included in the jar contains the lines:
Main-Class: titi.toto
Class-Path: lib/groovy-all-1.8.4.jar
Executable jar creation is done with the following ant target in build.xml:
<jar destfile="${build.exe.dir}/${exe.name}"
basedir="${build.dir}"
manifest="${src.conf.dir}/MANIFEST.MF"
/>
Now if I try to execute the jar, I get
$ java -jar target/toto.jar
Exception in thread "main" java.lang.NoClassDefFoundError: groovy/lang/Script
(I've checked that the groovy.lang.Script class exists.)
On the other hand, if I unjar groovy-all-version.jar in build by adding an unjar task before the jar, execution goes fine. OK, it works, but it slows down the compilation process and I'd like to understand...
Any feedback is highly appreciated.

Ant cannot create task name is undefined

I'm using Ant with eclipse and everything was working fine, until I decided to do some house keeping and created 2 sub directories under my Ant dir.
I moved my build.xml to the sub directory and now nothing is working an I get:
BUILD FAILED C:\Users\OdedHarniv\Workspaces\Force.com
IDE\vidmind\ANTs\Vid Service\build.xml:26: Problem: failed to create
task or type antlib:com.salesforce:retrieve Cause: The name is
undefined.
Any idea what am I doing wrong?
I see two possible problems
1 You have a relative path defined somewhere in your ant file, or a file that you are importing. Note that if you are importing e.g. a properties file, relative paths in that one will be interpreted relative to the directory containing the main ant file.
2 If you are running through Eclipse, it will run through a run-configuration. That might have extra jars put on its class path. I expect Eclipse will create a new run configurations when you move the file, which has the default class path. Too see your run configuration go to Run > External Tools > External Tools....

How should the log4j.properties put inside the war file using ANT build tool?

I have a log4j.properties that sit right under the src folder. When I building my war file using ANT tool, the particular properties file wasn't pack inside WEB-INF/classes folder, and it was right under the "root" directory of the war file (if you unwar it). I heard from my colleague mention that this is not correct. May I know is this true? If no, how should I correct it?
THanks #!
Yes, log4j.properties (or any other resource loaded from the classpath) should be at the root of your Java source folder (src/main/java, build/main, src, JavaSource, or however you have your project configured).
In the WAR, it should be under WEB-INF/classes, not at the root (as if you unzipped it).
If this is not the case, being able to see your ANT build file would be very helpful. You should have something like this line configured within your war task:
<classes dir="build/main"/>
As long as you have a build/main/log4j.properties in your Ant basedir, this should work as you're expecting.

Is there a customizable class loading order using ant junit task?

in our web-app project, we include some jar files. For patching some issues of one of the classes in a jar file, we changed the implemention of this class in a patches source folder.
Since there is a defined class loading order in tomcat (WEB-INF/classes before WEB-INF/lib), the patched version of the class is loaded by tomcat, not the original one in the jar file. So, as soon as we deploy our application, everything works as expected.
Now, we want to run junit tests from ant against this patched class. So we configure the class path to hold both, the original jar and the patched class file. But there seems to be no way to tell the ant's junit task to first load the patched class, not the unpatched version from the jar file.
Is there a way to get around the problem? Is there a way to determine the order in which the classes are loaded by ant's junit task? Is there any other way to test our patched class from ant?
I think an ant classpath works just like the standard Java classpath. The classpath is searched in the order that paths are declared and a class is loaded from the first path where it is found.
Your classpath element for your junit task should be something like:
<classpath>
<pathelement location="${patched.class.folder}"/>
<pathelement location="${original.class.jar}"/>
</classpath>

Resources