This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 175201 - Creation of JUnit Test From Blank src class files
Summary: Creation of JUnit Test From Blank src class files
Alias: None
Product: java
Classification: Unclassified
Component: JUnit (show other bugs)
Version: 6.x
Hardware: All All
: P2 blocker with 1 vote (vote)
Assignee: Victor Vasilyev
Depends on:
Reported: 2009-10-22 12:50 UTC by David.m Beer
Modified: 2009-10-23 01:19 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Note You need to log in before you can comment on or make changes to this bug.
Description David.m Beer 2009-10-22 12:50:00 UTC
My problem is that I have created a Src class file with just the class decleration. I then wanted to create a JUnit 
test class for this src class, I right clicked on the class in the Project Window, then selected Tools -> Create JUnit 
test. Selected what I wanted from the dialog and selected create. I then got presented with an error message saying 'No 
Methods defined. Can not create Test methods'. This then refused to create a blank test class with the template.

I have asked this question on the Mailing List and Marián Petráš has replied witht the following comment:

'I just looked to the bug database and found that that the bug I 
was talking was bug #129034 
( But it was 
about the Create Tests dialog. The problem you are facing is slightly 
different in that it is the routine for creation the test class what 
prevents from creating an empty test class.'

It seems that if there are no methods in the src class file then no empty test class will be created.
Comment 1 Alexei Mokeev 2009-10-22 13:01:00 UTC
Yep, seems that creation of empty unit test class should be allowed.
Comment 2 Victor Vasilyev 2009-10-23 00:03:46 UTC
I agree that generating tests for just created new class (without any methods) will be useful for a user practicing the
test driven development.

As far as I understood, generating tests for a class without methods was disabled because a generated class for a test
case will provokes an error like this:

Testcase: initializationError(javaapplication5.NewClassTest):        Caused an ERROR
No runnable methods
java.lang.Exception: No runnable methods
        at java.lang.reflect.Constructor.newInstance(

To avoid such error I'll add generating a stub test method like this:

    public void testSomeMethod() {
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");

A user may use the stub test method as a start point of the test driven development.
Comment 3 David.m Beer 2009-10-23 00:13:42 UTC
A small stub like that would be good. Is it possible to have the template similar to if you create the JUnit test 
class. As in the @Test and example is commented out.

so the stub would like

/*public void demoMethod() {
    example code
Comment 4 Victor Vasilyev 2009-10-23 00:41:53 UTC
Fixed in the main trunk:
Comment 5 Victor Vasilyev 2009-10-23 01:19:27 UTC

Commented out pieces of the code can be used only after "manual" review of the code.
Otherwise, if the test method is actually implemented then a user has a chance to find generated stub method via IDE
facilities, e.g. after first start of the test, via the Navigator, and so on.

Note, you may disable generating of the method body in a usual way, i.e. by unselecting the check box 
"Code Generation / Generated Code / Default Method Bodies" in the dialog "Crete Tests" that is displayed via the menu
item "Tools / Create JUnit Tests". In this case, you will have the following code:

    public void testSomeMethod() {

A next user action is the renaming, i.e. the user will select the method name and press Ctrl+R. And... Enjoy by the test
driven development!