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 - JUnit templates generates erroneous code
Summary: JUnit templates generates erroneous code
Status: REOPENED
Alias: None
Product: java
Classification: Unclassified
Component: JUnit (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: Theofanis Oikonomou
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-09-09 06:02 UTC by abonnema
Modified: 2011-11-04 12:08 UTC (History)
0 users

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments
A tarred gz file with test code (a netbeans 6.5.1 project) (8.42 KB, application/x-gzip)
2009-03-29 04:33 UTC, abonnema
Details

Note You need to log in before you can comment on or make changes to this bug.
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.