Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netbeans and creating JUnit tests

Tags:

junit

netbeans

I am using Netbeans 7.0.1 with a web project that I have imported from existing sources. I have added the JUnit library to my project. In Netbeans tutorials online for version < 7 it says to create a JUnit test for a given existing class by righ-clicking on the source file in the project, select the menu "Tools" and then there should be an option to create a JUnit test. However this option does not appear for me. There seem to bean old bug report/request for functionality, describing this but it does not seem to have been resolved as it is mentioned as late as 2011-05-09 on the Netbeans bugzilla list (link Related on bugzilla). On the bugzilla it is suggested to explicitly create the folder /test/unit/src in the project, but still after this when I try to create JUnit test for an existing class from rightclick project -> new -> other -> JUnit, I get "no tests root folder was found in the selected project" (I tried changing the foldername to tests as well). Can I explicitly set this location in some configuration file and is there any way of getting the expected functionality allowing me to simply righclick a source file and select "JUnit test" to create a JUnit stub for the selected class ?

like image 882
Lin A Avatar asked Oct 14 '11 13:10

Lin A


People also ask

Does NetBeans come with JUnit?

Netbeans already includes the junit classes. So there is nothing to install.

How do I create a test package in NetBeans?

Edit project properties: right click on the project, this time select "Properties" Under "Project Properties" select category "Sources" Notice the section "Test Package Folders" --> click "Add Folder"

Does NetBeans support JUnit 5?

JUnit 5.3. 1 has been added as a new Library to NetBeans, so users can easily add it to their Java projects. JUnit 5 is now the default JUnit version for Maven projects without any existing tests.


2 Answers

If you right click on the project and pick "Properties" you should see "Sources" on the left. Select that and then click "Add Folder..." under the "Test Package Folders" section. This will add a new test directory. If you have an existing one, delete it first (just make sure the test sources are saved somewhere else) and then add the new one.

like image 140
mamboking Avatar answered Oct 13 '22 22:10

mamboking


If you right click on your project and add new Unit Tests-> JUnit Test it will automatically create the path for you.

like image 31
Eduardo Dennis Avatar answered Oct 14 '22 00:10

Eduardo Dennis