How do I run JUnit from NetBeans?

前端 未结 6 1238
臣服心动
臣服心动 2020-12-30 02:41

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

相关标签:
6条回答
  • 2020-12-30 02:49

    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.

    0 讨论(0)
  • 2020-12-30 02:59

    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.

    0 讨论(0)
  • 2020-12-30 03:02

    Had a similar issue. In Netbeans 7.0.1, what worked for me was to locate the project.xml file (i.e. {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)

    0 讨论(0)
  • 2020-12-30 03:09

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

    0 讨论(0)
  • 2020-12-30 03:10

    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.

    0 讨论(0)
  • 2020-12-30 03:13

    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.
    0 讨论(0)
提交回复
热议问题