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 84565

Summary: JUnit templates generates erroneous code
Product: java Reporter: abonnema <abonnema>
Component: JUnitAssignee: Theofanis Oikonomou <theofanis>
Status: REOPENED ---    
Severity: blocker    
Priority: P3    
Version: 6.x   
Hardware: All   
OS: All   
Issue Type: ENHANCEMENT Exception Reporter:
Attachments: A tarred gz file with test code (a netbeans 6.5.1 project)

Description abonnema 2006-09-09 06:02:45 UTC
Hi,

I run netbeans 6.0 M2 from Ubuntu Dapper.

The template that is generated for any class contains code like this:

TestClass instance = null;

instance.add(object);

Which is wrong (nullpointerexception). So every testcase containing this must 
be altered. 

Is it possible to generate a statement like
"TestClass instance = new TestClass(params...);" ?
Comment 1 Alexei Mokeev 2009-03-27 12:38:03 UTC
Was not able to reproduce with 6.5 FCS.
Please reopen and attach a sample, if it's yet the case for you.
Comment 2 abonnema 2009-03-29 04:30:44 UTC
AFAICS the error still occurs. I used version 6.5.1 and got the same results apart from some extra annotations.
I will attach the project so you should be able to check the result. The following focuses on the issue.

If I code the following class:

public class TestClass {
    Date date;

    public TestClass(Date date) {
        this.date = date;
    }

    public boolean isLater(Date toDate) {
        return this.date.compareTo(toDate) > 0;
    }

}


The testcode generated for the isLaterDate() method looks like this:

    /**
     * Test of isLater method, of class TestClass.
     */
    @Test
    public void testIsLater() {
        System.out.println("isLater");
        Date toDate = null;
        TestClass instance = null;
        boolean expResult = false;
        boolean result = instance.isLater(toDate);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }
Now, setting an instance to null and consequently calling it, will always result in an exception. So, this code will
barf by definition.
At the time I was just wondering whether the testcase could be generated in a more obliging way, like:

    public void testIsLater() {
        System.out.println("isLater");
        Date toDate = new Date();
        TestClass instance = new TestClass(new Date());    // You could also test new TestClass(null)
        boolean expResult = false;
        boolean result = instance.isLater(toDate);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }

I realise this is not lifesaving code. I just feel that we should not have errors in the code we generate.

Hope this helps.
Comment 3 abonnema 2009-03-29 04:33:23 UTC
Created attachment 79008 [details]
A tarred gz file with test code (a netbeans 6.5.1 project)
Comment 4 Alexei Mokeev 2009-03-30 11:02:31 UTC
Got the point. Thank you.