Apache Camel 3.x Springboot 2.7.x routes are not added during testing

93 Views Asked by At

My RouteBuilder Class

@Component
public class MyRouteBuilder extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("direct:start")
            .to("mock:result"); // Or whatever the route should do
    }
}
@ExtendWith(SpringExtension.class)
@CamelSpringBootTest
@SpringBootTest
@MockEndpoints // Mock all endpoints by default if needed
public class CamelRouteTest {

    @Autowired
    private CamelContext camelContext;

    @BeforeEach
    public void setUp() throws Exception {
        // Assuming we have set autoStartup=false in application properties or configuration
        // Use adviceWith to mock or intercept endpoints before the context starts
        camelContext.getRouteDefinitions().forEach(routeDefinition -> {
            try {
                AdviceWith.adviceWith(routeDefinition, camelContext, new AdviceWithRouteBuilder() {
                    @Override
                    public void configure() throws Exception {
                        // Perform advice logic here
                        // For example, intercept sending to an endpoint and do something else
                        interceptSendToEndpoint("direct:endpoint")
                                .skipSendToOriginalEndpoint()
                                .to("mock:result");
                    }
                });
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });

        // Start Camel context manually after adviceWith configuration
        camelContext.start();
    }

    @AfterEach
    public void tearDown() throws Exception {
        // Properly stop the Camel context after each test
        if (camelContext != null) {
            camelContext.stop();
        }
    }

I am upgrading camel 3.2.x to Upgrade to 3.14.x. Springboot But the tests are throwing RouteDefinition must be specified error!! Debugging the test it shows camel is already started and it is not adding any routes!!

1

There are 1 best solutions below

0
user1013443 On

First: if you are using AdviceWith, you must specify that the CamelContext is NOT started before the routes are being adviced. You can do that with:

@UseAdviceWith
public class CamelRouteTest {

Second: My @BeforeEach looks like this: (You have to specify the routeID which you want to modify)

@BeforeEach
public void setup() throws Exception {
    AdviceWith.adviceWith(camelContext, "import_csv_frische_invoice",
            // intercepting (amend, change the route to our needs)
            r -> {
                r.weaveByToUri("xslt-saxon:*").replace().to("xslt-saxon:import/stylesheet/frische/csv_to_xml_invoice.xsl");
                r.weaveByToUri("direct:processInvoices").replace().to("mock:result"); // Removing the further processing and replace it with something which we can mock
            });
    camelContext.start();
}

Third: @ExtendWith(SpringExtension.class) is not needed

Fourth: I have several Testclasses: Each Testclass is using AdviceWith. But I cannot make them running without using @DirtiesContext. Maybe you have an idea. The problem to solve is, that each TestClass which uses @UseAdviceWith shall execute the setup method first, and once all setup method of all Testclasses are done, the camelcontext can be started. Let me know maybe you have an idea how to solve this the elegant way.