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.
Apache Camel is an open source integration framework designed to make integrating systems simple and easy. It allows end users to integrate various systems using the same API, providing support for multiple protocols and data types, while being extensible and allowing the introduction of custom protocols.
You can use returnReplyBody(Expession expression) method for a mock response.
See the example:
@Test
public void testGetActionRoute() throws Exception {
MockEndpoint greeterService = getMockEndpoint("mock:cxf:bean:greeterService");
greeterService.returnReplyBody(new Expression() {
@Override
public <T> T evaluate(Exchange exchange, Class<T> type) {
GreeterServiceResponse response = new GreeterServiceResponseResponse();
response.setGreeting("Hello!");
return (T) response;
}
});
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With