com.github.tomakehurst.wiremock.client.VerificationException



这是我的测试类:

public class CompanionDevicesRestServiceTest {
public static ComDevicesRestService comDevicesRestService;
public static IComDevices cdevicesService;
public static ComDevices cdevicesRequest;
@BeforeClass
public static void init(){
cdevicesService = new StubComDevicesService();
comDevicesRestService = new ComDevicesRestService(cdevicesService);
cdevicesRequest = mock(ComDevices.class);
}
@Test
public void testPostCdevices() throws JsonProcessingException{
WireMock.stubFor(WireMock.post(WireMock.urlEqualTo("/cdevices"))
.withHeader("Accept", WireMock.equalTo("application/json"))
.willReturn(WireMock.aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("Some content")));
}
}

我得到以下错误:

com.github.tomakehurst.wiremock.client.VerificationException:的预期状态201http://localhost:8080/__admin/mappings/new但是是404网址:com.github.tomakehurst.wiremock.client.HttpAdminClient.postJsonAssertOkAndReturnBody(HttpAdminClient.java:156)网址:com.github.tomakehurst.wiremock.client.HttpAdminClient.addStubMapping(HttpAdminClient.java:65)网址:com.github.tomakehurst.wiremock.client.wiremock.register(wiremock.java:138)网址:com.github.tomakehurst.wiremock.client.wiremock.register(wiremock.java:134)网址:com.github.tomakehurst.wiremock.client.wiremock.givenThat(wiremock.java:65)网址:com.github.tomakehurst.wiremock.client.wiremock.stubFor(wiremock.java:69)网址:com.charter.cdvices.rest.ComponentDevicesRestServiceTest.testPostCdevices(CompanionDevicesRestServicesTest.java:33)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于sun.reflect.NativeMethodAccessorImpl.invoke(未知源)在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)位于java.lang.reflect.Method.ioke(未知源)网址:org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)网址:org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)位于org.junit.runners.model.FrameworkMethod.invokeExplorery(FrameworkMethod.java:44)位于org.junit.internal.runners.statements.InvokeMethod.eevaluate(InvokeMethod.java:17)网址:org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)网址:org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)网址:org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)网址:org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)网址:org.junit.runners.ParentRunner$1schedule(ParentRunner.java:63)网址:org.junit.runners.ParentRunner.runChildren(ParentRunnr.java:236)网址:org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)网址:org.junit.runners.ParentRunner$2.eevaluate(ParentRunner.java:229)网址:org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)网址:org.junit.runners.ParentRunner.run(ParentRunner:309)网址:org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TtestReference.java:50)网址:org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)网址:org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)网址:org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)网址:org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)网址:org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

对于这个例子,我假设wiremock是从外部启动的,对吧?因此,您应该添加带有主机和端口的配置:

configureFor("localhost", 8080);

您应该使用WireMock规则

@Rule
public WireMockRule wireMockRule = new WireMockRule();

然后你可以用它来创建你的存根

wireMockRule.stubFor(WireMock.post(WireMock.urlEqualTo("/cdevices"))
.withHeader("Accept", WireMock.equalTo("application/json"))
.willReturn(WireMock.aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("Some content")));

ClassRule注释也可以使用:

public class CompanionDevicesRestServiceTest {
public static ComDevicesRestService comDevicesRestService;
public static IComDevices cdevicesService;
public static ComDevices cdevicesRequest;

@ClassRule
public static WireMockClassRule wireMockRule = new WireMockClassRule(wireMockConfig()
.containerThreads(20)
.port(8080)
);
@BeforeClass
public static void init(){
cdevicesService = new StubComDevicesService();
comDevicesRestService = new ComDevicesRestService(cdevicesService);
cdevicesRequest = mock(ComDevices.class);
}
@Test
public void testPostCdevices() throws JsonProcessingException{
wireMockRule.stubFor(WireMock.post(WireMock.urlEqualTo("/cdevices"))
.withHeader("Accept", WireMock.equalTo("application/json"))
.willReturn(WireMock.aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("Some content")));
}
}

并对类规则对象调用stubFor方法。

根据WireMock文档,我们有三种主要方法:JUnit 5.x、Junit4.x和standalone。

我的建议很简单:仔细检查哪种方法是你的,然后继续进行记录在案的建议,但是。。。

它从来没有对我起作用。为了解决这里讨论的问题,我做了以下操作:

  1. 在测试一开始就添加了一个@规则。我将在下面分享整个类声明,包括runner和使用PowerMockRunner运行这些东西的简短SSL技巧:
@RunWith(PowerMockRunner.class)  
@PowerMockIgnore("javax.net.ssl.*")  
public class StartingOddsChangesProviderIntegrationTest {  

@Rule public WireMockRule wireMockRule = new WireMockRule();  
(...)
  1. 您不需要实例化WireMockServer,也不需要添加server.start()/stop()行,因为@Rule注释已经存在

最后,相应pom.xml文件中的WireMock depedency条目如下:

<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>2.27.0</version>
<scope>test</scope>
</dependency>

我也尝试过wiremock-jre8工件,但最终出现了同样的错误,所以我正在推进wiremock单机版。

相关内容

  • 没有找到相关文章

最新更新