You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/02/12 08:47:21 UTC
[camel-examples] branch main updated: CAMEL-17633: Migrate the CDI tests to JUnit 5 (#72)
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-examples.git
The following commit(s) were added to refs/heads/main by this push:
new addc433 CAMEL-17633: Migrate the CDI tests to JUnit 5 (#72)
addc433 is described below
commit addc433723a548a2c2854c981763eeed65898510
Author: Nicolas Filotto <es...@users.noreply.github.com>
AuthorDate: Sat Feb 12 09:47:15 2022 +0100
CAMEL-17633: Migrate the CDI tests to JUnit 5 (#72)
---
examples/cdi-test/README.adoc | 17 ++++--
examples/cdi-test/pom.xml | 15 ++---
.../apache/camel/example/cdi/test/AdviceTest.java | 66 ++++++++++++++++++++++
.../camel/example/cdi/test/AlternativeTest.java | 12 ++--
.../example/cdi/test/ApplicationScopedTest.java | 14 ++---
.../camel/example/cdi/test/CustomContextTest.java | 18 +++---
.../apache/camel/example/cdi/test/OrderTest.java | 23 ++++----
examples/cdi/pom.xml | 2 +-
.../org/apache/camel/example/cdi/CamelCdiTest.java | 18 +++---
9 files changed, 130 insertions(+), 55 deletions(-)
diff --git a/examples/cdi-test/README.adoc b/examples/cdi-test/README.adoc
index 6f98b26..4cf424a 100644
--- a/examples/cdi-test/README.adoc
+++ b/examples/cdi-test/README.adoc
@@ -10,7 +10,7 @@ uses JBoss Weld as the minimal CDI container to run the application,
though you can run the application in any CDI compliant container.
This example comes with a series of test classes that each demonstrates
-particular features provided by the `+camel-test-cdi+` module:
+particular features provided by the `+camel-test-cdi-junit5+` module:
[width="100%",cols="33%,67%",options="header",]
|===
@@ -23,10 +23,13 @@ particular features provided by the `+camel-test-cdi+` module:
|A stateful `+@ApplicationScoped+` test class
|link:src/test/java/org/apache/camel/example/cdi/test/CustomContextTest.java[`+CustomContextTest+`]
-|Declares a custom Camel context bean for test purpose
+|Declares a custom Camel context bean for testing purpose
|link:src/test/java/org/apache/camel/example/cdi/test/OrderTest.java[`+OrderTest+`]
-|Orders the test methods execution with `+@Order+`
+|Orders the test methods' execution with `+@Order+`. Note that in this test, an old JUnit 4 Rule of type Verifier is used such that `junit-jupiter-migrationsupport` needs to be explicitly added to the test dependencies of the project, see https://junit.org/junit5/docs/current/user-guide/#migrating-from-junit4-rule-support[Limited JUnit 4 Rule Support] for more details.
+
+|link:src/test/java/org/apache/camel/example/cdi/test/AdviceTest.java[`+AdviceTest+`]
+|Simple example that shows how to advice a route
|===
=== Build
@@ -51,7 +54,7 @@ When the Camel application runs, you should see the following message
being logged to the console, e.g.:
....
-2016-03-04 17:54:04,147 [cdi.Main.main()] INFO route - Hello from camel-test-cdi
+2016-03-04 17:54:04,147 [cdi.Main.main()] INFO route - Hello from camel-1
....
The Camel application can be stopped pressing ctrl+c in the shell.
@@ -59,9 +62,13 @@ The Camel application can be stopped pressing ctrl+c in the shell.
You should see the following message being logged to the console:
....
-2016-03-04 17:54:18,725 [Thread-1 ] INFO route - Bye from camel-test-cdi
+2016-03-04 17:54:18,725 [Thread-1 ] INFO route - Bye from camel-1
....
+NOTE: The name `camel-1` is automatically generated by the default name strategy.
+In case, a custom Camel context is defined like in the example link:src/test/java/org/apache/camel/example/cdi/test/CustomContextTest.java[`+CustomContextTest+`],
+the value of the annotation `@Named` is used instead as name of the Camel context.
+
=== Run unit tests
There are unit tests implemented, which can be run with the following command:
diff --git a/examples/cdi-test/pom.xml b/examples/cdi-test/pom.xml
index ce1b8ad..69985c8 100644
--- a/examples/cdi-test/pom.xml
+++ b/examples/cdi-test/pom.xml
@@ -93,14 +93,15 @@
<!-- test -->
<dependency>
<groupId>org.apache.camel</groupId>
- <artifactId>camel-test-cdi</artifactId>
+ <artifactId>camel-test-cdi-junit5</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <!-- Needed to support JUnit 4 Rules like Verifier -->
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-migrationsupport</artifactId>
+ <version>${junit-jupiter-version}</version>
<scope>test</scope>
- <exclusions>
- <exclusion>
- <artifactId>hamcrest-core</artifactId>
- <groupId>org.hamcrest</groupId>
- </exclusion>
- </exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
diff --git a/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/AdviceTest.java b/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/AdviceTest.java
new file mode 100644
index 0000000..39241f6
--- /dev/null
+++ b/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/AdviceTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.example.cdi.test;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.AdviceWithRouteBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.model.ModelCamelContext;
+import org.apache.camel.test.cdi.AdviceRoute;
+import org.apache.camel.test.cdi.CamelCdiExtension;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+@ExtendWith(CamelCdiExtension.class)
+class AdviceTest {
+
+ @EndpointInject("mock:test")
+ MockEndpoint mock;
+
+ @Produce("direct:in")
+ ProducerTemplate template;
+
+ @Test
+ void testSendMessage(ModelCamelContext context) throws Exception {
+ assertNotNull(context);
+ String expectedBody = "Camel Rocks";
+
+ mock.expectedBodiesReceived(expectedBody);
+
+ template.sendBody(expectedBody);
+
+ mock.assertIsSatisfied();
+ }
+
+ /**
+ * This class indicates the {@link CamelCdiExtension} to replace the endpoint {@code to("direct:out")}
+ * with the endpoint {@code to("mock:test")} in the route <i>in-to-out</i> defined in {@link Application}.
+ */
+ @AdviceRoute("in-to-out")
+ static class TestBuilder extends AdviceWithRouteBuilder {
+
+ @Override
+ public void configure() throws Exception {
+ weaveByToUri("direct:out").replace().to("mock:test");
+ }
+ }
+}
diff --git a/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/AlternativeTest.java b/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/AlternativeTest.java
index cb871d5..31e68ff 100644
--- a/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/AlternativeTest.java
+++ b/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/AlternativeTest.java
@@ -24,18 +24,18 @@ import org.apache.camel.ProducerTemplate;
import org.apache.camel.cdi.Uri;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.cdi.Beans;
-import org.apache.camel.test.cdi.CamelCdiRunner;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.apache.camel.test.cdi.CamelCdiExtension;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied;
-@RunWith(CamelCdiRunner.class)
+@ExtendWith(CamelCdiExtension.class)
@Beans(
alternatives = AlternativeBean.class,
classes = TestRoute.class
)
-public class AlternativeTest {
+class AlternativeTest {
@Inject
@Uri("mock:out")
@@ -46,7 +46,7 @@ public class AlternativeTest {
ProducerTemplate producer;
@Test
- public void testAlternativeBean() throws InterruptedException {
+ void testAlternativeBean() throws InterruptedException {
mock.expectedMessageCount(1);
mock.expectedBodiesReceived("test with alternative bean!");
diff --git a/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/ApplicationScopedTest.java b/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/ApplicationScopedTest.java
index 05115d8..611342d 100644
--- a/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/ApplicationScopedTest.java
+++ b/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/ApplicationScopedTest.java
@@ -28,16 +28,16 @@ import org.apache.camel.ProducerTemplate;
import org.apache.camel.cdi.Uri;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.cdi.Beans;
-import org.apache.camel.test.cdi.CamelCdiRunner;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.apache.camel.test.cdi.CamelCdiExtension;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied;
@ApplicationScoped
-@RunWith(CamelCdiRunner.class)
+@ExtendWith(CamelCdiExtension.class)
@Beans(classes = TestRoute.class)
-public class ApplicationScopedTest {
+class ApplicationScopedTest {
private static final AtomicInteger COUNTER = new AtomicInteger();
@@ -53,12 +53,12 @@ public class ApplicationScopedTest {
}
@Test
- public void testOne(@Uri("mock:out") MockEndpoint mock) throws InterruptedException {
+ void testOne(@Uri("mock:out") MockEndpoint mock) throws InterruptedException {
assertIsSatisfied(1L, TimeUnit.SECONDS, mock);
}
@Test
- public void testTwo(@Uri("mock:out") MockEndpoint mock) throws InterruptedException {
+ void testTwo(@Uri("mock:out") MockEndpoint mock) throws InterruptedException {
assertIsSatisfied(1L, TimeUnit.SECONDS, mock);
}
}
diff --git a/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/CustomContextTest.java b/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/CustomContextTest.java
index c0acd09..0709e51 100644
--- a/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/CustomContextTest.java
+++ b/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/CustomContextTest.java
@@ -24,22 +24,22 @@ import javax.inject.Named;
import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.engine.DefaultManagementStrategy;
-import org.apache.camel.test.cdi.CamelCdiRunner;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.apache.camel.test.cdi.CamelCdiExtension;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
-@RunWith(CamelCdiRunner.class)
-public class CustomContextTest {
+@ExtendWith(CamelCdiExtension.class)
+class CustomContextTest {
@Test
- public void testCustomContext(CamelContext context) {
- assertEquals("Management strategy is incorrect!", context.getManagementStrategy().getClass(), DefaultManagementStrategy.class);
+ void testCustomContext(CamelContext context) {
+ assertEquals(DefaultManagementStrategy.class, context.getManagementStrategy().getClass(), "Management strategy is incorrect!");
}
@Default
- @Named("camel-test-cdi")
+ @Named("camel-test-cdi-junit5")
@ApplicationScoped
static class CustomCamelContext extends DefaultCamelContext {
diff --git a/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/OrderTest.java b/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/OrderTest.java
index f2dccfb..944c5bd 100644
--- a/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/OrderTest.java
+++ b/examples/cdi-test/src/test/java/org/apache/camel/example/cdi/test/OrderTest.java
@@ -26,18 +26,21 @@ import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.cdi.Uri;
-import org.apache.camel.test.cdi.CamelCdiRunner;
-import org.apache.camel.test.cdi.Order;
+import org.apache.camel.test.cdi.CamelCdiExtension;
import org.junit.ClassRule;
-import org.junit.Test;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.rules.Verifier;
-import org.junit.runner.RunWith;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
-import static org.junit.Assert.assertThat;
-@RunWith(CamelCdiRunner.class)
-public class OrderTest {
+@ExtendWith(CamelCdiExtension.class)
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+class OrderTest {
@ClassRule
public static MessageVerifier verifier = new MessageVerifier();
@@ -48,19 +51,19 @@ public class OrderTest {
@Test
@Order(2)
- public void sendMessageTwo() {
+ void sendMessageTwo() {
producer.sendBody("two");
}
@Test
@Order(3)
- public void sendMessageThree() {
+ void sendMessageThree() {
producer.sendBody("three");
}
@Test
@Order(1)
- public void sendMessageOne() {
+ void sendMessageOne() {
producer.sendBody("one");
}
diff --git a/examples/cdi/pom.xml b/examples/cdi/pom.xml
index 6407779..69fd273 100644
--- a/examples/cdi/pom.xml
+++ b/examples/cdi/pom.xml
@@ -72,7 +72,7 @@
<!-- for testing -->
<dependency>
<groupId>org.apache.camel</groupId>
- <artifactId>camel-test-cdi</artifactId>
+ <artifactId>camel-test-cdi-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
diff --git a/examples/cdi/src/test/java/org/apache/camel/example/cdi/CamelCdiTest.java b/examples/cdi/src/test/java/org/apache/camel/example/cdi/CamelCdiTest.java
index b914428..09eb64e 100644
--- a/examples/cdi/src/test/java/org/apache/camel/example/cdi/CamelCdiTest.java
+++ b/examples/cdi/src/test/java/org/apache/camel/example/cdi/CamelCdiTest.java
@@ -18,31 +18,29 @@ package org.apache.camel.example.cdi;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.NotifyBuilder;
-import org.apache.camel.test.cdi.CamelCdiRunner;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.apache.camel.test.cdi.CamelCdiExtension;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import javax.inject.Inject;
import java.util.concurrent.TimeUnit;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* A unit test checking that Camel can be configured using CDI.
*/
-@RunWith(CamelCdiRunner.class)
-public class CamelCdiTest {
+@ExtendWith(CamelCdiExtension.class)
+class CamelCdiTest {
@Inject
CamelContext context;
@Test
- public void should_be_configured_using_cdi() {
+ void should_be_configured_using_cdi() {
NotifyBuilder notify = new NotifyBuilder(context).whenCompleted(1)
.whenAllDoneMatches(exchange -> exchange.getIn().getBody(String.class).startsWith("Saying Hello World"))
.create();
- assertTrue(
- "1 message should be completed", notify.matches(20, TimeUnit.SECONDS)
- );
+ assertTrue(notify.matches(20, TimeUnit.SECONDS), "1 message should be completed");
}
}