How to run junit tests at build time in netbeans and jenkins? - netbeans

I'm trying to set up a project to run junit tests at build time, so that every member of the team and the Jenkins build server runs the tests when it builds.
I believe we have set up a fairly standard webproject in Netbeans, but I can't seem to find anyone solving this problem on stackoverflow or google.
How would you go about doing this?

Go to file: /nbproject/build-impl.xml and fidn the dist target. It should look like this:
<target depends="init,compile,-pre-dist,do-dist,-post-dist" description="Build distribution (WAR)." name="dist"/>
Copy paste it into /build.xml, and add the "test" target into it:
<target depends="init,compile,test,-pre-dist,do-dist,-post-dist" description="Build distribution (WAR)." name="dist"/>
This was in Netbeans 7.3. It now builds and runs the tests on every build, also on the Jenkins build server.

Never modify your build-impl.xml! Netbeans regenerates this file when you perform any changes to your project.
The better approach would be to modify your build.xml and add a post-jar task:
<target name="-post-jar" depends="test"/>

Related

Unittest an entire netbeans application including dependent projects

Here is the situation:
I have a java application (netbeans project) that uses several java libraries (each of them is a netbeans project). I have an automated build script that builds the application using ant without the netbeans IDE. The command to build the application is
ant jar
That works fine, it recursively builds jars for all dependent libraries and finally the application jar.
Now, I want to run all my unit tests in the automated build. The default ant target depends on the targets jar, test, and javadoc, so I thought I could simply call
ant
as the build command. As before, all jars are built for the dependent libraries, but only the unit tests for the application project are executed (same for javadoc, btw.).
So, how do I get ant to run the unit tests for each dependent library project recursively?
Used versions are: netbeans 8, junit 4.
The only solution I found is to add a dependency into the build.xml used by ant for every library and the application project:
<target name="-pre-jar" depends="test" />
But that would mean to execute all tests on every build of a jar file. And since netbeans uses these build.xml files as well, every simple build would lead to executing all unit tests.
After some testing I think I've found a solution that works.
I had to add the following code into the build.xml of each netbeans project whose unit tests should be run (fyi: the build.xml is provided by netbeans for personal changes to the build process).
<target name="-pre-jar" depends="test-on-release" />
<target name="test-on-release" if="release.runtests">
<antcall target="test" />
</target>
Then, if I run ant on the commandline I have to define the property release.runtests to enable the execution of unittests:
ant -Drelease.runtests= jar
If I remove the definition of the property from the call, the target test-on-release is not executed and thus the tests are not run. The same happens when I use the netbeans IDE to build a project.
That said, it is a solution, not the solution, at least not the solution I had hoped for. It would be much more convenient if there was a way to run the test target recursively without having to edit all these build.xml files. Maybe that is not possible with the constraints given by the netbeans-generated build files.

False Error Message in Eclipse for Ant Include Task

In my ant build I am including a second ant build file (build.xml):
<project name="Including" default="echoC" basedir=".">
<include file="build.xml" as="Included"/>
<target name="echoC" depends="Included.echoB">
<echo>C</echo>
</target>
</project>
build.xml is in the same directory as the including build file and everything is working fine actually. I can run the target echoC and also the target echoB from the included build file is executed first.
What is irritating however is that Eclipse is displaying the error message Target Included.echoB does not exist in this project in the Ant Editor. Does anybody know how to get rid of that?
Thanks a lot for your support!
This seems a bug in Eclipse according to https://bugs.eclipse.org/bugs/show_bug.cgi?id=412809. The comments suggest there is no support for the task in the editor.
Using the import task instead works fine.

Eclipse execute Maven build with tests

In simple words: How to runs in eclipse tests with building process (as simple as possible)?
I want to execute test defined in pom with every maven build. Currently all is build, but none test is run. When I execute maven test it runs test, but during build it only builds.
How can I add this phase to eclipse build process?
I already read Eclipse Maven Build and Test with One Button, and it is close to optimal, still I would like to run tests with build to be more TDD.
My solution(might not be optimal):
require maven runpath
Go to Project -> Builders
Add new Program
In Edit configuration enter (as on picture):
Location: your/maven/path
arguments: test -f /yourProjectPomPath/pom.xml
As on picture tab Build Options -> Check(click) During auto builds option select
Save changes and check if all works

eclipse several ant build file

currently I'm developing in eclipse 3.5 in different project web applications for tomcat 6.0.24.
For each of this project I have written a ant build file to generate the war file to deploy the project to the tomcat container.
So I have to run for each project the ant build file (a lot of clicks, and a waste of time).
Question: Is there a possibility to run all needed ant build files with a single click, from a single project or whatever?
#thelost
thank you very much for your hint, you brought me on the right way,
I solved the problem:
<target name="build">
<ant antfile="jar-build.xml" target="GEN-JAR_FILES" />
<ant antfile="war-service01.xml" target="SVC01-WAR"/>
<ant antfile="war-service02.xml" target="SVC02-WAR"/>
</target>
best regards, Alex
Sure. E.g., you could create a master build file and use the Exec task.

Creating ANT script to deploy to glassfish, run junit tests and then un-deploy

Hey guys, I have a j2ee app which I am building with Netbeans. My task is to modify the build.xml so that after the app builds, ANT deploys the app to a server, runs Junit tasks on the app, and then un-deploys the app. So far I have the deploy and un-deploy working but I'm running into some trouble running the junit tasks.
I have a client project in Netbeans where my junit tasks lie. My trouble is that when this project is built, it doesn't compile my junit tests into the .jar. This causes problems when I run my ant junit tasks and ANT cannot find the appropriate .class files for the junit tests.
In the Netbeans Project Properties it allows me to set "Source Package Folders" and "Test Package Folders". If I add the "test" folder into the "Source Package Folders" and build the project it compiles the tests and includes them with the jar. This works, however it prevents me from running my junit tests as tests in netbeans which slows development.
Has anyone had any experience with solving such a problem? There may be a simple solution I am overlooking so if anyone has a word of advice I would appreciate it. Thanks in advance.
-Brad
If I understand what you're asking,
You shouldn't need to compile the test classes into the jar. Just compile them into some directory, say 'classes'. Then just include this directory in the fileset nested element for the junit task.
A simple example,
<target name="junit">
<junit printsummary="true">
<classpath>
<pathelement location="${classes.dir}"/>
</classpath>
<test name="test.class.TestClass"/>
</junit>
</target>
Had to point my junit task to the correct classpath. Was pointing to the exact directory of the .class files(project1/classes/com/blah/blah2/blah3) which is incorrect. Set classpath to project1/classes and it worked. Noob mistake.