2016-06-02 23 views
0

Dies ist mein Test-Klasse:com.github.tomakehurst.wiremock.client.VerificationException: Erwarteter Zustand 201 für http: // localhost: 8080/__ admin/Mappings/neu, wurde aber 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"))); 

    } 
} 

Ich bin immer folgende Fehlermeldung:

com.github.tomakehurst.wiremock.client.VerificationException: Erwarteter Zustand 201 für http://localhost:8080/__admin/mappings/new aber war 404 bei com.github.tomakehurst.wiremock.client.HttpAdminClient.postJsonAssertOkAndReturnBody (HttpAdminClient.java : 156) bei com. github.tomakehurst.wiremock.client.HttpAdminClient.addStubMapping (HttpAdminClient.java:65) bei com.github.tomakehurst.wiremock.client.WireMock.register (WireMock.java:138) bei com.github.tomakehurst.wiremock. client.WireMock.register (WireMock.java:134) bei com.github.tomakehurst.wiremock.client.WireMock.givenThat (WireMock.java:65) bei com.github.tomakehurst.wiremock.client.WireMock.stubFor (WireMock.java:69) bei com.charter.cdevices.rest.CompanionDevicesRestServiceTest.testPostCdevices (CompanionDevicesRestServiceTest.java:33) bei sun.reflect.NativeMethodAccessorImpl.invoke0 (native Methode) bei sun.reflect.NativeMethodAccessorImpl.invoke (Unbekannt Quelle) bei sun.reflect.DelegatingMethodAccessorImpl.invoke (Unbekannte Quelle) bei java.lang.reflect.Method.invoke (Unbekannte Quelle) bei org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall (FrameworkMethod.java:47) bei org.junit.internal.runners.model.ReflectiveCallable. run (ReflectiveCallable.java:12) bei org.junit.runners.model.FrameworkMethod.invokeExplosively (FrameworkMethod.java:44) bei org.junit.internal.runners.statements.InvokeMethod.evaluate (InvokeMethod.java:17) bei org.junit.runners.ParentRunner.runLeaf (ParentRunner.java:271) bei org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java:70) bei org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner. java: 50) bei org.junit.runners.ParentRunner $ 3.run (ParentRunner.java:238) bei org.junit.runners.ParentRunner $ 1.schedule (ParentRunner.java:63) bei org.junit.runners.ParentRunner.runChildren (ParentRunner.java:236) bei org.junit.runners.ParentRunner.access $ 000 (ParentRunner.java:53) bei org.junit.runners.ParentRunner $ 2.evaluate (ParentRunner.java:229) bei org.junit.internal.runners.statements.RunBefores.evaluate (RunBefores.java:26) bei org .junit.runners.ParentRunner.run (ParentRunner.java:309) bei org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run (JUnit4TestReference.java:50) bei org.eclipse.jdt.internal.junit .runner.TestExecution.run (TestExecution.java:38) bei org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java:459) bei org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java:675) bei org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (RemoteTestRunner.java:382) bei org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (RemoteTestRunner.java: 192)

Antwort

0

Sie die WireMock Regel

@Rule 
public WireMockRule wireMockRule = new WireMockRule(); 

und nach können Sie es Ihre Stubs verwenden sollten

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

Für dieses Beispiel verwenden zu schaffen, nehme ich die wiremock war von außen angefangen, oder? So sollten Sie die configure für mit dem Host hinzufügen und den Hafen:

configureFor("localhost", 8080); 
0

ClassRule Anmerkung kann auch verwendet werden:

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

    } 
} 

und stubFor Methode für die Klasse der Regel Objekt aufrufen.