1

I am writing some unit test that I want to pass environment parameters depending on the environment they are being executed.

@Test public void databaseConfigTest() throws AutomatedTestFailureException {
        String connectionUrl = System.getenv("app.database_url");
        String databaseName = "dbname";
        Assertions.assertTrue(domainService.canConnect(connectionUrl, databaseName));
    }

I am trying to execute this test using the following command:

mvn -Dapp.database_url=somedatabase_connection compile test

However, I get the following error:

com.test.automated.exception.AutomatedTestFailureException: java.lang.NullPointerException

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test.automated</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Automated Test</name>
    <description>automated testing</description>

    <properties>
        <java.version>1.8</java.version>
        <junit.jupiter.version>5.5.2</junit.jupiter.version>
        <junit.platform.version>1.5.2</junit.platform.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver-sync</artifactId>
            <version>4.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.23.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>26.0-jre</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

This mvn command will be executed by TeamCity using build job configuration parameters. Also is there a better way to accomplish this kind of test because this project will have alot of configuration parameters defined that needs to be passed to tests during execution.

user2054833
  • 2,115
  • 7
  • 24
  • 39
  • Just pass `spring.datasource.url` as a property and let spring do all the complex stuff. Also currently you are using `getenv` which gets environment variables NOT system variables passed with `-D` use `getProperty` for that. However it kind of looks like you are trying to work around Spring Boot instead of working with it. – M. Deinum May 20 '20 at 07:11

1 Answers1

2

Try the following example.

mvn compile test -Dapp.database_url=somedatabase_connection -Dtest=AppTest#getURL

You need to expose the value to the test add the following. The getProperty method retrieves the specified key.

String url = System.getProperty("app.database_url");

public class App {

    public static String getURL(String url){
        return url;
    }
}

import org.junit.Assert;
import org.junit.Test;

/**
 * Unit test for simple App.
 */
public class AppTest {

    @Test
    public void testURL() {
        String url = System.getProperty("app.database_url");;
        Assert.assertEquals("somedatabase_connection", App.getURL(url));
    }

}

To pass more than one value:

mvn compile test -Dapp.database_url=somedatabase_connection -Dname=foo -Dtest=AppTest#testURL

Follow up answer to your question.

You can run a test or tests multiple times by utilizing @ParameterizedTest which consumes your arguments from the source.

From JUnit 5 docs:

@ParameterizedTest @ValueSource(ints = { 1, 2, 3 }) void testWithValueSource(int argument) { assertTrue(argument > 0 && argument < 4); }

What's happening above: @ParameterizedTest method will be invoked three times, with the values 1, 2, and 3 respectively.

As parameters continue to grow overtime this annotation in my opinion provides a good maintainable solution.