微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

找不到Mockserver请求

如何解决找不到Mockserver请求

我目前正在尝试建立期望,并通过模拟服务器(可以在here中找到它们)。

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

    @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));
    }

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

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)

解决方法

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

尝试类似这样的方法:

dotnet ef migrations add InitialCreate

如果您使用的是Spring,则RestTemplate更易于使用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?