我正在尝试将 Cucumber-JVM 与 WireMock 集成,并且我不断得到
java.net.ConnectException: Connection refused: connect
我已经尝试了几个教程,包括来自 cucumber.io的官方文档,并且还尝试了以下这些:
我的 Gradle 依赖项:
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 Cucumber for Java Book 如果有更好的方法来测试微服务,我愿意接受新的想法。
我有一个带有我的步骤定义的测试类,它从属性文件中获取端口信息。如下所示:
@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")));
}
}
我还创建了一个带有可运行示例的存储库。
如果您没有找到 README 文件,在查看 repo 时,您可以使用以下命令运行项目:
./gradlew cucumber
或者如果您在 Windows 上:
gradle cucumber
在我让它工作之后,我重构了代码并将示例留在了我上面链接的存储库中,如果你有同样的问题,请检查它。