0

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??

shbfy
  • 2,075
  • 3
  • 16
  • 37
  • 1
    Have you tried a test-setup like [this](https://stackoverflow.com/a/51892629/1377895) already? There is also a further [answer available](https://stackoverflow.com/a/44325673/1377895) that uses a similar setup like yours. – Roman Vottner Oct 02 '19 at 14:57
  • Yeah I’ve tried both :( I’m wondering if there is something else in the configuration that could impact the test environment – shbfy Oct 02 '19 at 14:58
  • 1
    I haven't tried Camel 3 yet, TBH. I can't state whether the test-setup that worked with Camel 2.22+ also works with Camel 3, which today has released RC2. You could try to go back to 2.24.2 (or the like), if possible, and see if the configuration works with that version. Maybe also someone on the active [Camel Gitter channel](https://gitter.im/apache/apache-camel) can help on your issue – Roman Vottner Oct 02 '19 at 15:04
  • Thanks @RomanVottner that’s helpful :) – shbfy Oct 02 '19 at 15:09

1 Answers1

1

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.

burki
  • 6,741
  • 1
  • 15
  • 31
  • Thank you this is very helpful! Would you mind linking me to an example of testing a processing bean? – shbfy Oct 02 '19 at 17:54
  • 1
    The Bean is a plain Java object. No frameworks at all. You can test its methods with plain JUnit as you would test any other Java method. See here for a quite long introduction how to use Beans in Camel: https://access.redhat.com/documentation/en-us/red_hat_jboss_fuse/6.2/html/apache_camel_development_guide/basicprinciples-beanintegration – burki Oct 03 '19 at 05:37