Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Pass a base URL into a TestNG test from testng.xml, with a default when it is not set, and override it from the command line.

We declare <parameter name="baseUrl" value="http://localhost:8080"/> in testng.xml, at suite level or test level, where test level wins, and receive it with @Parameters({"baseUrl"}) on a configuration method whose argument is marked @Optional("http://localhost:8080") so a missing value falls back to the default instead of failing. For a command-line override, the common approach is a system property: read System.getProperty("baseUrl", valueFromXml) and pass -DbaseUrl=... through Maven Surefire. Secrets never go into testng.xml; they come from environment variables.

Why this question is asked

Almost every integration or UI suite needs to know where the system under test lives: localhost on a laptop, a container in CI, a staging URL before release. Hard-coding it means editing code to switch environments, so TestNG offers parameters in the suite file. Interviewers ask this coding question to see whether we know how parameters flow from XML into a method, what happens when one is missing, and how to layer a command-line override on top. In this answer, we will declare the parameter, receive it with a default, see the failure without @Optional, add the command-line override, and finish with what must never be passed this way.

1A Quick Recap of TestNG Parameters

A testng.xml parameter is a name and a string value. TestNG injects it into the arguments of a method annotated with @Parameters, which lists the parameter names in the order of the method's arguments. It works on test methods and on configuration methods such as @BeforeClass, and the configuration method is the usual place, because the URL is set up once and used by many tests.

2Declaring the Parameter in testng.xml

Parameters can be declared on <suite> and on <test>. A value on <test> overrides the same name on <suite> for the classes in that test, which lets one suite file run the same classes against two places.

XML
<suite name="shop">
<parameter name="baseUrl" value="http://localhost:8080"/> <!-- suite default -->
<test name="api-local">
<classes><class name="com.shop.OrderApiTest"/></classes> <!-- baseUrl = localhost:8080 -->
</test>
<test name="api-stub">
<parameter name="baseUrl" value="http://localhost:9090"/> <!-- test level wins -->
<classes><class name="com.shop.OrderApiTest"/></classes> <!-- baseUrl = localhost:9090 -->
</test>
</suite>

Notice that the values are plain strings. TestNG converts them to simple argument types such as int or boolean when the method asks for them, but a URL stays a String that we parse ourselves.

3Receiving It With a Default

In the test class, @Parameters names the parameter, and @Optional on the argument gives the value to use when the suite does not define it, for example when a developer runs the class directly from the IDE without any testng.xml.

Java
public class OrderApiTest {
private String baseUrl;
@Parameters({"baseUrl"})
@BeforeClass
public void setUp(@Optional("http://localhost:8080") String baseUrl) {
this.baseUrl = baseUrl; // from testng.xml, or the @Optional default
}
@Test
public void listsOrders() {
HttpResponse<String> r = http.send(
HttpRequest.newBuilder(URI.create(baseUrl + "/orders")).build(),
HttpResponse.BodyHandlers.ofString());
assertEquals(r.statusCode(), 200);
}
}

With the api-stub test above, setUp receives http://localhost:9090. Run from the IDE without a suite file, it receives the @Optional value.

4What Happens Without @Optional

If we remove @Optional and run the class without the parameter defined, TestNG cannot call setUp, because it has no value for the argument. It fails the configuration method with an error saying that the parameter is required by the method but has not been marked @Optional or defined; the exact wording depends on the version.

When a @BeforeClass method fails, TestNG does not count that as a failed test. It reports every test in the class as SKIPPED, so a run can show 0 failures and a column of skips; depending on the version, a missing parameter may instead stop the run with a TestNGException. Either way the cause sits in a configuration error rather than in a red test, which is easy to miss in CI. That is why @Optional with a sensible local default, or a deliberate failure with a clear message, is better than relying on the suite file always being present.

5Overriding It From the Command Line

TestNG parameters come from the suite file, so to override a value from the command line most teams use a Java system property, read in the setup method, with the XML value as the fallback. Maven passes -D options to Surefire, and Surefire passes system properties into the forked test JVM.

Java
@Parameters({"baseUrl"})
@BeforeClass
public void setUp(@Optional("http://localhost:8080") String xmlBaseUrl) {
this.baseUrl = System.getProperty("baseUrl", xmlBaseUrl);
}
// mvn test -> baseUrl = value from testng.xml or @Optional
// mvn test -DbaseUrl=https://staging.shop.test -> baseUrl = https://staging.shop.test
Step throughWhere baseUrl comes from
  1. Is the system property baseUrl set?
    YesUse it (command line wins)NoLook at testng.xml
  2. Does the <test> define baseUrl?
    YesUse the test-level valueNoLook at the suite
  3. Does the <suite> define baseUrl?
    YesUse the suite-level valueNoUse the @Optional default

3 steps in order. Play them, or step through them one at a time.

The order of precedence is now explicit in one line: the command line wins, then testng.xml, then the @Optional default. If the pom sets the value permanently, it goes in Surefire's systemPropertyVariables, and a Maven property lets the command line still change it.

6Keeping Secrets Out of testng.xml

A base URL is harmless to commit, but the same mechanism tempts people to add a password or an API token as another <parameter>. testng.xml is committed to Git, copied between branches and printed in reports, so anything in it should be treated as public. Secrets should come from environment variables that the CI system injects from its secret store, read with System.getenv("SHOP_API_TOKEN"), and the setup should fail with a clear message if the variable is missing, rather than running with an empty token.

Java
String token = System.getenv("SHOP_API_TOKEN");
if (token == null || token.isBlank()) {
throw new IllegalStateException("Set SHOP_API_TOKEN; CI injects it from the secret store");
}

Notice that the error names the variable but never prints its value, so logs stay clean even when something goes wrong.

7Follow-up questions

Can @Parameters be used on a test method instead of @BeforeClass?

Yes, TestNG injects parameters into @Test methods too. Using a configuration method is usually cleaner, because the value is read once and every test in the class uses it, and a missing value is reported once instead of once per test.

How does this differ from a @DataProvider?

A parameter is one value per run, chosen by the suite, such as where the system lives. A data provider supplies many rows of test data to one test method, which runs once per row. We use parameters for environment settings and data providers for test cases.

Where would you put Surefire's systemPropertyVariables?

In the configuration of the maven-surefire-plugin, for example <systemPropertyVariables><baseUrl>${baseUrl}</baseUrl></systemPropertyVariables> with a Maven property baseUrl that has a default in the pom. Then mvn test -DbaseUrl=... changes the Maven property, and Surefire passes the new value to the test JVM.

8Conclusion

In this answer, we declared baseUrl in testng.xml at suite and test level, saw that the test level wins, and received it with @Parameters and an @Optional default on a @BeforeClass method. Without @Optional, a missing parameter fails the configuration method and skips every test in the class. We added a system property override that Maven passes with -DbaseUrl, so the precedence is command line, then XML, then default, and kept secrets out of the suite file by reading them from environment variables.

What to say in the interviewAbout 90 seconds aloud

Open with

In testng.xml I declare a parameter element with name baseUrl and value http localhost 8080. It can sit on the suite, as a default, or on a test element, and the test-level value wins for the classes in that test.

Then, if they want more

In the class I put @Parameters with baseUrl on a @BeforeClass method, and the method argument is annotated @Optional with the localhost URL, so if the suite does not define it, for example when I run the class from the IDE, I still get a sensible default.

Without @Optional and without the parameter, TestNG fails the configuration method, and that shows up as every test in the class being skipped rather than failed, which is easy to miss.

For the command-line override I use a system property, because TestNG parameters come from the suite file: in setUp I write baseUrl equals System.getProperty baseUrl, falling back to the value TestNG injected. Then mvn test with -DbaseUrl equals the staging URL overrides it, and if I want a default in the pom I use Surefire's systemPropertyVariables with a Maven property.

So the precedence is command line, then XML, then the @Optional default, all in one line of code. And I would never put passwords or tokens into testng.xml, since it is committed to Git; those come from environment variables injected by CI, with a clear error if they are missing.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Pass a base URL into a TestNG test from testng.xml, with a default when it is not set...": https://scrivio-interview-library.netlify.app/java/testng/897804155a/