This is an automated email from the ASF dual-hosted git repository.

fmariani pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 67bbce9f0034 Fix CamelContext not stopped with 
@TestInstance(Lifecycle.PER_CLASS)
67bbce9f0034 is described below

commit 67bbce9f00349f890c83d8346712223a90c7f7f0
Author: Croway <[email protected]>
AuthorDate: Mon Mar 2 14:36:42 2026 +0100

    Fix CamelContext not stopped with @TestInstance(Lifecycle.PER_CLASS)
---
 .../camel/test/junit5/ContextManagerExtension.java |  1 +
 .../test/junit5/ContextNotClosedPerClassTest.java  | 62 ++++++++++++++++++++++
 .../camel/test/junit6/ContextManagerExtension.java |  1 +
 3 files changed, 64 insertions(+)

diff --git 
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/ContextManagerExtension.java
 
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/ContextManagerExtension.java
index 77ce67c17b48..42bb86b6e3bf 100644
--- 
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/ContextManagerExtension.java
+++ 
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/ContextManagerExtension.java
@@ -75,6 +75,7 @@ public final class ContextManagerExtension
     @Override
     public void afterAll(ExtensionContext context) throws Exception {
         contextManager.stop();
+        contextManager.close();
     }
 
     @Override
diff --git 
a/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/ContextNotClosedPerClassTest.java
 
b/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/ContextNotClosedPerClassTest.java
new file mode 100644
index 000000000000..4f6bcd4e0f67
--- /dev/null
+++ 
b/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/ContextNotClosedPerClassTest.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.test.junit5;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.api.extension.AfterAllCallback;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Reproduces a bug where the CamelContext is never stopped when using {@code 
@TestInstance(Lifecycle.PER_CLASS)}.
+ * <p>
+ * {@link ContextManagerExtension#afterAll} only calls {@link 
CamelContextManager#stop()}, but
+ * {@link LegacyCamelContextManager#stop()} is a NO-OP. The actual shutdown 
logic lives in
+ * {@link LegacyCamelContextManager#close()}, which is never invoked.
+ */
+@TestInstance(TestInstance.Lifecycle.PER_CLASS)
+class ContextNotClosedPerClassTest extends CamelTestSupport {
+
+    // @Order(0) ensures this runs AFTER ContextManagerExtension(@Order(1))'s 
afterAll,
+    // because "after" callbacks execute in reverse registration order.
+    @RegisterExtension
+    @Order(0)
+    AfterAllCallback contextStopVerifier = extensionContext -> {
+        assertTrue(context.isStopped(),
+                "CamelContext should be stopped after all tests completed, but 
status is: " + context.getStatus());
+    };
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:start").to("mock:result");
+            }
+        };
+    }
+
+    @Test
+    void testRouteWorks() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+        template.sendBody("direct:start", "Hello");
+        getMockEndpoint("mock:result").assertIsSatisfied();
+    }
+}
diff --git 
a/components/camel-test/camel-test-junit6/src/main/java/org/apache/camel/test/junit6/ContextManagerExtension.java
 
b/components/camel-test/camel-test-junit6/src/main/java/org/apache/camel/test/junit6/ContextManagerExtension.java
index b0dcfd0dd82f..ecb073f42faa 100644
--- 
a/components/camel-test/camel-test-junit6/src/main/java/org/apache/camel/test/junit6/ContextManagerExtension.java
+++ 
b/components/camel-test/camel-test-junit6/src/main/java/org/apache/camel/test/junit6/ContextManagerExtension.java
@@ -75,6 +75,7 @@ public final class ContextManagerExtension
     @Override
     public void afterAll(ExtensionContext context) throws Exception {
         contextManager.stop();
+        contextManager.close();
     }
 
     @Override

Reply via email to