Unittest an entire netbeans application including dependent projects - netbeans

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.

Related

Spock script not compiling automatically in Spring MVC project which is using Eclipse IDE

I am working on a Spring based web project (Eclipse as IDE), in which we want to introduce integration testing framework using Spock. This project is based on Maven. For this purpose I have installed Greclipse plugin and converted the existing project to groovy nature. Also configured Maven to run the test classes in src/test/groovy folder, and everything working fine and all the spock tests are running fine with run "Maven test" phase. Configured the build path to compile the groovy test files to target/test-classes.
The problem is coming when I run the Spock tests using Run -> Run configuration. It runs file with Junit runner using run configuration, it is picking up the compiled test class from target/test-classes. Whenever I modify the Spock test script file, it is not automatically generating the classes, Run with Run Configuration is always picking up the old compiled class.
How to force the script to compile so that I don't always need to Maven clean and Maven test, to force it compile and run.
I have found in some other threads talking about modifying the Groovy compiler options to "Enable script folder Support", both checking and unchecking is of no use either. Its not forcing the Spock script to compile (also tried enabling the same option in Eclipse Preferences global Groovy compiler option)
Any help is greatly appreciated.
Spock tests are implemented as a class extending Specification. So they are treated like any other Groovy class.
If in Eclipse under "Project" → "Build Automatically" is enabled, Eclipse compiles the classes automatically on every change. So if you change a Spock test in Eclipse, running it should always use the newest compiled version.
For automatic compilation to work correctly, the source has to be configured properly in Eclipse. Check via "Configure Build Path..." on your Eclipse project that under the "Source" tab the src/test/groovy/ folder is configured correctly.

Eclipse with m2e not building tests

I have an Eclipse project that uses Maven. The regular source files (ie, in src/main) build fine in both Eclipse and Maven. However, the test files (ie, in src/test) will only build in Maven. I cannot find any way to get them to build in Eclipse.
That is, the tests in Eclipse are run as the last version which Maven compiled. Before I used Maven, tests would be compiled automatically when they were run in Eclipse. "Build automatically" in the project menu is still enabled.
This only affects the tests (which are JUnit 4 tests). As mentioned, the regular source files are being built automatically correctly.
Here's the POM file, with irrelevant data pruned: https://gist.github.com/anonymous/10001049
Add the src/test directory to your build path. Windows / Preferences / Java / Build Path, or something like that.
Updating the project might also work, since m2e can get out of sync with your pom file quite often.

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

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"/>

Build NetBeans Ant Project from Gradle

I'm converting our build from Ant to Gradle. Our first step is to add the Gradle build files so we can start using Gradle as our build tool. This lets us use our existing build scripts to build using Ant and convert them to Gradle over time. I want to simply have Gradle call the existing Ant build files. Our projects are all NetBeans projects, which have build.xml and nbproject/build-impl.xml files. Some of the projects require NetBeans build properties, which can be found at ~/.netbeans/6.5.1/build.properties. I have the NetBeans user.properties.file property successfully set to ~/.netbeans/6.5.1/build.properties.
When I build with Ant, I invoke this:
ant -Duser.properties.file=~/.netbeans/6.5.1/build.properties dist
This executes the dist target, which depends on the init target which depends on the targets listed below:
pre-init, init-private, init-userdir, init-user, init-project, do-init, post-init, init-check, -init-taskdefs
The targets listed above are executed in the order specified. When I invoke 'gradle dist', it invokes the init Ant target, but then it executes the targets listed above in reverse order, starting with -init-taskdefs. There are required properties which are setup in the targets before the -init-taskdefs target which aren't being setup when run from gradle.
Really, all I want to do right now is to use gradle to invoke Ant to build my projects. What's the best way to do this since using gradle to build using Ant build.xml files doesn't seem to work as expected? Do I have to resort to using exec? (I hope not).
I found trying to use Gradle's integration of Ant with a Netbeans project was too difficult and error prone. Instead, I use Gradle's exec() command. Below is an example, lifted from my code that builds a NetBeans Project named 'Common Library'.
task commonLibrary {
doLast {
ant.echo("Building Common Library")
exec () {
workingDir = "../netbeans/nb/CommonLibrary"
executable = "ant"
args = ['clean', 'jar']
}
}
}
I realize that wasn't the answer you were hoping for, but posted it as a possible solution for other people, particular people who aren't in a position to start reworking their build.xml files.

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.