我想在dumpToLogAsJava模式下使用MockServer analysis来打印我对HTTP服务的java格式的期望。

此外,我想为此使用mockserver-maven-plugin。

到目前为止,我已经能够获得像这样的json格式的输出:

2016-06-07 19:10:46,348 DEBUG [main] org.mockserver.cli.Main [?:?]

Using command line options: proxyPort=18080

2016-06-07 19:10:46,623 INFO [MockServer HttpProxy Thread] org.mockserver.proxy.http.HttpProxy [?:?] MockServer proxy started on port: 18080
2016-06-07 19:10:46,626 DEBUG [MockServer HttpProxy Thread] o.m.c.ConfigurationProperties [?:?] Property file not found on classpath using path [mockserver.properties]
2016-06-07 19:10:46,626 DEBUG [MockServer HttpProxy Thread] o.m.c.ConfigurationProperties [?:?] Property file not found using path [mockserver.properties]
2016-06-07 19:11:01,838 DEBUG [nioEventLoopGroup-3-1] o.m.client.netty.NettyHttpClient [?:?] Sending request: {
"method" : "GET",
"path" : "/sources",
 [...]
}
2016-06-07 19:11:02,180 DEBUG [nioEventLoopGroup-3-1] o.m.client.netty.NettyHttpClient [?:?] Received response: {
 [...]
}
2016-06-07 19:11:02,220 INFO [nioEventLoopGroup-3-1] o.m.proxy.http.HttpProxyHandler [?:?] returning response:
        {
          [...]
        }
 for request as json:
        {
          [...]
        }

 as curl:
        [...]


通过将以下内容添加到我的pom.xml

                <plugin>
                    <groupId>org.mock-server</groupId>
                    <artifactId>mockserver-maven-plugin</artifactId>
                    <version>3.10.4</version>
                    <configuration>
                        <proxyPort>18080</proxyPort>
                        <logLevel>DEBUG</logLevel>
                    </configuration>
                    <executions>
                        <execution>
                            <id>start-mock</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>runForked</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>stop-mock</id>
                            <phase>post-integration-test</phase>
                            <goals>
                                <goal>stopForked</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>


我不知道如何在不编写一些其他Java代码的情况下将输出格式更改为Java(即使那样,我也不知道我到底要写什么)。

PS。我似乎根本无法获得Java格式的转储-我在GH上填写了一个问题

最佳答案

缺少的部分是在执行了您感兴趣的请求之后,您需要转储期望。
我的印象是,它是您启用的功能,然后Mockserver随时随地转储期望。

无论如何,这是一种丑陋的方式:

    ClientAndProxy client = ClientAndProxy.startClientAndProxy(18080);
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            client.dumpToLogAsJava();
        }
    });

09-28 01:59