You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by al...@apache.org on 2019/08/21 16:00:42 UTC

[camel] 01/02: CAMEL-13826: Merged CamelTestSupport.isCreateCamelContextPerClass() into JUnit 5 Lifecycle.PER_CLASS

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

aldettinger pushed a commit to branch CAMEL-13826
in repository https://gitbox.apache.org/repos/asf/camel.git

commit a1727ab3860c509a96f93e72673bd54624d92f3f
Author: aldettinger <al...@gmail.com>
AuthorDate: Tue Aug 20 17:33:30 2019 +0200

    CAMEL-13826: Merged CamelTestSupport.isCreateCamelContextPerClass() into JUnit 5 Lifecycle.PER_CLASS
---
 .../src/main/docs/test-junit5.adoc                 |  3 +++
 .../apache/camel/test/junit5/CamelTestSupport.java | 25 ++++++++++++++--------
 .../CreateCamelContextPerTestFalseTest.java        |  7 ++----
 .../CreateCamelContextPerTestTrueTest.java         |  5 -----
 .../FilterCreateCamelContextPerClassTest.java      | 10 ++-------
 5 files changed, 23 insertions(+), 27 deletions(-)

diff --git a/components/camel-test-junit5/src/main/docs/test-junit5.adoc b/components/camel-test-junit5/src/main/docs/test-junit5.adoc
index 0ae295f..f31ff75 100644
--- a/components/camel-test-junit5/src/main/docs/test-junit5.adoc
+++ b/components/camel-test-junit5/src/main/docs/test-junit5.adoc
@@ -62,6 +62,9 @@ Projects using `camel-test` would need to use `camel-test-junit5`. For instance,
 * `TestSupport` static methods should be imported where needed, for instance `import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf`
 * Usage of the field `CamelTestSupport.log` should be replaced by another logger, for instance `org.slf4j.LoggerFactory.getLogger(MyCamelTest.class);`
 * Usage of the method `CamelTestSupport.createRegistry` should be replaced by `CamelTestSupport.createCamelRegistry()`
+* Overrides of `isCreateCamelContextPerClass()` returning `false` should be removed
+* Overrides of `isCreateCamelContextPerClass()` returning `true` should be replaced by `@TestInstance(Lifecycle.PER_CLASS)`
+
 
 === Typical migration steps linked to JUnit 5 itself
 Once Camel related steps have been performed, there are still typical JUnit 5 migration steps to remember:
diff --git a/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java b/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java
index 1818a98..083f3fc 100644
--- a/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java
+++ b/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java
@@ -66,8 +66,10 @@ import org.apache.camel.util.TimeUtils;
 import org.apache.camel.util.URISupport;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestInstance.Lifecycle;
 import org.junit.jupiter.api.extension.AfterAllCallback;
 import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
 import org.junit.jupiter.api.extension.BeforeEachCallback;
 import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
 import org.junit.jupiter.api.extension.ExtensionContext;
@@ -82,7 +84,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
  * with some routes along with a {@link org.apache.camel.ProducerTemplate} for
  * use in the test case Do <tt>not</tt> use this class for Spring Boot testing.
  */
-public abstract class CamelTestSupport implements BeforeEachCallback, AfterAllCallback, BeforeTestExecutionCallback, AfterTestExecutionCallback {
+public abstract class CamelTestSupport implements BeforeEachCallback, AfterAllCallback, BeforeAllCallback, BeforeTestExecutionCallback, AfterTestExecutionCallback {
 
     /**
      * JVM system property which can be set to true to turn on dumping route
@@ -112,6 +114,7 @@ public abstract class CamelTestSupport implements BeforeEachCallback, AfterAllCa
     private static final ThreadLocal<AtomicInteger> TESTS = new ThreadLocal<>();
     private static final ThreadLocal<CamelTestSupport> INSTANCE = new ThreadLocal<>();
     private String currentTestName;
+    private boolean isCreateCamelContextPerClass = false;
     private CamelRouteCoverageDumper routeCoverageDumper = new CamelRouteCoverageDumper();
     // CHECKSTYLE:ON
 
@@ -135,6 +138,11 @@ public abstract class CamelTestSupport implements BeforeEachCallback, AfterAllCa
     }
 
     @Override
+    public void beforeAll(ExtensionContext context) {
+        isCreateCamelContextPerClass = context.getTestInstanceLifecycle().filter(lc -> lc.equals(Lifecycle.PER_CLASS)).isPresent();
+    }
+
+    @Override
     public void afterAll(ExtensionContext context) {
         CamelTestSupport support = INSTANCE.get();
         if (support != null && support.isCreateCamelContextPerClass()) {
@@ -197,12 +205,11 @@ public abstract class CamelTestSupport implements BeforeEachCallback, AfterAllCa
     }
 
     /**
-     * Override to control whether {@link CamelContext} should be setup per test
-     * or per class.
+     * Tells whether {@link CamelContext} should be setup per test or per class.
      * <p/>
-     * By default it will be setup/teardown per test (per test method). If you
-     * want to re-use {@link CamelContext} between test methods you can override
-     * this method and return <tt>true</tt>
+     * By default it will be setup/teardown per test method. This method returns
+     * <code>true</code> when the camel test class is annotated
+     * with @TestInstance(TestInstance.Lifecycle.PER_CLASS).
      * <p/>
      * <b>Important:</b> Use this with care as the {@link CamelContext} will
      * carry over state from previous tests, such as endpoints, components etc.
@@ -214,8 +221,8 @@ public abstract class CamelTestSupport implements BeforeEachCallback, AfterAllCa
      *
      * @return <tt>true</tt> per class, <tt>false</tt> per test.
      */
-    public boolean isCreateCamelContextPerClass() {
-        return false;
+    public final boolean isCreateCamelContextPerClass() {
+        return isCreateCamelContextPerClass;
     }
 
     /**
@@ -539,7 +546,7 @@ public abstract class CamelTestSupport implements BeforeEachCallback, AfterAllCa
         LOG.info("********************************************************************************");
 
         if (isCreateCamelContextPerClass()) {
-            // will tear down test specially in CamelTearDownRule
+            // will tear down test specially in afterAll callback
         } else {
             LOG.debug("tearDown()");
             doStopTemplates(consumer, template, fluentTemplate);
diff --git a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/CreateCamelContextPerTestFalseTest.java b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/CreateCamelContextPerTestFalseTest.java
index 34c8591..149edb9 100644
--- a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/CreateCamelContextPerTestFalseTest.java
+++ b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/CreateCamelContextPerTestFalseTest.java
@@ -27,12 +27,14 @@ import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.junit5.CamelTestSupport;
 import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
+@TestInstance(TestInstance.Lifecycle.PER_METHOD)
 public class CreateCamelContextPerTestFalseTest extends CamelTestSupport {
 
     private static final Logger LOG = LoggerFactory.getLogger(CreateCamelContextPerTestFalseTest.class);
@@ -47,11 +49,6 @@ public class CreateCamelContextPerTestFalseTest extends CamelTestSupport {
     protected ProducerTemplate template;
 
     @Override
-    public boolean isCreateCamelContextPerClass() {
-        return false;
-    }
-
-    @Override
     protected CamelContext createCamelContext() throws Exception {
         LOG.info("createCamelContext()");
         CREATED_CONTEXTS.incrementAndGet();
diff --git a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/CreateCamelContextPerTestTrueTest.java b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/CreateCamelContextPerTestTrueTest.java
index a234455..222fe62 100644
--- a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/CreateCamelContextPerTestTrueTest.java
+++ b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/CreateCamelContextPerTestTrueTest.java
@@ -52,11 +52,6 @@ public class CreateCamelContextPerTestTrueTest extends CamelTestSupport {
     protected ProducerTemplate template;
 
     @Override
-    public boolean isCreateCamelContextPerClass() {
-        return true;
-    }
-
-    @Override
     protected CamelContext createCamelContext() throws Exception {
         LOG.info("createCamelContext()");
         CREATED_CONTEXTS.incrementAndGet();
diff --git a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/FilterCreateCamelContextPerClassTest.java b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/FilterCreateCamelContextPerClassTest.java
index 0f5be29..be38b86 100644
--- a/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/FilterCreateCamelContextPerClassTest.java
+++ b/components/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/FilterCreateCamelContextPerClassTest.java
@@ -19,21 +19,15 @@ package org.apache.camel.test.junit5.patterns;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.test.junit5.CamelTestSupport;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
 
 /**
  * Tests filtering using Camel Test
  */
 // START SNIPPET: example
+@TestInstance(TestInstance.Lifecycle.PER_CLASS)
 public class FilterCreateCamelContextPerClassTest extends CamelTestSupport {
 
-    @Override
-    public boolean isCreateCamelContextPerClass() {
-        // we override this method and return true, to tell Camel test-kit that
-        // it should only create CamelContext once (per class), so we will
-        // re-use the CamelContext between each test method in this class
-        return true;
-    }
-
     @Test
     public void testSendMatchingMessage() throws Exception {
         String expectedBody = "<matched/>";