Bug with @Optional parameters ?

626 views
Skip to first unread message

Yves Dessertine ☼

unread,
Jun 17, 2010, 9:08:17 AM6/17/10
to testng-users
Hello,

I wonder if I discovered a bug in the way Testng handles @Optional
parameters. I reproduced this with v5.12 and 5.12.1. Here is 5.12.

testng.xml file:
==================================================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Parameters">
<test name="OptionalParameterFails">
<classes>
<class name="OptionalParameterFails"></class>
</classes>
</test>
</suite>
==================================================

Class:
==================================================
public class OptionalParameterFails {

@Parameters("optional")
public OptionalParameterFails(@Optional String optional) {
// never gets executed
}
}
==================================================

Stack trace:
==================================================

org.testng.TestNGException:
An error occurred while instantiating class OptionalParameterFails:
null
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:
329)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:
71)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:90)
at
org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:
119)
at org.testng.TestRunner.initMethods(TestRunner.java:305)
at org.testng.TestRunner.init(TestRunner.java:251)
at org.testng.TestRunner.init(TestRunner.java:221)
at org.testng.TestRunner.<init>(TestRunner.java:183)
at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:
105)
at org.testng.remote.RemoteTestNG
$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:158)
at org.testng.SuiteRunner
$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:551)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:241)
at org.testng.SuiteRunner.run(SuiteRunner.java:195)
at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:903)
at org.testng.TestNG.runSuitesLocally(TestNG.java:872)
at org.testng.TestNG.run(TestNG.java:780)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:75)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:127)
Caused by: java.lang.NullPointerException
at
org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:
23)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:
276)
... 17 more
==================================================

What's going wrong? If I provide a value (@Optional("defaulValue")),
it works...

According to the @Optional javadoc:

/** Specifies that the current parameter is optional. TestNG will
pass
* in a specified default value, or <code>null</code> if none is
specified.
*/

Any help welcome :)



Yves Dessertine

Yves Dessertine ☼

unread,
Jun 18, 2010, 4:55:29 AM6/18/10
to testng-users
Can you just tell me if I made a stupid error, or if this "should"
work the way it's written? :)

Thx,

Y.

Yves Dessertine ☼

unread,
Jun 18, 2010, 5:01:55 AM6/18/10
to testng-users
This looks like a specific problem with the "null" value. If I write
@Optional("foo"), it works. If I write @Optional("null"), it doesn't,
exactely like @Optional


The fact that @Optional("null") behaves the same that @Optional is
normal, because the @Optional annotation is defined like this:

public @interface Optional {
public String value() default Parameters.NULL_VALUE;
}

And :

public class Parameters {
public static final String NULL_VALUE= "null";
}


I continue to investigate what's goning wrong with the null value.

Yves

Cédric Beust ♔

unread,
Jun 18, 2010, 8:55:16 AM6/18/10
to testng...@googlegroups.com
Hi Yves,

I'll take a look as soon as I can, but as you correctly noticed, the @Optional annotation needs to define a default value if I want it to be optional as well, and the null pointer not being a constant in Java, I had to come up with something else, hence the "null" value.

--
Cédric



--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.




--
Cédric

Yves Dessertine ☼

unread,
Jun 19, 2010, 8:51:22 AM6/19/10
to testng-users
Hello Cedric, glad to hear about you.

I've been looking a little bit in Testng's source code (v5.12.1), and
maybe I have a clue about the problem described above.

The stacktrace says:
org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:
23)

And here are the lines 21 to 24:

21: Class[] paramClasses = new Class[params.length];
22: for(int i = 0; i < params.length; i++) {
23: paramClasses[i] = params[i].getClass();
24: }

And, since params is an array containing the null value (not the
String "null" but the special value), line 23 generates a NPE.

OK, fine. But what's the paramClasses[] array for? It isn't used
anywhere. It seems it's here just to generate a NPE :p

I will try rebuilding TestNG after removing the lines 21 to 24 and
see if it's better...

Yves

Yves Dessertine ☼

unread,
Jun 19, 2010, 9:17:55 AM6/19/10
to testng-users
OK, it *seems* to work with lines 21-24 removed.....

Do you want me to open a bug report on JIRA for this?

It's the first time I think I'm able to FIX a bug in an opensource
project, I'm really proud. Champagne :)

Yves

Cédric Beust ♔

unread,
Jun 19, 2010, 10:46:13 AM6/19/10
to testng...@googlegroups.com
Hi Yves,

Good catch!

I just submiited your fix to the trunk, can you update and confirm?

And congratulations on your first open source bug, hope there will be many more :-)

--
CVédric



Yves

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.




--
Cédric

Yves Dessertine ☼

unread,
Jun 19, 2010, 11:49:44 AM6/19/10
to testng-users
Thanks, for your encouragement and for the quick fix. After having
checked out the trunk and built the testng-5.12.2beta.jar, I can
confirm that it works :)

Another last question: when do you plan to release v5.12.2? I need
this fix monday :p (but will build the trunk in the meanwhile, no
hurry).

Have a nice weekend.

Y.
Reply all
Reply to author
Forward
0 new messages