简体   繁体   中英

How do I run JUnit from NetBeans?

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?

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.

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

All the above answers are correct, but if you are using in mac little change needed.

Step 1 Write your junit class.

Step 2 Right click on the class ->Tools-> Create/Updates Tests-> Select framework as Junit-> click ok.

Step 3 Right click on the file again ->Test File.

Now it will run as junit, will show the Test Result.

I had the same issue after imported a eclipse project into NetBeans.

To resolve it, I followed the above steps outlined by alangalloway, but instead of creating a new folder, I just pointed to the imported test folder.

Maybe in future release, NetBeans can automatically recognize imported test cases.

Thanks.

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.

Had a similar issue. In Netbeans 7.0.1, what worked for me was to locate the project.xml file (ie {project}/nbproject/project.xml) and change:

        <test-roots/>

to:

        <test-roots>
            <root id="src.dir"/>
        </test-roots>

(in my case the test files are in the same dir as the source dir)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM