java.lang.IllegalArgumentException: CamelContext must be specified on: Message[] - Camel core

1.2k Views Asked by At

Below is builder from camel core & writting junit testcases with it & camel core version used is 2.22.1.

new ExchangeBuilder(null)
      .withBody(body)
      .withHeader(header, headerValue)
      .build();

Junit testcases is throwing error when calling above builder - java.lang.IllegalArgumentException: CamelContext must be specified on: Message[]

1

There are 1 best solutions below

0
On

You'll have to provide constructor of ExchangeBuilder a instance of CamelContext. If your test class inherits from CamelTestBuilder you can use context() method to obtain it during a test.

public class SomeTest extends CamelTestSupport {
    
    @Test
    public void testSomething(){

        String body = "some body";
        String header = "SomeHeader";
        String headerValue = "Some header value";

        ExchangeBuilder builder = new ExchangeBuilder(context())
            .withBody(body).withHeader(header, headerValue);
        
        Exchange exchange = builder.build();
        CamelContext contextFromExchange = exchange.getContext();
        // Do something with the exchange?
    }
}

If you're not using one of the camel-test modules you'll have to create and configure one manually using e.g new DefaultCamelContext();. For testing routes use of one of the camel-test modules is highly recommended though as they make things a lot easier.