Gatito
Gatito

Reputation: 63

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();
}

Upvotes: 1

Views: 2283

Answers (1)

Brandon G
Brandon G

Reputation: 145

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

Upvotes: 2

Related Questions