This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new efc34ab CAMEL-15486 Move skipping of context start to method `beforeTestClass` (#4177) efc34ab is described below commit efc34ab2b98f3697ebb482cf1645fda6f0794797 Author: Michel Erard <michel.er...@gmail.com> AuthorDate: Thu Sep 10 10:44:57 2020 +0200 CAMEL-15486 Move skipping of context start to method `beforeTestClass` (#4177) * CAMEL-15486 Move skipping of context start to method `beforeTestClass` to have the settings earlier in the test lifecycle. * CAMEL-15486 Prevent Camel context from being started in both cases: before the class and before the test. Co-authored-by: tker7 <michel.er...@six-group.com> --- .../junit5/CamelSpringBootExecutionListener.java | 31 +++++++++++++++------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/components/camel-test-spring-junit5/src/main/java/org/apache/camel/test/spring/junit5/CamelSpringBootExecutionListener.java b/components/camel-test-spring-junit5/src/main/java/org/apache/camel/test/spring/junit5/CamelSpringBootExecutionListener.java index cfd1ba4..1b836dd 100644 --- a/components/camel-test-spring-junit5/src/main/java/org/apache/camel/test/spring/junit5/CamelSpringBootExecutionListener.java +++ b/components/camel-test-spring-junit5/src/main/java/org/apache/camel/test/spring/junit5/CamelSpringBootExecutionListener.java @@ -28,6 +28,7 @@ public class CamelSpringBootExecutionListener extends AbstractTestExecutionListe protected static ThreadLocal<ConfigurableApplicationContext> threadApplicationContext = new ThreadLocal<>(); private static final Logger LOG = LoggerFactory.getLogger(CamelSpringBootExecutionListener.class); + private static final String PROPERTY_SKIP_STARTING_CAMEL_CONTEXT = "skipStartingCamelContext"; /** * Returns the precedence that is used by Spring to choose the appropriate execution order of test listeners. @@ -40,6 +41,12 @@ public class CamelSpringBootExecutionListener extends AbstractTestExecutionListe } @Override + public void beforeTestClass(TestContext testContext) throws Exception { + // prevent other extensions to start the Camel context + preventContextStart(); + } + + @Override public void prepareTestInstance(TestContext testContext) throws Exception { LOG.info("CamelSpringBootExecutionListener preparing: {}", testContext.getTestClass()); @@ -49,13 +56,8 @@ public class CamelSpringBootExecutionListener extends AbstractTestExecutionListe CamelAnnotationsHandler.handleDisableJmx(null, testClass); CamelAnnotationsHandler.handleExcludeRoutes(null, testClass); - // we are customizing the Camel context with - // CamelAnnotationsHandler so we do not want to start it - // automatically, which would happen when SpringCamelContext - // is added to Spring ApplicationContext, so we set the flag - // not to start it just yet - SpringCamelContext.setNoStart(true); - System.setProperty("skipStartingCamelContext", "true"); + // prevent the Camel context to be started to be able to extend it. + preventContextStart(); ConfigurableApplicationContext context = (ConfigurableApplicationContext) testContext.getApplicationContext(); CamelAnnotationsHandler.handleUseOverridePropertiesWithPropertiesComponent(context, testClass); @@ -67,10 +69,21 @@ public class CamelSpringBootExecutionListener extends AbstractTestExecutionListe CamelAnnotationsHandler.handleMockEndpoints(context, testClass); CamelAnnotationsHandler.handleMockEndpointsAndSkip(context, testClass); - System.clearProperty("skipStartingCamelContext"); + System.clearProperty(PROPERTY_SKIP_STARTING_CAMEL_CONTEXT); SpringCamelContext.setNoStart(false); } + /** + * Sets the {@link SpringCamelContext#setNoStart(boolean)} and the system property + * <code>skipStartingCamelContext</code>to <code>true</code> to let us customizing the Camel context with + * {@link CamelAnnotationsHandler} before it has been started. It's needed as early as possible to prevent other + * extensions to start it <b>and</b> before every test run. + */ + private void preventContextStart() { + SpringCamelContext.setNoStart(true); + System.setProperty(PROPERTY_SKIP_STARTING_CAMEL_CONTEXT, "true"); + } + @Override public void beforeTestMethod(TestContext testContext) throws Exception { LOG.info("CamelSpringBootExecutionListener before: {}.{}", testContext.getTestClass(), @@ -83,7 +96,7 @@ public class CamelSpringBootExecutionListener extends AbstractTestExecutionListe threadApplicationContext.set(context); // mark Camel to be startable again and start Camel - System.clearProperty("skipStartingCamelContext"); + System.clearProperty(PROPERTY_SKIP_STARTING_CAMEL_CONTEXT); // route coverage need to know the test method CamelAnnotationsHandler.handleRouteCoverage(context, testClass, s -> testName);