shbfy
shbfy

Reputation: 2135

Test Camel Processor in Spring Boot

I'm trying to test a Camel Processor in Spring Boot. However I am getting alot of exceptions even when following the guidance from Camel in Action and other SO answers.

I am using Camel 3.0.0-M4

For example here is my test:

@SpringBootTest
@BootstrapWith(SpringBootTestContextBootstrapper.class)
public class MyProcessorTest extends CamelTestSupport {

    @Produce
    ProducerTemplate template;

    @EndpointInject(uri = "mock:out")
    private MockEndpoint resultEndpoint;

    @Before
    public void setUp() throws Exception {
        super.setUp();
    }

    @BeforeClass
    public static void stopCamelStart() {
        SpringCamelContext.setNoStart(true);
    }

    @AfterClass
    public static void enableCamelStart() {
        SpringCamelContext.setNoStart(false);
    }

    @Before
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("seda:test")
                        .process(new MyProcessor())
                        .to("mock:out");
            }
        };
    }

    @Test
    public void testPrepend() throws Exception {
        Map<String, String> body = new HashMap<String, String>();
        body.put("test", "body");

        template.sendBody("seda:test", body);
        String result = resultEndpoint.getExchanges().get(0).getIn().getBody(String.class);

    }
}

I get an java.lang.ArrayIndexOutOfBoundsException

If I try and start the camel context context.start(); I get a java.lang.NullPointerException

If I swap the route I sent the body to template.sendBody("mock:out", body); no processing has happened.

If I change from seda:test to direct:test I get a very slow running test and org.apache.camel.component.direct.DirectConsumerNotAvailableException: No consumers available on endpoint: direct://test

Where am I going wrong??

Upvotes: 0

Views: 3130

Answers (1)

burki
burki

Reputation: 7005

On first view you have two methods marked with @Before. The order of execution of the two in not at all guaranteed by JUnit. So perhaps this is causing trouble.


However, I don't see any need to setup a Camel Route Test to test a Processor.

I highly recommend to replace your Processor(s) (they are clumsy and hard to test) with a POJO and call it with .bean instead of .processor.

Upvotes: 1

Related Questions