2

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!

Ulrich Schuster
  • 1,670
  • 15
  • 24
  • I **think** you should not start the CamelContext yourself, it will be done automatically as soon as the class is annotated with @CamelSpringBootTest – TacheDeChoco Aug 01 '22 at 07:01
  • This is the recommended behavior when using adviced routes. See: https://camel.apache.org/manual/advice-with.html#_enabling_advice_during_testing – Ulrich Schuster Aug 01 '22 at 07:03
  • See also http://www.masterspringboot.com/camel/camel-with-spring-boot-example-masterspringboot/ – TacheDeChoco Aug 01 '22 at 07:04

1 Answers1

2

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.

Jeremy Ross
  • 11,544
  • 3
  • 36
  • 36