Wiremock和Spring Integration

时间:2017-04-20 20:09:42

标签: spring-integration spring-test spring-restcontroller wiremock

我在Spring集成中有一个流程,其工作原理如下

从MQ读取 - >进行消息转换 - >将转换后的消息发送到Api

现在在我的集成测试中,我试图嘲笑Api,我尝试了两种口味但没有成功。

这是线索风味的测试(也发布在Wiremock Github上)

String requestBody = TestUtil.createSampleInputMessage(rNumber);

System.out.println("Expected Post msg by EvApiClient :" + requestBody);


stubFor(post(urlEqualTo(TestUtil.EVENT_URL))
            //.withHeader("Content-Type", equalTo("application/json"))
            //.withRequestBody(equalToJson(requestBody))

        );


ResponseEntity response = restTemplate.getForEntity("http://localhost:9966/__admin", String.class);

System.out.println("Printing Expected response :" + response);

sendToJmsOutChannel.send(MessageBuilder.withPayload(TestUtil.createSampleInputMessage(receiptNumber)).build());

verify(postRequestedFor(urlEqualTo(TestUtil.EVENT_URL)));`

我得到的失败是

  

com.github.tomakehurst.wiremock.client.VerificationException:任何存根映射都无法匹配请求。最近的存根映射是:预期:<   POST   / PA /他/ V1 / EV和GT;但是:<   POST   / PA /他/ V1 / EV和GT;

以下是 Spring的MockRestServiceServer

的测试
@Before
public void setup() throws PdsListenerException, URISyntaxException {       

    mockServer = MockRestServiceServer.createServer(restTemplate);

}

@Test
public void test_001_success() throws InterruptedException, JSONException, URISyntaxException {
    rNumber = TestUtil.generateRNumber();

    String requestBody = TestUtil.createSampleInputMessage(rNumber);



    mockServer.expect(once(), requestTo("http://localhost:9966" + TestUtil.EVENT_URL))
        .andRespond(withCreatedEntity(new URI(TestUtil.EVENT_URL)));



    sendToJmsOutChannel.send(MessageBuilder.withPayload(TestUtil.createSampleInputMessage(rNumber)).build());


    mockServer.verify();


}

我得到的失败是

  

java.lang.AssertionError:预期会有1个未满足的期望的进一步请求。   0请求已执行。

我不确定匹配失败的原因。是否有人可以指出我应该在哪里看。

1 个答案:

答案 0 :(得分:1)

实际上发现了WireMock的问题,我将使用这种味道。

我需要改变的是

    @ClassRule
public static WireMockClassRule wireMockRule = new WireMockClassRule(9966);

@Rule
public WireMockClassRule instanceRule = wireMockRule;

...

instanceRule.stubFor(post(urlPathEqualTo(Constants.EVENT_URL))

...
 instanceRule.verify(1, postRequestedFor(urlEqualTo(TestUtil.EVENT_URL)));

基本上使用WireMockRule的实例进行存根和验证,而不是像以前那样。

stubFor(post(urlEqualTo(TestUtil.EVENT_URL))