Search code examples
javamavencucumbercucumber-jvmcucumber-java

How do I pass a CLI argument to a Cucumber Java test suit?


I have a test suite that test my web service, I want to sent a custom IP as a CLI argument from maven to the test suite so instead of having a hard-coded IP like in the example below:

@Before
public void server_connection() {
    ConnectionToServer serverConnection = new ConnectionToServer("localhost", 5776);
    serverConnection.open();
}

I want to have a variable IP like this:

@Before
public void server_connection() {
    ConnectionToServer serverConnection = new ConnectionToServer(IPArgumentFromMaven, 5776);
    serverConnection.open();
}

Solution

  • In your method, you can read this value as a system property like this:

    @Before
    public void server_connection() {
        ConnectionToServer serverConnection = new ConnectionToServer(System.getProperty("IPArgumentFromMaven"), 5776);
        serverConnection.open();
    }
    

    Now you can pass a value for IPArgumentFromMaven to maven from command line like this:

    mvn clean test -DIPArgumentFromMaven=localhost