How to isolate subsequent Camel Spring-Boot test suites

78 Views Asked by At

I want to test my Spring-Boot/Camel application (Camel 4.1, Spring-Boot 3.1.5, JUnit5), which includes a Kafka consumer, or a producer, or both. I have written several JUnit test suites. When I run these test suites in succession, I see that several Camel contexts are being started, and that the Kafka consumer (clientId=BusinessEventConsumer) seems to be started multiple times:

// First test suite
INFO  AbstractCamelContext:2415 - Apache Camel 4.1.0 (camel-1) is starting
...
// Second test suite
INFO  AbstractCamelContext:2415 - Apache Camel 4.1.0 (camel-2) is starting
WARN  AppInfoParser:68 - Error registering AppInfo mbean
javax.management.InstanceAlreadyExistsException: kafka.producer:type=app-info,id=BusinessEventConsumer
...
etc.

I suppose that these multiple Kafka consumers are the reason for errors I encounter in test cases. The same holds true for producers.

How can I isolate my test suites so that Camel Kafka components are not started multiple times?

My test classes are annotated like this (Kotlin Code):

@CamelSpringBootTest
@SpringBootTest(
    classes = [TestApplication::class],
    properties = ["camel.springboot.java-routes-include-pattern=**/TestEventProducerRoute, **/BusinessEventSubscriberRoute"]
)
@Import(TestEnvironmentConfiguration::class)
@DisableJmx(true)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
internal class OutboundDirectTest(...){..}

Edit: When looking at running and waiting threads during the execution of my tests, I see that both the first and the second test suite have RUNNING consumer threads and WAITING producer threads, like so:

camel-1 thread #1 - KafkaConsumer in group "main": RUNNING
camel-1 thread #2 - KafkaProducer in group "main": WAITING
camel-1 thread #3 - KafkaProducer in group "main": WAITING
...
camel-2 thread #10 - KafkaConsumer in group "main": RUNNING
camel-2 thread #11 - KafkaProducer in group "main": WAITING
...

Why is this? I would assume that the Spring/Camel context related to the first test class will be shut down completely, including all Kafka consumer and producer threads, before the second test context is started (I am not running tests in parallel). Plus, why is there a separate KafkaProducer thread created for each test inside a test class?

How can I ensure that the camel context pertaining to one test class is shut down completely before a new test context is launched?

0

There are 0 best solutions below