I'm trying to integrate Cucumber-JVM with WireMock and I keep getting
java.net.ConnectException: Connection refused: connect
I've tried several tutorials, including the Official Docs from cucumber.io And also tried these below:
Introduction to WireMock from Baeldung
My Gradle dependencies:
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'io.cucumber:cucumber-java:6.2.2'
testImplementation 'io.cucumber:cucumber-junit:6.2.2'
testImplementation 'io.rest-assured:spring-web-test-client:4.3.1'
testCompile "com.github.tomakehurst:wiremock-jre8:2.27.0"
compile group: 'io.cucumber', name: 'cucumber-spring', version: '6.4.0'
The basic idea is to Mock a Server Response, so in the future I'll be able to create some integration tests between several microservices. The idea came from a book while I'm reading The Cucumber for Java Book If there are better ways to test microservices I'm open to new ideas.
I've a Test Class with my Step Definitions that getting the port info form a propertie file. Like below:
@SpringBootTest
@CucumberContextConfiguration
public class ConnectionWithCucumber {
@Value("${another.server.port}")
private int PORT;
@Rule
private WireMockRule wireMockRule = new WireMockRule(PORT);
private String messageResult;
@Given("I want to read a message")
public void iWantToRead() {
createMessageStub();
}
@When("I send the request")
public void iSendTheRequest() {
messageResult = given().get("localhost:8082/message").getBody().asString();
}
@Then("I should be able to read the word {string}")
public void iShouldBeAbleToReadTheWord(String arg0) {
assertEquals(arg0, messageResult);
}
private void createMessageStub() {
wireMockRule.stubFor(get(urlEqualTo("/message"))
.withHeader("Accept", equalTo("application/json"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("message")));
}
}
And I've also created a repository with a runnable example.
If you don't find a README file, while looking the repo you can run the project by using:
./gradlew cucumber
or if you are on Windows:
gradle cucumber
After I got it working, I refactored the code and left the example on the repository I've linked above, if you are have the same problem check it out.