QA Interview Hub · Knowledge base · Quizzes · Українська

Java Core › The test project

Gradle in brief, configuration through properties and env, SLF4J logging

Test settings are not hardcoded: a system property wins, then an environment variable, then the default in a .properties file. Secrets live in the CI secret store, not in the repository. Gradle is an alternative to Maven with a Groovy or Kotlin script, and it does not pass -D to the test process, so a property is forwarded with systemProperty. Logging goes through SLF4J with {} placeholders.

Java · Project and build

The same test suite must run on stage and on CI, and the password must not live in the repository. How?

An automation project outlives a single environment: URL, browser and tokens change, and it is run from a laptop and from CI. The three topics of this lesson answer three questions: what to build with (Gradle as an alternative to Maven), where to take settings from, and how to leave behind a readable log.

Gradle. It is an alternative to Maven with a build.gradle (Groovy) or build.gradle.kts (Kotlin) script. Dependencies, plugins and tasks are the same, but the script is code. That is more flexible and brings incremental builds with a cache, but it reads harder than a declarative pom. A few differences matter to an automation engineer: JUnit 5/6 is enabled explicitly through useJUnitPlatform() (plus the junit-platform-launcher dependency), tags are set in includeTags, and a run looks like ./gradlew test --tests "*LoginTest". The wrapper (gradlew, like mvnw in Maven) pins the tool version: use it instead of a global installation.

// build.gradle.kts
tasks.test {
    useJUnitPlatform { includeTags("api") }   // JUnit tags
    // -D goes to the Gradle JVM, not to the tests: pass it on
    systemProperty("base.url", System.getProperty("base.url") ?: "https://stage.example.test")
}

dependencies {
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

How it works and why: configuration. URL, browser and credentials are not hard-coded. A typical scheme has three layers, highest priority first: a system property from the command line, an environment variable, a default value in a .properties file. Locally a run with no parameters is enough, on CI a variable is enough, and the secret lives in the CI secret store, not in the repository.

Configuration layers

1. -Dbase.url=… (command)the highest priority in this example2. Environment variablesecrets: API_TOKEN from CI3. config.propertiesdefaults kept in the repository
Figure 1. The order of priorities is a convention: what matters is that there is one for the whole project and it is written in the README.
public final class Config {
    private static final Properties DEFAULTS = load("config.properties");

    public static String get(String key) {
        String sys = System.getProperty(key);                 // 1. command
        if (sys != null) return sys;
        String env = System.getenv(key.toUpperCase().replace('.', '_')); // base.url → BASE_URL
        if (env != null) return env;                          // 2. environment
        return DEFAULTS.getProperty(key);                     // 3. file
    }
}

An important trap: Maven Surefire passes -D into the test process, but Gradle does not. The command ./gradlew test -Dbase.url=… sets the property in the JVM of Gradle itself, so the test task must forward it through systemProperty, as in the example above. Otherwise the test silently takes the default and goes to the wrong environment. Secrets come from environment variables, are not printed to the log and are not put into test names.

Logging through SLF4J. SLF4J is a facade: code writes to org.slf4j.Logger, and the real work is done by an implementation (Logback, Log4j 2) on the classpath. There must be one implementation, otherwise SLF4J warns about several providers (in SLF4J 1.x these were bindings). Compare two styles:

System.out.println("Order " + orderId + " created for " + user);   // no levels, no time
log.debug("Response: " + response.asString());                       // the string is built always
private static final Logger log = LoggerFactory.getLogger(CheckoutTest.class);

log.info("Order {} created for user {}", orderId, userId);
log.debug("Response status {}", response.statusCode());
log.error("Checkout failed for order {}", orderId, exception); // the exception goes last
  • The {} placeholders format the message only when the level is enabled, while concatenation builds the string always, even for a disabled debug.
  • The exception goes as the last argument: then the log has the stack trace, not only toString().
  • System.out.println has no levels, time, thread name or file output, and in a parallel run lines of different tests mix.
  • The level is controlled in configuration, not in code: Logback looks for logback-test.xml in src/test/resources first.
<configuration>
  <appender name="OUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} %-5level [%thread] %logger{20} - %msg%n</pattern>
    </encoder>
  </appender>
  <root level="INFO"><appender-ref ref="OUT"/></root>
  <logger name="com.example.api" level="DEBUG"/>
</configuration>

The thread name in the pattern (%thread) is useful in parallel runs: it shows which test a line belongs to. With Lombok the @Slf4j annotation is enough.

Limits and alternatives.

  • Gradle pays off in large multi-module projects and for its cache; for a small test project the difference from Maven is small. The choice is usually dictated by the team and existing infrastructure.
  • For complex configuration there are libraries (for example Typesafe Config or Owner), but for a few keys a custom class of 15 lines is enough.
  • A log does not replace reports: logs explain what happened, while a report (Allure or HTML) shows the summary. Screenshots and request records are attached to the report.
  • DEBUG is useful locally, and on CI it is enabled only for the package you are investigating: otherwise the log is too big.

Typical mistakes.

  • A password or token in a .properties file in the repository or in the log.
  • Not forwarding -D into the Gradle test task: tests ignore the parameter.
  • Two SLF4J implementations on the classpath: warnings and unpredictable messages.
  • Concatenation in log.debug and System.out.println instead of a logger.
  • Logging a full response string with personal data or a token.

Check yourself

Why might ./gradlew test -Dbase.url=… not change the behaviour of the tests?

Answer

Gradle sets -D in its own JVM, while tests run in a separate process. The property must be forwarded in the test task through systemProperty. Maven Surefire passes -D itself.

Why are {} placeholders better than concatenation in a logger message?

Answer

The message is formed only when the level is enabled. Concatenation builds the string every time, even with debug off, and the exception is best passed as the last argument.

Where should the password of a test account live in an automation project?

Answer

In an environment variable or the CI secret store. The repository holds only defaults without secrets, and the secret is not printed to the log.

In the interview. Junior: Gradle is an alternative to Maven with a Groovy or Kotlin script. JUnit is enabled through useJUnitPlatform, and I run it through gradlew. I take settings from a default file, override them with a system property, and take secrets from environment variables. I log through SLF4J with placeholders, not System.out. Middle/Senior: I keep one configuration priority order per project and know that Gradle must forward -D into tests explicitly. Secrets live in the CI store, not in the log. One SLF4J implementation, the level is controlled in logback-test.xml, and the thread name in the pattern helps read parallel runs.

More articles in this chapter