Home > OS >  How to test camel route that only set properties
How to test camel route that only set properties

Time:04-30

I have a route builder that depending on the content of a message sets specific properties. It doesn't send it to other endpoints so I cannot mock them and check what they got. I can produce a message or exchange but is there a way to check it after it was transformed by this route builder?

CodePudding user response:

Assuming that your route is synchronous (i.e. not from:seda), you can simply check that the property was updated on the exchange that you send via the ProducerTemplate.

Let's assume that you need to check the value of the property TestProp:

package com.example.demo;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.ExchangeBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
class PropertyUpdateTest {
    @Produce("direct:testProps")
    ProducerTemplate template;

    @Autowired
    CamelContext camelContext;

    @Test
    void verifyRouteUpdatesProperty() {
        Exchange exchange = ExchangeBuilder.anExchange(camelContext)
                .withProperty("TestProp", "InitialVal")
                .build();

        template.send(exchange);

        assertThat(exchange.getProperty("TestProp")).isEqualTo("UpdatedVal");
    }

}

  • Related