Automatically run junit tests after build in NetBeans 8.0.1 - java

I have a simple question. Is there a way to configure NetBeans so that after build JUnit tests in test package would run automatically ? I'm using NetBeans 8.0.1.
Thanks in advance.

If you are using Maven there's an option under Tools / Options / Java / Maven / Execution / Skip Tests for any build executions not directly related to testing. Make sure that this one is unchecked.
If you are using Ant this is also possible. It's been some years since I used Ant but from top of my head you need to modify your build.xml. There needs to be a target like "compile" and there you add in the depends-property the targetname for the unit tests.

For a project specific solution:
Right-click on your project -> Properties -> Actions.
Select "Build project" ("Debug project" and "Run project" work too!"
Add "surefire:test " at the beginning of the Execute Goals.
If you now share your source code with others, they will automatically run your tests too!

Related

IntelliJ IDEA runs full test suite when I try to debug a single unit test

I've inherited a Maven project that includes a large number of unit tests and integration tests. I added a unit test for a change I've made. However, this unit test is failing and so are a number of integration tests (because they rely on a specific environment). I want to debug my unit test (and the method under test), but when I right-click on the test method and choose "debug", IntelliJ IDEA does a "Build" first, and that's the Maven build which tries to execute all tests and integration tests. As a result, the build fails and I can't therefore debug my unit test. How can I get around this?
Intellij has specific configuration for the builds.
The default configuration is the "build" command which will build the project.
You can change this command into a maven command so you will manually skip the tests
to do that click edit configuration and go to the profile configuration you want to change. :
At the very bottom you can see the before launch part :
from there you remove the previous with the ( - ) and you click the ( + ) to add a new one. Scroll down in the list to find the maven goal
once you check it you have to add the manual command there. Note: You dont need to write mvn as its ommited.
An other approach you can follow is to instruct intellij to use maven for all of its actions by delegating everything to maven.
to do that just go to the settings and follow the path :
Build,Execution,Deployment -> Build Tools > Maven > Runner
and check the Delegate IDE build/run actions to Maven.

Running JUnit tests together with Java project

I'm writing code in Intellij and have a JUnit test class included in a project and i understand that running of JUnit should always be done at build time.
Is there a way to first run the JUnit and only if there were no test error run the project itself ? I want them to run together with 1 click (NOT run them seperately/manually).
Also, i would like the above to work even when the project is packed as a .jar file.
How can it be done ?
Thanks !
In Intellij:
Run -> Edit Configurations
Create a JUnit configuration for your tests
Create a Run configuration for your project.
And on "Before Launch": Add -> Run Another Configuration and choose the one created at point 1.
It doesn't matter how your project is packed (jar, ...)
Normally this is done by using a build management tool like: maven, gradle, ant. In this way the build tool will run tests for you and stop if they fail.
With maven, it's just a command: mvn clean package exec:java which will compile code, build project, run tests and execute your code.
See example project: https://github.com/HaveACupOfJava/maven-jar-demo

Creating a Site Using Eclipse Maven Plugin

I would like to create a 'site' containing test reports for my Maven Java project. However, all the online resources that I found were talking about the command 'mvn site'. I do work on Windows, though, therefore I would like to avoid any interaction with its console.
Can I do it using solely the Eclipse Maven Plugin? If I want to tell Maven to build my project, I can right-click it and then choose 'Run As' 'Maven Install'. Is there something similar for generating sites?
Thank you all in advance!
Why the hate for specifically the Windows command prompt? What has it done to you to deserve a cold shoulder?
In any case and more seriously: Eclipse allows you to define run configurations yourself. run -> run configurations. Under "maven build" you can add a new configuration to do a mvn site. This page gives an idea by demonstrating how to do a clean install:
http://books.sonatype.com/m2eclipse-book/reference/running-sect-running-maven-builds.html

Missing "Run as JUnit Test"

I created a JUnit 4 test in Eclipse by right-clicking on a Java class and selecting New JUnit Test Case. When I right-click the test class I get "Run on Server", but not "Run as JUnit Test". I am using Eclipse 3.6.1.
In my case, Eclipse must have reached a corrupt state. Restarting Eclipse fixed the problem.
I think I see the problem. You need to have an actual test in the file before Eclipse identifies it as a test case. Try inserting the following:
#Test
public void foo() {
}
for junit5 jdk8 is needed in build path and project facets.
Make sure your class has JUnit traits (extends from TestCase, or use #Test etc);
Right-click "Run As" -> "Run Conciguration" -> Create JUnit test from left icon "JUnit" anyway;
check whether you have jave 8 or above and configured correct jdk in build path.
check at least #Test is used in your test case class file
Then go to run configuration->select Junit->right click and new configuration->browse the package->select junit5 as test runner.
In case you experience it in maven project add following into pom.xml and reload project using Alt+F5. Tests do not work because old JDK version.
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Make sure to have a valid default constructor for your test class.
In my case, the problem was different. I was converting a TestNG test to JUnit. The #Test annotation was satisfied by the TestNG import, but that was the wrong annotation. I removed the TestNG import and added the JUnit import for #Test, and the right-click menu option to run as a JUnit test appeared.
The eclipse shortcut to run Junit test is Alt+Shift+X, T.
If its not working just press Alt+shift+X a menu will popup just look for Junit.
I ran into these symptoms when importing an existing project into a Kepler-based Eclipse IDE for Java Developers version.
Importing the project into a Luna-based Eclipse IDE for Java EE Developers correctly set set it to a Java project (project icon now includes that little J) and now allows running JUnit tests.
In my case the Java Build path (.classpath file) was corrupt. In particular it had a merge conflict which was unresolved. Therefore, the JUnit 4 library was missing.
I used to have a similar problem and it turned out that was because I forgot "extends Specification" after the "ClassToBeTestedSpec" in the declaration.
Make sure that you include import org.junit.Test; and the tests have #Test before them, I have missed that before when coping some functions from other files.
To solve the situation, a restart of eclipse wasn´t enough in my case, I had to remove and re-add the project to my workspace as well.
If, the configuration is of SpringRunner.class for Junit.
Make sure your maven clean install with maven update and refresh is run in your project with right jdk and mavens password/configuration.
This recurred for me, this time it was that Junit simply wasn't added to the classpath (but it has in the past been). This is the first Eclipse project for me to use Junit 5. Maybe that has something to do with it? I know this is a very old post, but I think this might be needed for more recent reasons.
Right-click your project and choose 'properties'
choose Build Path -> Configure Build Path
Click on Libraries tab.
Click on Add Library button.
Click on JUnit and Next button.
Choose JUnit version. (for me, Junit 5)
Click Finish.
Info From
https://www.eclipse.org/forums/index.php/t/304679/

Most effective way to go from Eclipse Project to Hudson Build

We want to use Hudson/Jenkins to build our project which is currently realized entirely in Eclipse. From what I can tell, there are various ways to go from A to B, or E to H, as it were: export as Ant script, export as Maven script, export as Runnable JAR while creating an Ant script for that, etc.
All of the above seem to have in common that between "This runs in Eclipse" and "Hudson produces something that runs" there are multiple steps which are independent, for example, you can change your project, commit to SVN and trigger a Hudson build, but unless you specifically remember to "Export as Ant Script" in between, it will fail.
Is there a "one in all" solution ? I'm not worried about the amount of clicks, but instead about the various steps in between that, to make matters worse, are only needed sometimes. In short: I am looking for something that goes from "I can click on the 'Run' button and it works" to "Hudson produces something that works" without every developer having to remember every optional step in between.
Ideas ?
Edit: All of the answers so far seem to suffer from the same issue: it's all parallel development. You have your Eclipse Run Configuration, and you have Maven/Ant/Whatever build. If you change your run config, you have to then remember later to change your Maven/Ant/Whatever build, commit it, and then HOPE that all other developers notice the change to the Maven/Ant/Whatever build during their daily SVN Update, manually open the file, inspect the changes and then duplicate those changes in their own run configs. That seems like it's just begging for bugs and mistakes, isn't there anything that's properly integrated with the Eclipse Run Configurations ?
Hudson can build Maven or Ant projects, so the first step is to get a reproducible build with either tool, which you only need to set up one time. Then you need to take that pom.xml or build.xml file and actually commit it to Subversion. This is necessary since Hudson won't open Eclipse and will instead use the command-line to execute a build.
Then you can setup a new Hudson job that will watch Subversion for any changes. Your developers can use their normal workflow, where they use Eclipse to do builds and commit changes to source control when they're ready. Hudson will see it and pull down a fresh copy of the code base, and then will do its own compile and will report back any problems.
Personally I prefer Maven2, since I know Hudson has solid integration with it and will do things like run your JUnit tests. Eclipse used to be painful with Maven, but now there's the m2eclipse plugin.
I'd try http://www.ant4eclipse.org/.
It allows you to build your eclipse project from an ant file. From the first paragraph here: http://www.ant4eclipse.org/node/6 it sounds very much like what you want. With ant4eclipse ant will access your eclipse project and then it should be able to build through Hudson.
The aim of the ant4eclipse project is to avoid (or at least: to reduce) the redundancy of Eclipse and Ant configurations. More precisly: it consists of Ant tasks that are able to read and work with some of Eclipse's configuration files.
Migrating to Maven, Hudson has great first class intergration with Maven.
Maven 3 + Archiva makes a very potent build system. Of course there are other Repository Managers but Archiva does just enough for what I need.
Once you get Maven, you really wonder how you did without it up until then. A dedicated private Repository Manager helps this greatly, that is why Archiva is important to the mix.

Categories

Resources