I want to mock WS client call in camel route, check request and provide response.
Here is a test
package com.example.helloworld;
import org.apache.camel.*;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelSpringTestSupport;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author deveproject
* @version 9/15/14
*/
public class WsClientMockTest extends CamelSpringTestSupport {
@Produce(uri = "direct:test-ws-client-mock")
protected ProducerTemplate template;
@Override
public String isMockEndpoints() {
return "cxf:bean:greeterService";
}
@Test
public void testGetActionRoute() throws Exception {
MockEndpoint greeterService = getMockEndpoint("mock:cxf:bean:greeterService");
greeterService.expectedMessagesMatches(new Predicate() {
@Override
public boolean matches(Exchange exchange) {
assertEquals("U.S.", exchange.getIn().getBody(String.class));
return true;
}
});
template.sendBody("U.S.");
greeterService.assertIsSatisfied();
}
@Override
protected AbstractApplicationContext createApplicationContext() {
return new ClassPathXmlApplicationContext("META-INF/spring/camel-context.xml");
}
}
The org.apache.camel.Predicate checks is request is correct. I cannot find a way how to provide a response.
I have working example with above test. I can publish it on demand.
Thank you.
You can use the returnReplyBody(Expession expression)
method for a mock response.
@Test
public void testGetActionRoute() throws Exception {
MockEndpoint greeterService = getMockEndpoint("mock:cxf:bean:greeterService");
GreeterServiceResponse response = new GreeterServiceResponse();
response.setGreeting("Hello!");
greeterService.returnReplyBody(constant(response));
greeterService.expectedMessagesMatches(exchange -> {
assertEquals("U.S.", exchange.getIn().getBody(String.class));
return true;
});
template.sendBody("U.S.");
greeterService.assertIsSatisfied();
}