Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock WS client and provide response in camel route

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.

like image 315
deveproject Avatar asked Sep 15 '14 20:09

deveproject


People also ask

What is apache Camel used for?

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.


1 Answers

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();
}
like image 161
Vladimir Petrusha Avatar answered Oct 30 '22 23:10

Vladimir Petrusha