Why TestNG Optional Parameters Include Double Quotes and How to Fix Them
Learn why TestNG values seem to include double quotes, how to trace their source, and how to fix Java, XML, and system-property inputs.

Short answer: the quotes in @Optional("mysql") are Java syntax and are not part of the value. TestNG supplies the string mysql when the matching parameter is absent. If your test receives "mysql" with quote characters, those characters came from the actual input source: an escaped quote in Java, " in XML, a command-line or system-property argument, or another programmatic parameter source.
The reliable fix is to inspect the value at the receiving method, identify which source won, and remove quote characters from that source while retaining delimiters required by Java, XML, or your shell. This guide covers the behavior, precedence rules, runnable examples, diagnostics, common failures, and integration options.
1. What @Optional actually does
TestNG parameters can come from testng.xml, Java system properties, programmatic configuration, and other runner integrations. A test method commonly declares the parameter name with @Parameters and supplies a fallback with @Optional:
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class DatabaseTest {
@Test
@Parameters("db")
public void connects(@Optional("mysql") String db) {
System.out.println("database=[" + db + "]");
}
}
If no parameter named db is found, TestNG passes the default value mysql. The quotation marks delimit the Java string literal; the Java compiler does not include those delimiters in the resulting string. TestNG’s parameter documentation uses the same example and states that the method receives the default specified inside @Optional when the XML parameter is missing (TestNG parameters documentation).
The annotation itself does not strip or add quote characters. Therefore, these two declarations produce different values:
@Optional("mysql") // value: mysql
@Optional("\"mysql\"") // value: "mysql"
In the second declaration, the backslash escapes an embedded quote, so the quote becomes data. Use that form only when the receiving application really needs quote characters.
2. Confirm whether quotes are really in the value
Console formatting can make a value look quoted. Always print visible boundaries and, when necessary, the character code points.
@Test
@Parameters("db")
public void diagnose(@Optional("mysql") String db) {
System.out.println("value=[" + db + "]");
System.out.println("length=" + db.length());
for (int i = 0; i < db.length(); i++) {
System.out.printf("index=%d char=%s code=%d%n",
i, db.charAt(i), (int) db.charAt(i));
}
}
A value printed as value=[mysql] contains no quotes. A value printed as value=["mysql"] starts with code point 34 and ends with code point 34, proving that quote characters are data. This distinction prevents you from changing a correct annotation when the apparent quotes came from a logger, debugger, JSON representation, or custom formatting.
3. Find the parameter source that actually won
Do not assume the @Optional default was used. TestNG resolves parameters from configured sources, and a matching value can override the fallback.
XML parameters and scope
A minimal testng.xml file looks like this:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Example">
<test name="Database">
<parameter name="db" value="mysql"/>
<classes>
<class name="DatabaseTest"/>
</classes>
</test>
</suite>
The quotes around mysql delimit the XML attribute. They are not part of the attribute value. XML 1.0 defines " for a literal quote inside an attribute (W3C XML 1.0 specification):
<parameter name="db" value=""mysql""/>
The latter intentionally passes "mysql". Remove " when the quotes were accidental.
TestNG parameters can be scoped at suite, test, class, and method levels. More specific scopes take precedence in the order <suite> → <test> → <class> → <methods>. A method-level value can therefore hide the value you inspected at suite level. Check every matching declaration and make sure the name is exactly the same, including capitalization.
System properties and runner arguments
TestNG also documents Java system properties as a parameter source. A command such as:
mvn test -Ddb=mysql
can provide the value through the build runner. Shell quoting is needed to preserve spaces, but those shell delimiters should normally be removed before the JVM receives the property. For example, this keeps the space in the value:
java -Dlast-name="von Braun" -jar tests.jar
Whether quote characters become data depends on the shell, build tool, IDE, and argument parser. Inspect the effective property inside the JVM:
String db = System.getProperty("db");
System.out.println("system property=[" + db + "]");
If the output contains quote characters, remove them from the property value or build configuration. Keep the command-line delimiters required to group a value containing spaces.
Programmatic and framework sources
Listeners, factories, data providers, Gradle or Maven plugins, and CI variables can inject values without appearing beside the test method. Search the repository and CI configuration for the parameter name:
rg -n "db|Optional|Parameters|systemProperty|parameter" .
Log the final value at the method boundary rather than only logging configuration files. The method boundary shows what TestNG delivered after all precedence rules were applied.
4. A complete reproducible example
The following small Maven project demonstrates the plain default, an XML override, and an intentional quoted value.
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.9.0</version>
<scope>test</scope>
</dependency>
import org.testng.Assert;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class ParameterValueTest {
@Test
@Parameters("db")
public void defaultHasNoQuotes(@Optional("mysql") String db) {
Assert.assertEquals(db, "mysql");
Assert.assertFalse(db.contains("\""));
}
@Test
@Parameters("quotedDb")
public void escapedQuotesAreData(@Optional("\"mysql\"") String db) {
Assert.assertEquals(db, "\"mysql\"");
}
}
<suite name="Parameter suite">
<test name="Defaults">
<classes>
<class name="ParameterValueTest"/>
</classes>
</test>
<test name="Quoted XML value">
<parameter name="quotedDb" value=""mysql""/>
<classes>
<class name="ParameterValueTest"/>
</classes>
</test>
</suite>
Run it with your normal Maven or Gradle TestNG configuration. The first test receives mysql; the second receives quote characters because both the Java default and XML value explicitly encode them.
5. Common errors and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
"mysql" is received from @Optional |
Escaped quotes are inside the annotation. | Change @Optional("\"mysql\"") to @Optional("mysql"). |
| Quotes appear only with XML | The attribute uses " around the value. |
Use value="mysql" unless quotes are required data. |
Changing @Optional has no effect |
A matching XML, system-property, or programmatic value overrides it. | Trace the source and inspect effective scope and precedence. |
| Parameter is missing | Name mismatch or wrong @Parameters order. |
Match names exactly and map method arguments in the same order as the annotation list. |
| Value differs in CI only | Environment variable, JVM argument, or build profile supplies another value. | Print the effective value and inspect CI command arguments and secrets configuration. |
| Spaces disappear or quotes remain | Shell and build-tool parsing changed the argument. | Quote the shell argument for grouping, then verify the JVM property value. |
| Debugger shows quotes but assertion passes | The debugger displays string delimiters. | Inspect length and code points; trust the runtime value, not display formatting. |
6. A practical troubleshooting checklist
- Print the value with brackets:
[value]. - Print its length and first and last code points if the output is ambiguous.
- Search for every declaration of the parameter name.
- Inspect suite, test, class, and method scopes in
testng.xml. - Check the
@Parametersnames and their order against method arguments. - Inspect
-Dproperties, environment variables, Maven or Gradle profiles, and CI job arguments. - Remove quote characters from data sources while retaining syntax delimiters.
- Repeat the assertion at the receiving method so a future configuration change fails clearly.
7. Java, XML, and shell quoting rules in one view
| Source | Syntax for plain mysql |
Syntax for quoted "mysql" |
|---|---|---|
| Java annotation | @Optional("mysql") |
@Optional("\"mysql\"") |
| XML attribute | value="mysql" |
value=""mysql"" |
| Shell property | -Ddb=mysql |
-Ddb='"mysql"' (quotes intentionally passed as data) |
Java string-literal behavior is defined by the Java Language Specification (JLS string literals). TestNG’s @Optional contract is described in its API reference (TestNG 7.9.0 Optional API).
8. Performance, reliability, and maintenance
Parameter lookup is negligible compared with browser startup, database setup, or network calls in a typical test. Reliability problems usually come from configuration drift: duplicate names, hidden CI overrides, and values that are formatted differently by local shells and build agents.
- Define canonical defaults in one place and document intentional overrides.
- Fail early when a value contains unexpected delimiters, for example with an assertion that rejects leading or trailing quotes.
- Prefer explicit XML or build configuration for environment-specific values instead of relying on shell-specific escaping.
- Log parameter names and safe metadata, but avoid printing credentials or tokens.
- Pin and record the TestNG version used by the build; the API behavior described here is documented for TestNG 7.9.0, while the general parameter model is shared across current releases.
9. Or skip the browser setup
If your test workflow also needs screenshots of environments, reports, or failed pages, ScreenshotNeo provides a website screenshot API and MCP server. A single request returns PNG, JPEG, WebP, or PDF without maintaining a browser stack. See the ScreenshotNeo documentation for all options.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
Cookie banners, newsletter popups, and chat widgets are removed before the shot. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and response headers identify the page verdict and billing result. The MCP server includes take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000. Create a free ScreenshotNeo account.
10. FAQ
Does @Optional add quotes automatically?
No. It returns the characters in the Java string value. Delimiter quotes are removed by Java parsing.
Can XML quotation marks be removed entirely?
No. Keep the XML attribute delimiters. Remove only " sequences that were intended as formatting rather than data.
Why does my default not apply?
A matching parameter from XML, a system property, or another configured source is taking precedence. Trace the effective value at the test method.
Does parameter order matter?
Yes. TestNG maps names listed in @Parameters to method arguments in that order.
Should I trim the value in application code?
Only if surrounding whitespace is invalid for your domain. Trimming does not reliably solve embedded quote characters; fix the source that supplied them.


