diff --git a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java index f9471bfd455cf..e4ac89e944afd 100644 --- a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java +++ b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java @@ -698,18 +698,7 @@ private static void doStopTemplates( } protected void startCamelContext() throws Exception { - if (camelContextService != null) { - camelContextService.start(); - } else { - if (context instanceof DefaultCamelContext) { - DefaultCamelContext defaultCamelContext = (DefaultCamelContext) context; - if (!defaultCamelContext.isStarted()) { - defaultCamelContext.start(); - } - } else { - context.start(); - } - } + CamelContextTestHelper.startCamelContextOrService(context, camelContextService); } protected CamelContext createCamelContext() throws Exception { diff --git a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/CamelContextTestHelper.java b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/CamelContextTestHelper.java index ae0d6c0117cb7..af9d836a6db9c 100644 --- a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/CamelContextTestHelper.java +++ b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/CamelContextTestHelper.java @@ -23,6 +23,7 @@ import org.apache.camel.NoSuchEndpointException; import org.apache.camel.RouteConfigurationsBuilder; import org.apache.camel.RoutesBuilder; +import org.apache.camel.Service; import org.apache.camel.ServiceStatus; import org.apache.camel.component.mock.InterceptSendToMockEndpointStrategy; import org.apache.camel.component.mock.MockEndpoint; @@ -170,4 +171,33 @@ public static void configureIncludeExcludePatterns(CamelContext context, String context.getCamelContextExtension().getContextPlugin(Model.class).setRouteFilterPattern(include, exclude); } } + + /** + * Start the given context + * @param context the context to start + * @throws Exception + */ + public static void startCamelContext(CamelContext context) throws Exception { + if (context instanceof DefaultCamelContext defaultCamelContext) { + if (!defaultCamelContext.isStarted()) { + defaultCamelContext.start(); + } + } else { + context.start(); + } + } + + /** + * Starts a CamelContext or a service managing the CamelContext. The service takes priority if provided. + * @param context the context to start + * @param camelContextService the service managing the CamelContext + * @throws Exception + */ + public static void startCamelContextOrService(CamelContext context, Service camelContextService) throws Exception { + if (camelContextService != null) { + camelContextService.start(); + } else { + CamelContextTestHelper.startCamelContext(context); + } + } }