views:

3024

answers:

3

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?

+2  A: 

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

NA
Hmm . . . Something is wrong. If I right click on a source file and select tools, there is no JUnit option. If I right click on a test file, "Run Test" is not an option. If I right click on the project and select "Test" it indicates that it is running a test, but gives no meaningful information. I'll edit the question to specify this behavior.
FarmBoy
+1 for the comment about being able to right click on a test file and run it that way. When I'm writing new test cases, I don't want to re-run my whole test suite every time (e.g. for the unrelated classes I'm not changing)
I82Much
+2  A: 

Even though I've accepted an answer, I thought I should mention my difficulty, as someone else may encounter it.

When importing a project from existing sources into NetBeans, if you do not specify a folder for test packages, then NetBeans will not offer the JUnit options on the tools menu.

The only solution I found was to re-import the project. While primitive, it worked.

FarmBoy
+4  A: 

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.
alangalloway