通过Wiremock发送获取请求会导致错误

时间:2017-12-11 00:31:05

标签: unit-testing http testing mocking wiremock

我正在尝试设置一个wiremock服务器来对一个进行get http调用的类进行单元测试。我能够成功设置模拟服务器,但是当我尝试存根请求时,我遇到了错误。我对WireMock非常陌生,这看起来很简单。请指教:

@Test
public void testMethod{
.........
    WireMockServer wireMockServer = new WireMockServer(8089);
    wireMockServer.start();

    stubFor(get(urlEqualTo("/api/get-magic"))
        .willReturn(aResponse()
            .withHeader("Content-Type", "application/json")
            .withBody("{\"currently\":{\"windSpeed\":12.34}}")));
........
    wireMockServer.start();

}

堆栈跟踪:

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Error 403 No valid crumb was included in the request</title>
</head>
<body><h2>HTTP ERROR 403</h2>
<p>Problem accessing /__admin/mappings. Reason:
<pre>    No valid crumb was included in the request</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>

</body>
</html>
; line: 1, column: 2]

    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1702)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:558)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:456)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleOddValue(ReaderBasedJsonParser.java:1906)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:749)
    at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3850)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3799)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2858)
    at com.github.tomakehurst.wiremock.common.Json.read(Json.java:50)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.safelyExecuteRequest(HttpAdminClient.java:431)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.executeRequest(HttpAdminClient.java:399)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.addStubMapping(HttpAdminClient.java:133)
    at com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:292)
    at com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:287)
    at com.github.tomakehurst.wiremock.client.WireMock.givenThat(WireMock.java:106)
    at com.github.tomakehurst.wiremock.client.WireMock.stubFor(WireMock.java:110)
    at com.move.lead.la.service.allocation.ods.OdsTest.testGetOdsDataLive(OdsTest.java:156)
    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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
    at org.testng.TestNG.run(TestNG.java:1031)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

1 个答案:

答案 0 :(得分:4)

你需要调用WireMock.configureFor(8089);以便为端口8089配置静态DSL。目前它将被默认为8080,这是你运行Jenkins的地方。