spring-bootkotlinapache-camelcamel-test

Failure running Spring-Boot + Camel tests in batch


For my Kotlin application with Spring-Boot 2.7.0 and Apache Camel 3.17.0, I am running into a rather surprising issue: I have a set of JUnit 5 test cases that individually run fine (using mvn test -DTest="MyTest"); but when run in batch via mvn test or in IntelliJ IDEA, some test cases fail with org.apache.camel.FailedToCreateRouteException... because of Cannot set tracer on a started CamelContext.

The funny thing is, that these test cases do not have tracing enabled. My test setup looks like the following for most of the tests:

@CamelSpringBootTest
@SpringBootTest(
    classes = [TestApplication::class],
    properties = ["camel.springboot.java-routes-include-pattern=**/SProcessingTestRoute"]
)
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
@UseAdviceWith
internal class ProcessingTest(
    val template: FluentProducerTemplate,
    @Value("classpath:test-resource") private val TestResource: Resource,
    val camelContext: CamelContext
) {
    @EndpointInject("mock:result")
    lateinit var resultMock: MockEndpoint

    @Test
    fun `test my route`() {
        AdviceWith.adviceWith(camelContext, "processing-route") { route ->
            route.weaveAddLast().to("mock:result")
        }
        resultMock.expectedCount = 1
        camelContext.start()

        // ...
        // here comes the actual test
    }
}

There are a couple of tests where I do not advice routes; i.e., these test cases do not have the @AdviceWith annotation, and these test cases do not fail during the batch run.

Debugging this issue is hard; therefore, I would highly appreciate any pointers, hints, or hypothesis for potential causes, and ideas on what to try to narrow down the problem!


Solution

  • You probably need a fresh camel context for each test. Try adding @DirtiesContext to each test class. If that doesn't work, add it to each test method.