How to set header on Apache Camel 3.X.X route?

Viewed 600

I am doing integration testing on the Camel route.

The Camel route:

from("direct:abc")
  .routeId("abc-route")
  .process(MyProcessor())
  .toD("direct:xyz")

As this route is getting tested into isolation, I need to set headers in the test; previously, they arrived from another route. How can I do it?

Simplified test class:

@SpringBootTest
@MockEndpoints
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
class MyRouteTest {

  @Autowired
  lateinit var camelContext: CamelContext

  @Produce(uri = "direct:abc")
  lateinit var template: ProducerTemplate

  @EndpointInject(uri = "mock:direct:xyz")
  lateinit var destEndpoint: MockEndpoint

  @Test
  fun `should test route`() {
    template.sendBody("")
  }
}

I have tried doing camelContext.adapt(ModelCamelContext::class.java) but still cannot get to the route and forcibly modify it.

1 Answers

Even though already answered in the comments, I think the question is worth to have an answer.

The ProducerTemplate interface has lots of different methods to send a Camel Exchange to a Camel endpoint.

There are methods to just send a message body, a body with one or multiple headers or a body with one or multiple Exchange properties.

There is also a fluent interface that can be used to customize the Exchange in a nice, descriptive way.

Example from the Camel Docs:

Integer result = FluentProducerTemplate.on(context)
    .withHeader("key-1", "value-1")
    .withHeader("key-2", "value-2")
    .withBody("Hello")
    .to("direct:inout")
    .request(Integer.class);
Related