【发布时间】:2020-12-06 04:45:30
【问题描述】:
我正在尝试将 Cucumber-JVM 与 WireMock 集成,但我一直在尝试
java.net.ConnectException: Connection refused: connect
我尝试了几个教程,包括来自 cucumber.io 的 Official Docs 并且还尝试了以下这些:
Introduction to WireMock from Baeldung
我的 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")));
}
}
我还创建了一个带有可运行示例的repository。
如果您没有找到 README 文件,在查看 repo 时,您可以使用以下命令运行项目:
./gradlew cucumber
或者如果您使用的是 Windows:
gradle cucumber
在我让它工作之后,我重构了代码并将示例留在了我上面链接的存储库中,如果你有同样的问题,请检查它。
【问题讨论】:
标签: java spring gradle cucumber-jvm wiremock