com.github.tomakehurst.wiremock.client.VerificationException: ожидаемый статус 201 для http: // localhost: 8080 / __ admin / mappings / new, но был 404

Это мой тестовый класс:

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")));

    }
}

Я получаю следующую ошибку:

Вы должны использовать правило WireMock


person Sat    schedule 02.06.2016    source источник


Ответы (3)


и после этого вы можете использовать его для создания своих заглушек

@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")));
person xjodoin    schedule 20.10.2016

Также можно использовать аннотацию ClassRule:

configureFor("localhost", 8080);
person Anderson Luiz    schedule 07.06.2017

и вызовите метод stubFor для объекта правила класса.

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")));

  }
}

com.github.tomakehurst.wiremock.client.VerificationException: ожидаемый статус 201 для http://localhost:8080/__admin/mappings/new, но было 404 на com.github.tomakehurst.wiremock.client.HttpAdminClient.postJsonAssertOkAndReturnBody (HttpAdminClient.java:156) на com.github.tomakehurst.wiremock.clientClientClient. 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.cdevices.rest.CompanionDevicesRestService (CompanionDevicesRestServiceTest.java:33) в sun.reflect.NativeMethodAccessorImpl.invoke0 (Nat ive Method) в sun.reflect.NativeMethodAccessorImpl.invoke (Неизвестный источник) в sun.reflect.DelegatingMethodAccessorImpl.invoke (Неизвестный источник) в java.lang.reflect.Method.invoke (Неизвестный источник) в 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.invokex ) в org.junit.internal.runners.statements.InvokeMethod.evaluate (InvokeMethod.java:17) в org.junit.runners.ParentRunner.runLeaf (ParentRunner.java:271) в org.junit.runner.ClassJUnner BlockJUnit4ClassRunner.java:70) в org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java:50) в org.junit.runners.ParentRunner $ 3.run (ParentRunner.java.java:23 .schedul e (ParentRunner.java:63) на org.junit.runners.ParentRunner.runChildren (ParentRunner.java:236) на org.junit.runners.ParentRunner.access $ 000 (ParentRunner.java:53) на org.junit.runners. ParentRunner $ 2.evaluate (ParentRunner.java:229) в org.junit.internal.runners.statements.RunBefores.evaluate (RunBefores.java:26) в org.junit.runners.ParentRunner.run (ParentRunner.java:309) в org.eclipse.jdt.internal.junit4.runner. JUnit4TestReference.run (JUnit4TestReference.java:50) в org.eclipse.jdt.internal.junit.runner.TestExecution.run (TestExecution.java:38) в org.eclipse.jdt.internal.junit.terunestRunest.Reunest RemoteTestRunner.java:459) на org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java:675) на org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.java: 382) на org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (RemoteTestRunner.java:192)

person Robert Gabriel    schedule 18.01.2018