Camel REST DSL Consumer Template Testing

I have the following code:

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;

public class OrderNumberRouteBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        restConfiguration().component("servlet").bindingMode(RestBindingMode.json)          
            .dataFormatProperty("prettyPrint", "true")
            .contextPath("suppliera/rest").port(8080);

        rest("/ordernumber").description("ordernumber rest service")
            .consumes("application/json").produces("application/json")

            .get("/{id}").description("get ordernumber").outType(ServiceResponse.class)
            .to("bean:orderNumberService?method=getOrderNumber(${header.id})");
    }
}

      

How can I use JUnit to test this code? Can it CamelTestSupport

handle it?

I want to create a test like:

@Produce(------myendpoint----) 
protected ProducerTemplate testProducer; 

public void mytest(){
testProducer.requestBody("foo");
}

      

how can I mock this? what am I putting in ----- myendpoint ---- to link to this route?

+3


source to share


2 answers


As a possible solution, you can set a URI for the REST route and use that URI in your junit testing. To do this, you need to switch RestDefinition to RouteDefinition by calling the route method, and then you can call the from method and set the uri argument. Example using a straight endpoint:

    rest("/ordernumber").description("ordernumber rest service")
    .consumes("application/json").produces("application/json")
    .get("/{id}").description("get ordernumber").outType(ServiceResponse.class)
    .route().from("direct:myendpoint")
    .to("bean:orderNumberService?method=getOrderNumber(${header.id})");

      

In your junit class, you can type:



@Produce(uri = "direct:myendpoint")
protected ProducerTemplate testProducer;

      

Hope it helps.

+2


source


Have you seen examples on this page? https://camel.apache.org/testing.html



Since you have Spring, try the Spring Testing Examples on this page. This will be more appropriate for your situation than use CamelTestSupport

.

0


source







All Articles