Trouble Running simple JUnit Test through Ant on eclipse - java

I have the following JUnit test in eclipse:
package test;
import org.junit.Test;
public class SimpleJUnitTest
{
#Test
public void doTest() { System.out.println("Test did run"); }
}
And the following build.xml in the same folder:
<?xml version="1.0" encoding="UTF-8"?>
<project name="LoggerTest" default="JUnitTest" basedir=".">
<target name="JUnitTest">
<junit>
<classpath location="../../lib/junit.jar" />
<test name="test.SimpleJUnitTest" />
</junit>
<echo>boo</echo>
</target>
</project>
If I run the test class under "Run As..." and choose JUnit, it runs without error. If I run the build.xml under "Run As..." and choose Ant Build, I get the following output:
Buildfile: C:\Users\995868\workspace\JUnit1\tst\test\build.xml
JUnitTest:
[junit] Test test.SimpleJUnitTest FAILED
[echo] boo BUILD SUCCESSFUL Total time: 390 milliseconds
If I remove the classpath attribute under JUnit, I get a different error message about needing the junit jar on the classpath, so I think JUnit is getting invoked. I just don't understand what its error is here. I've tried putting static block code in the class to do a System.out.println() when the class is loaded, and it does not appear, so there seems to be something I'm doing wrong in the configuration.
Can someone please tell me what's wrong here?
EDIT:
directory structure:
JUnit1
--bin
--test
--SimpleJUnitTest
--lib
--junit.jar
--scripts
--build.xml
--src
--tst
--test
--SimpleJUnitTest.java
I also copied build.xml to tst and ran it from the command line from that directory, same result.
I've copied junit.jar to %ant_home%\lib with no effect, though when I took the pathelement line out of the classpath I got the message "The for must include junit.jar if not in Ant's own classpath". I'm not sure where "Ant's own classpath" is specified. The classpath block with the new error message is this:
<classpath>
<pathelement location="c:/users/995868/apache-ant-1.9.4/lib" />
<pathelement location="../bin" />
</classpath>
I'm not using hamcrest features anywhere, so I haven't looked it up and put it in. I was trying to make a simple example, and the documentation for junit under ant (at least) does not mention that hamcrest is necessary.

I suppose you are missing hamcrest-core.jar from your ant classpath.
EDIT: and you are also missing the test file class itself from your classpath. So you need to update your classpath in the following manner:
<classpath>
<pathelement location="bin"/> <!--here is your test class-->
<pathelement location="lib/hamcrest-core-1.3.jar"/>
<pathelement location="lib/junit-4.12.jar"/>
</classpath>

Your class does not contain any testcases, which is an error. Your #Test annotation will be ignored, as you have not specified an #RunWith that will actually use it, so JUnit searches for methods named "test...".
So you have two choices:
a) Rename your method to "testSomething" or similar.
b) Add #RunWith(BlockJUnit4ClassRunner.class) above your class definition
Both ways will ensure that at least one test exists in your class, either via naming convention (a) or via annotion (b).

Related

Exclude particular test execution with Ant

Executing apache cassandra tests using ant test command. Need to skip execution of few tests like org.apache.cassandra.audit.AuditLoggerTest , org.apache.cassandra.fql.FullQueryLoggerTest etc.
Tried excluding testfiles in https://github.com/apache/cassandra/blob/trunk/build.xml under <target name="_build-test"> however it didn't worked.
<exclude name="org/apache/cassandra/audit/AuditLoggerTest.java"/>
<exclude name="${test.unit.src}/org/apache/cassandra/audit/AuditLoggerTest.java"/>
#Ignore annotation for test classes works however I am looking for solution with build.xml

Running ant-target to send a mail via java

I´m a pretty new to ant and I´m trying to send an email within an ant-target which is called by Java. I´m using the Netbeans IDE.
Ant:
<project name="AntTargets" default="main" basedir=".">
<description>Builds, tests, and runs the project AntTargets.</description>
<import file="nbproject/build-impl.xml"/>
<target name="run" depends="jar">
<java classpathref="AntTargets.classpath" fork="true" classname="Client" />
</target>
<target name="main">
<echo message="******************[MAIN]******************"/>
<property file="nbproject/build.properties"/>
<property name="to" value="${builder}" />
<property name="from" value="${builder}" />
<property name="server" value="${server}" />
<property name="port" value="${port}" />
<mail mailhost="${server}" mailport="${port}" subject="Test build">
<from address="${from}"/>
<replyto address="${to}"/>
<to address="test#domain.de"/>
<message>The build has been completed</message>
</mail>
<echo message="Email to ${to} from ${from} has been sent using the server ${server} on port ${port}."/>
</target>
</project>
Java:
import java.io.File;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
public class AntTargets {
public static void main(String[] args) {
//get new file
File buildFile = new File("build.xml");
//create new project
Project p = new Project();
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
p.init();
//initialize helper
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
//parse and execute
helper.parse(p, buildFile);
p.executeTarget(p.getDefaultTarget());
}
}
When I execute the ant part directly, the properties file is found, read and works overall but the mail-part gives me this error:
Failed to send email: javax.mail.internet.MimeMessage
build.xml:16:
java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage
When I call it with java I get:
Reference AntTargets.classpath not found.
BUILD FAILED (total time: 0 seconds)
I would greatly appreciate any help and tip overall.
The issue is Ant doesn't know what AntTargets.classpath is.
You must define a classpath variable and give it id="AntTargets.classpath. In this path variable you should define the physical locations of the libraries you use. (You should look to creating path variables to reduce redundancies in code, particularly when you want to use the same path in different ant targets.)
Here's an example: Let's say that your project requires the Gson library at runtime.
First, create a path for that library. Let's assume you have your libraries in a lib directory.
<path id="library.gson-2.8.0.classpath">
<pathelement location="${basedir}/lib/gson-2.8.0.jar"/>
</path>
Do the same for other dependencies as well.
Next, create a path that will contain all these dependencies, so you can refer to all the dependencies as one variable. This will be your AntTargets.classpath
<path id="AntTargets.classpath">
<path refid="library.gson-2.8.0.classpath"/>
...... <!-- Add paths for your other dependencies by refid here-->
</path>
Now, when you use AntTargets.classpath Ant will know what that is and use it to run your jar.
I notice that you use this in the run target, so if you're trying to run the Client class from your own jar, include that in your classpath as well.
Also keep in mind that "Client" may not be enough information--you must provide the fully qualifying name of the class, including package names, e.g. something like "com.package.Client".
EDIT:
I just noticed the exception you're getting from running directly with Ant. You need to include the javax.mail library in this path variable we create. That should resolve that issue.
EDIT 2:
I seem to have misunderstood. I thought that your ant task was trying to call your Java program which would send the mail, but it couldn't since it didn't know where the javax.mail.jar is, and so I instructed you to add it to the classpath.
But what you want to do is:
Use the Ant mail task to send a mail.
And in the Java you want to read the build file and execute the target that sends the mail.
Here's some information on that: mail is an Ant Task that requires the javax.mail library, if you want to use certain arguments. Looking at a few questions like this and this, I see that you should put ${ANT_HOME}/lib, i.e. where your ant.jar is, so that Ant's classloader can pick it up and use it. In that case, if my assumption about what you want is correct, then you should be able to get rid of AntTargets.classpath, because really, your build file doesn't need to know how you call any of it's targets.

Resource bundle in external library not found when running JUnit in via Ant

I have the following scenario:
JUnit tests against some test classes that test app classes. The app classes rely on a library with some resource bundles inside them. The library itself is included in classpath for tests with
<classpath>
<!-- all modules -->
<path refid="lib.app_modules.path" />
...
Where
<path id="lib.app_modules.path">
<path location="${myLib}" />
...
Effect: All classes in the library are found by app classes BUT not the resource bundles along them (loaded from inside the library).
The "solution" (or workaround):
Change:
<path id="lib.app_modules.path">
<path location="${myLib}" />
...
to:
<path id="lib.app_modules.path">
<pathelement path="${myLib}"/>
and everything runs fine. Somehow path beneath path seems to include only classes whereas pathelement seems to include everything inside the jar...

Find cause of ant failure

I have some ant files that one imports the other. Specifically build.xml imports a project_default.xml. When I try to do a build I get following error:
Buildfile: C:\myproject\build.xml [taskdef] Could not load
definitions from resource net/sf/antcontrib/antcontrib.properties. It
could not be found.
BUILD FAILED C:\myproject\build.xml:14: The following error occurred
while executing this line: C:\myproject\project_default.xml:17:
Problem: failed to create task or type if Cause: The name is
undefined. Action: Check the spelling. Action: Check that any
custom tasks/types have been declared. Action: Check that any
/ declarations have taken place.
Line 17 that is reported is the following:
<if>
<contains string="${env.PROJECT_SELECTION}" substring="env.PROJECT_SELECTION" />
I also added the following in the build.xml:
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="D:\UserData\ant-contrib-0.6-bin\lib\ant-contrib-0.6.jar"/>
</classpath>
</taskdef>
But got the same error. Any idea of what is the problem here?
with ant version > 1.6 you should use "net/sf/antcontrib/antlib.xml" instead of "net/sf/antcontrib/antcontrib.properties"
so change the taskdef to this:
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="D:\UserData\ant-contrib-0.6-bin\lib\ant-contrib-0.6.jar"/>
</classpath>
</taskdef>
cheers,
The first thing to check is that you have the ant-contrib jar path corrext. Run ant with -verbose to get more info about what it's doing and what tasks it's trying to load from where.
Where did you add the taskdef? If you're using the <if> task outside a target then you need the taskdef before it, also outside a target. If you're using it inside a target then the taskdef can be (textually) after the call as long as it is either outside a target or in a target that runs before the <if> in dependency order.

Filtering sources before compilation in NetBeans & Ant

I need to filter java files before compilation, leaving the original sources unchanged and compiling from filtered ones (basically, I need to set build date and such).
I'm using NetBeans with its great Ant build-files.
So, one day I discovered the need to pre-process my source files before compilation, and ran into a big problem. No, I did not run to SO at once, I did some research, but failed. So, here comes my sad story...
I found the "filter" option of "copy" task, overrided macrodef "j2seproject3:javac" in build-impl.xml file and added filter in the middle of it. I got the desired result, yes, but now my tests are not working, since they use that macrodef too.
Next, I tired to overriding "-do-compile" target, copying&filtering files to directory build/temp-src, and passing an argument of new source directory to "j2seproject3:javac":
<target depends="init,deps-jar,-pre-pre-compile,-pre-compile, -copy-persistence-xml,
-compile-depend,-prepare-sources"
if="have.sources" name="-do-compile">
<j2seproject3:javac gensrcdir="${build.generated.sources.dir}" srcdir="build/temp-src"/>
<copy todir="${build.classes.dir}">
<fileset dir="${src.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
</copy>
</target>
And now Ant says to me, that macrodef in question does not exist!
The prefix "j2seproject3" for element "j2seproject3:javac" is not bound.
That's strange, since build-impl.xml contains that macrodef, and build-impl.xml is imported into main build file.
And, by the way, I cannot edit build-impl.xml directly, since NetBeans rewrites it on every other build.
So, my question is: how can I automatically filter sources before compiling in NetBeans, and do not break the build process?
Looking at the default build.xml, it contains a comment that reads (in part):
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
So, to inject some pre-compile processing, you would provide a definition for -pre-compile.
FWIW, the error you got is because the j2seprojectX prefix is defined on the project tag of build-impl.xml, and the code in build.xml is outside of that tag.
Since I found an answer, and because it seems that nobody knows the answer, I'll post my solution.
build.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Needed to add xmlns:j2seproject3 attribute, to be able to reference build-impl.xml macrodefs -->
<project name="Parrot" default="default" basedir="." xmlns:j2seproject3="http://www.netbeans.org/ns/j2se-project/3">
<import file="nbproject/build-impl.xml"/>
<target depends="init,deps-jar,-pre-pre-compile,-pre-compile, -copy-persistence-xml, -compile-depend,-prepare-sources" if="have.sources" name="-do-compile">
<j2seproject3:javac gensrcdir="${build.generated.sources.dir}" srcdir="build/temp-src"/>
<copy todir="${build.classes.dir}">
<fileset dir="${src.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
</copy>
</target>
<!-- target to alter sources before compilation, you can add any preprocessing actions here -->
<target name="-filter-sources" description="Filters sources to temp-src, setting the build date">
<delete dir="build/temp-src" />
<mkdir dir="build/temp-src" />
<tstamp>
<format property="build.time" pattern="yyyy-MM-dd HH:mm:ss"/>
</tstamp>
<filter token="build-time" value="${build.time}" />
<copy todir="build/temp-src" filtering="true">
<fileset dir="src">
<filename name="**/*.java" />
</fileset>
</copy>
</target>
</project>

Categories