集成WireMock和JMeter时,本地主机连接被拒绝

问题描述 投票:1回答:1

我正在尝试将Wiremock集成到Jmeter测试计划中,以便每次执行测试计划时,它将在开始时启动WireMock的实例,然后运行我概述的测试。我遵循了这个答案(https://stackoverflow.com/a/49130518/12912945),但是我遇到的问题是我总是会收到错误:

Response message:Non HTTP response message: Connect to localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect

据我所见,即使在测试计划开始时在JSR223 Sampler中包含以下代码,Wiremock服务器也永远不会启动:

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import static com.github.tomakehurst.wiremock.client.WireMock.*;

public class WireMockTest {

    public static void main(String[] args) {
        WireMockServer wireMockServer = new WireMockServer();
        configureFor("127.0.0.1", 8080);
        wireMockServer.start();
        StubMapping foo = stubFor(get(urlEqualTo("/some/thing"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withBody("Hello World")));
        wireMockServer.addStubMapping(foo);
    }
}

任何人都可以向我指出如何正确整合两者的正确方向,我曾尝试将其添加到类路径中,但感觉好像我没有正确地做到这一点,或者我缺少了一些东西

谢谢!

jmeter wiremock
1个回答
0
投票

您正在定义main function,但我看不到您在哪里执行它。换句话说,您的Wiremock初始化代码根本不会执行。

您需要显式调用此main函数来执行代码,要完成此操作,请将下一行添加到脚本的末尾:

main

一旦完成,JSR223采样器将调用WireMockTest.main() 函数内部的代码,然后将启动Wiremock服务器。

另一个选择是删除这些main和函数声明,仅使用

class

因为您在JSR223测试元素中定义的脚本不需要import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.stubbing.StubMapping; import static com.github.tomakehurst.wiremock.client.WireMock.*; WireMockServer wireMockServer = new WireMockServer(); configureFor("127.0.0.1", 8080); wireMockServer.start(); StubMapping foo = stubFor(get(urlEqualTo("/some/thing")) .willReturn(aResponse() .withStatus(200) .withBody("Hello World"))); wireMockServer.addStubMapping(foo);

签出entry point文章以获取有关JMeter测试中Groovy脚本编写的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.