I run my tests using both Eclipse and Ant. When running from Ant, I'd like to get a parameter using the @Parameters annotation in the testng.xml file. Whe running from Eclipse, I'd like to just default the parameter to a value I hold in my own property file. Because no testng.xml (other than the temporary one that is created on the fly) file is used when running the file from Eclipse environment, the parameter is not found and I get the message:
org.testng.TestNGException:
Parameter 'propertyName' is required by @Configuration on method preTest
but has not been defined...
I was hoping that a missing parameter would contain a null so that I could fall back to my default but apparently that isn't the case.
Does anyone have any suggestions for 'optional' @Parameters?
In a nutshell, I'm trying to get a configurable value from testng.xml when that value is found in the file but I want processing to continue without an exception when the value is not found in the configuration file.
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=85495&messageID=151927#151927
--
Cédric
The behavior is exactly this. If the parameter is missing then the
test is aborted with the message:
Parameter 'propertyName' is required by @Configuration/@Test on method
methodName but has not been defined.
./alex
--
.w( the_mindstorm )p.
TestNG co-founder
EclipseTestNG Creator
On 5/24/07, Cédric Beust ♔ <cbe...@google.com> wrote:
> Hi,
>
> We could introduce a flag to enable this behavior. It would have to be a
> flag because I think the default behavior should be to abort when a
> parameter is missing. I'll look into this.
>
The behavior is exactly this. If the parameter is missing then the
test is aborted with the message:
Parameter 'propertyName' is required by @ Configuration/@Test on method
methodName but has not been defined.
Well, I have probably miss read your message then. Sorry.
./alex
--
.w( the_mindstorm )p.
TestNG co-founder
EclipseTestNG Creator
> --
>
> Cédric
> >
>
I want it for the same reason: Eclipse can't know to use an XML suite when I click on a class and say "Run as TestNG test".
-Dan
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=85495&messageID=326925#326925
> I'd like to vote for this, also. I'd love to be able to mark a
> parameter as @Optional and have the test not fail, (or even warn) but
> just pass null when the parameter hasn't been set.
>
> I want it for the same reason: Eclipse can't know to use an XML suite
> when I click on a class and say "Run as TestNG test".
I've filed this as TESTNG-213
http://jira.opensymphony.com/browse/TESTNG-213
-Dan
> Would there be any value in also having some form of default value for the
> @Optional to use when its not provided? So maybe @Optional would just pass
> in null, but @Optional("foo") would pass in "foo" (or an error if the type
> was a mismatch).
Sure, that'd be cool.
-Dan
> Would there be any value in also having some form of default value for the
> @Optional to use when its not provided? So maybe @Optional would just pass
> in null, but @Optional("foo") would pass in "foo" (or an error if the type
> was a mismatch).
Bizarrely, I think this is impossible, or, at least, not possible in an
ordinary way.
It is illegal to write this annotation:
public @interface Optional {
public String value() default null;
}
It's a compile-time error. "attribute value must be constant", which
means that null is not a valid default value for an annotation parameter.
I have no idea why they made this choice in the specification, but there
you have it. (Presumably, under the hood, they can't distinguish between
a null default and the absence of a default...?)
You could, of course, use a poison value, like this:
public @interface Optional {
public String value() default "SPECIAL_NULL_VALUE_FOR_TESTNG_ONLY";
}
... and then have TestNG pass null in that case. I don't dare use "" as
the default poison, because it's too likely someone would want to
distinguish between "" and null.
-Dan
> Mark Derricutt wrote:
>
>> Would there be any value in also having some form of default value for the
>> @Optional to use when its not provided? So maybe @Optional would just pass
>> in null, but @Optional("foo") would pass in "foo" (or an error if the type
>> was a mismatch).
>
> Bizarrely, I think this is impossible, or, at least, not possible in an
> ordinary way.
Oh, I was wrong. TestNG has (of course) already encountered this
delightful restriction on null values, and has already specified a
particular poison value to mean null: "null". So this, for example,
will pass a null value as a parameter when used in a TestNG suite XML
file:
<parameter name="my-string" value="null" />
(I just hope you never need to pass that particular four-letter word as a
parameter to a TestNG method; you'll find an entirely different word in
its place.)
Anyway, I followed in its footsteps with a patch submitted to TESTNG-213,
available here:
http://jira.opensymphony.com/browse/TESTNG-213
So now you can do this:
@Parameters({"this parameter doesn't exist"})
@Test
public void testNonExistentParameter(@Optional String foo) {}
or even:
@Parameters({"this parameter doesn't exist"})
@Test
public void testNonExistentParameter(@Optional("bar") String foo) {}
Notably, I had to break JDK14 compatibility to submit this patch; as far
as I know, it's impossible to support parameter annotations in JDK14 (e.g.
with qdox).
-Dan