Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run JUnit from NetBeans?

Tags:

I've been trying to understand how to start writing and running JUnit tests.

When I'm reading this article:

http://junit.sourceforge.net/doc/testinfected/testing.htm

I get the the middle of the page and they write, "JUnit comes with a graphical interface to run tests. Type the name of your test class in the field at the top of the window. Press the Run button."

I don't know how to launch this program. I don't even know which package it is in, or how you run a library class from an IDE.

Being stuck, I tried this NetBeans tutorial:

http://www.netbeans.org/kb/docs/java/junit-intro.html

It seemed to be going OK, but then I noticed that the menu options for this tutorial for testing a Java Class Library are different from those for a regular Java application, or for a Java Web App. So the instructions in this tutorial don't apply generally.

I'm using NetBeans 6.7, and I've imported JUnit 4.5 into the libraries folder. What would be the normal way to run JUnit, after having written the tests?

The JUnit FAQ describes the process from the Console, and I'm willing to do that if that is what is typical, but given all that I can do inside netbeans, it seems hard to believe that there isn't an easier way.

Thanks much.

EDIT: If I right-click on the project and select "Test" the output is:

init: deps-jar: compile: compile-test: test-report: test: BUILD SUCCESSFUL (total time: 0 seconds) 

This doesn't strike me as the desired output of a test, especially since this doesn't change whether the test condition is true or not.

Any ideas?

like image 478
Eric Wilson Avatar asked Sep 04 '09 20:09

Eric Wilson


People also ask

Does NetBeans support JUnit 5?

Junit 5 is, architecturally, so different to previous versions, that it should really be called Jupiter, instead. However, this is not a problem when you're using Maven, since Maven resolves all transitive dependencies for you, and is supported in that way from Apache NetBeans 10 onwards.

How do I run a Java program in JUnit?

To run JUnit 5 tests from Java code, we'll set up an instance of LauncherDiscoveryRequest. It uses a builder class where we must set package selectors and testing class name filters, to get all test classes that we want to run.


2 Answers

One way is to right click on your project in the Projects pane and select "Tests". That will run the JUnit tests. You can also right click on the test file and select "Run Test" and that single file will be ran. The keyboard shortcuts depends on how you have your keymapping set, but you'll see them in the context menus.

You can also have NetBeans autogenerate tests for you by right clicking your source file and then "Tools > Create JUnit Tests".

like image 101
NA. Avatar answered Sep 20 '22 15:09

NA.


Re-importing does not appear to be necessary. I had the same issue (imported project, right clicking did not bring up any JUnit test options). I took these steps, which resolved it, using NetBeans 6.8:

  1. Add a folder called "tests" to your project.
  2. Right-click your project and select Properties.
  3. Select Sources.
  4. Under Test Package Folders, click the Add Folder button, and select the "tests" folder.
  5. Right clicking a file + Tools > Create JUnit Tests.
  6. Once a test is created, right-clicking a file + Test File runs the test.
like image 28
alangalloway Avatar answered Sep 23 '22 15:09

alangalloway