首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找不到Mockserver请求

找不到Mockserver请求
EN

Stack Overflow用户
提问于 2020-09-11 16:39:11
回答 1查看 1.1K关注 0票数 0

我目前正在尝试创建期望,并通过mockServer (可以在here中找到)验证它们。

我已经看过他们的文档好几次了,但是我找不到为什么我的期望验证失败了。这是我的代码

代码语言:javascript
复制
    @Rule
    public MockServerRule mockServerRule = new MockServerRule(this, 9000);

    private MockServerClient mockServer;

    @Test
    public void testVerification() {
        mockServer.when(
                HttpRequest.request()
                  .withMethod("POST")
                  .withPath("/validate")
                  .withHeader("\"Content-type\", \"application/json\"")
                  .withBody("{username: 'foo', password: 'bar'}"))
                  .respond(
                      HttpResponse.response()
                        .withStatusCode(401)
                        .withHeaders(
                          new Header("Content-Type", "application/json; charset=utf-8"),
                          new Header("Cache-Control", "public, max-age=86400"))
                        .withBody("{ message: 'incorrect username and password combination' }")
                        .withDelay(TimeUnit.SECONDS,1)
                    );

        mockServer.verify(HttpRequest.request().withMethod("POST")
                  .withPath("/validate")
                  .withBody("{username: 'foo', password: 'bar'}"),
                VerificationTimes.exactly(1));
    }

在尝试验证时,我总是收到错误消息

代码语言:javascript
复制
java.lang.AssertionError: Request not found exactly once, expected:<{
  "method" : "POST",
  "path" : "/validate",
  "body" : "{username: 'foo', password: 'bar'}"}> but was:<>
    at org.mockserver.client.server.MockServerClient.verify(MockServerClient.java:267)
    at it.HttpTest.testVerification(HttpTest.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.mockserver.junit.MockServerRule$1.evaluate(MockServerRule.java:107)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-11 16:43:53

好吧,你忘了向你的模拟服务器发送一个请求。您只是模拟了它的行为,然后检查服务器是否已被调用,但您错过了调用。

用下面这样的方式试试:

代码语言:javascript
复制
public int callValidateUrl() throws IOException {
    URL url = new URL("http://localhost:9000/validate");

    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("POST");
    con.setRequestProperty("Content-Type", "application/json");

    con.setDoOutput(true);
    String jsonInputString = "{username: 'foo', password: 'bar'}";
    try (OutputStream os = con.getOutputStream()) {
        byte[] input = jsonInputString.getBytes("utf-8");
        os.write(input, 0, input.length);
    }

    return con.getResponseCode();
}

如果你使用的是Spring,RestTemplate更容易使用。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63843619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档