Skip to content

Commit

Permalink
CamelTestSupport style of testing apache#3511
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriOndrusek committed Jul 7, 2022
1 parent 3726021 commit f9c3dec
Show file tree
Hide file tree
Showing 51 changed files with 2,996 additions and 3 deletions.
24 changes: 24 additions & 0 deletions docs/modules/ROOT/pages/user-guide/testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,27 @@ class MyTest {
----

More examples of WireMock usage can be found in the Camel Quarkus integration test source tree such as https://github.com/apache/camel-quarkus/tree/main/integration-tests/geocoder[Geocoder].

== `CamelTestSupport` style of testing

If you used plain Camel before, you may know https://camel.apache.org/components/latest/others/test-junit5.html[CamelTestSupport] already.
Unfortunately the Camel variant won't work on Quarkus and so we prepared a replacement called `CamelQuarkusTestSupport`, which can be used in JVM mode.

There are several limitations:

* Test has to be annotated by `@QuarkusTest` and has to extend `CamelQuarkusTestSupport`.
* Quarkus runs tests in a custom classloader which JUnit is not aware of (see the https://quarkus.io/guides/getting-started-testing#applying-interceptors-to-tests[documentation]). If JUnit's callback (i.e. `org.junit.jupiter.api.extension.BeforeEachCallback`) is used, it may not work as expected. Use the quarkus callbacks instead (see the https://quarkus.io/guides/getting-started-testing#enrichment-via-quarkustestcallback[documentation]) or use annotations like `@org.junit.jupiter.api.BeforeEach`.
* Camel Quarkus lifecycle does not allow to start/stop Camel context. Context is started before execution of the first test and closed after the finish of the last one. Test has to be written with consideration with this limitation. If it is not possible to write a test with such limitation, `@TestProfile` has to be used. Test profile forces quarkus to restart its engine, therefore it creates a new Camel context (see the https://quarkus.io/guides/getting-started-testing#testing_different_profiles/[documentation] about this feature).
The `CamelQuarkusTestSupport` implements `QuarkusTestProfile`, therefore the test class could be used as a value for `@TestProfile`.
* Camel Quarkus executes the production of beans during the build phase. Because all the tests are
build together, exclusion behavior is implemented into `CamelQuarkusTestSupport`. If a producer of the specific type and name is used in one tests, the instance will be the same for the rest of the tests.

[source,java]
----
@QuarkusTest
@TestProfile(SimpleTest.class) //necessary only if "newly created" context is required for the test (worse performance)
public class SimpleTest extends CamelQuarkusTestSupport {
...
}
----

Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public class InjectionPointsProcessor {
.createSimple(EndpointInject.class.getName());
private static final DotName PRODUCE_ANNOTATION = DotName
.createSimple(Produce.class.getName());
private static final DotName TEST_SUPPORT_CLASS_NAME = DotName
.createSimple("org.apache.camel.quarkus.test.CamelQuarkusTestSupport");

private static SyntheticBeanBuildItem syntheticBean(DotName name, Supplier<?> creator) {
return SyntheticBeanBuildItem.configure(name)
Expand Down Expand Up @@ -226,12 +228,16 @@ void syntheticBeans(
BuildProducer<SyntheticBeanBuildItem> syntheticBeans,
BuildProducer<NativeImageProxyDefinitionBuildItem> proxyDefinitions) {

Set<String> alreadyCreated = new HashSet<>();

for (AnnotationInstance annot : index.getIndex().getAnnotations(ENDPOINT_INJECT_ANNOTATION)) {
final AnnotationTarget target = annot.target();
switch (target.kind()) {
case FIELD: {
final FieldInfo field = target.asField();
endpointInjectBeans(recorder, syntheticBeans, index.getIndex(), annot, field.type().name());
if (!excludeTestSyntheticBeanDuplicities(annot, alreadyCreated, field.declaringClass(), index.getIndex())) {
endpointInjectBeans(recorder, syntheticBeans, index.getIndex(), annot, field.type().name());
}
break;
}
case METHOD: {
Expand All @@ -251,8 +257,10 @@ void syntheticBeans(
switch (target.kind()) {
case FIELD: {
final FieldInfo field = target.asField();
produceBeans(recorder, capabilities, syntheticBeans, proxyDefinitions, beanCapabilityAvailable,
index.getIndex(), annot, field.type().name(), field.name(), field.declaringClass().name());
if (!excludeTestSyntheticBeanDuplicities(annot, alreadyCreated, field.declaringClass(), index.getIndex())) {
produceBeans(recorder, capabilities, syntheticBeans, proxyDefinitions, beanCapabilityAvailable,
index.getIndex(), annot, field.type().name(), field.name(), field.declaringClass().name());
}
break;
}
case METHOD: {
Expand All @@ -266,6 +274,45 @@ void syntheticBeans(
}
}

private boolean excludeTestSyntheticBeanDuplicities(AnnotationInstance annot, Set<String> alreadyCreated,
ClassInfo declaringClass, IndexView index) {
String identifier = annot.toString(false) + ":" + getTargetClass(annot).toString();

if (extendsCamelQuarkusTest(declaringClass, index)) {
if (alreadyCreated.contains(identifier)) {
return true;
} else {
alreadyCreated.add(identifier);
}
}
return false;
}

private DotName getTargetClass(AnnotationInstance annot) {
switch (annot.target().kind()) {
case FIELD:
return annot.target().asField().type().name();
case METHOD:
return annot.target().asMethod().returnType().name();
default:
return null;
}
}

private boolean extendsCamelQuarkusTest(ClassInfo declaringClass, IndexView indexView) {
if (declaringClass == null) {
return false;
}

if (TEST_SUPPORT_CLASS_NAME.equals(declaringClass.name())) {
return true;
}

//iterate over parent until found CamelQuarkusTest or null
return (declaringClass.superName() != null &&
extendsCamelQuarkusTest(indexView.getClassByName(declaringClass.superName()), indexView));
}

void produceBeans(CamelRecorder recorder, List<CapabilityBuildItem> capabilities,
BuildProducer<SyntheticBeanBuildItem> syntheticBeans,
BuildProducer<NativeImageProxyDefinitionBuildItem> proxyDefinitions,
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
<module>integration-test-groups</module>
<module>docs</module>
<module>integration-tests-jvm</module>
<module>test-framework</module>
</modules>

<developers>
Expand Down Expand Up @@ -586,6 +587,10 @@
<path>integration-tests-support</path>
<artifactIdPrefix>camel-quarkus-integration-test-support-</artifactIdPrefix>
</extensionDir>
<extensionDir>
<path>test-framework</path>
<artifactIdPrefix>camel-quarkus-test-framework</artifactIdPrefix>
</extensionDir>
</extensionDirs>
</configuration>
</plugin>
Expand Down
20 changes: 20 additions & 0 deletions poms/bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-dataset</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-datasonnet</artifactId>
Expand Down Expand Up @@ -1840,6 +1845,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-directvm</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-disruptor</artifactId>
Expand Down Expand Up @@ -5307,6 +5317,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-junit5</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-threadpoolfactory-vertx</artifactId>
Expand Down Expand Up @@ -7610,6 +7625,11 @@
<artifactId>camel-quarkus-jta-deployment</artifactId>
<version>${camel-quarkus.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-junit5</artifactId>
<version>${camel-quarkus.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-kafka</artifactId>
Expand Down
20 changes: 20 additions & 0 deletions poms/bom/src/main/generated/flattened-full-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>camel-dataset</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<version>3.18.0-SNAPSHOT</version><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
</dependency>
<dependency>
<groupId>org.apache.camel</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>camel-datasonnet</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
Expand Down Expand Up @@ -1786,6 +1791,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>camel-directvm</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<version>3.18.0-SNAPSHOT</version><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
</dependency>
<dependency>
<groupId>org.apache.camel</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>camel-disruptor</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
Expand Down Expand Up @@ -5253,6 +5263,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>camel-test-junit5</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<version>3.18.0-SNAPSHOT</version><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
</dependency>
<dependency>
<groupId>org.apache.camel</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>camel-threadpoolfactory-vertx</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
Expand Down Expand Up @@ -7554,6 +7569,11 @@
<artifactId>camel-quarkus-jta-deployment</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<version>2.11.0-SNAPSHOT</version><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>camel-quarkus-junit5</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<version>2.11.0-SNAPSHOT</version><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>camel-quarkus-kafka</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
Expand Down
20 changes: 20 additions & 0 deletions poms/bom/src/main/generated/flattened-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-dataset</artifactId>
<version>3.18.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-datasonnet</artifactId>
Expand Down Expand Up @@ -1786,6 +1791,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-directvm</artifactId>
<version>3.18.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-disruptor</artifactId>
Expand Down Expand Up @@ -5253,6 +5263,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-junit5</artifactId>
<version>3.18.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-threadpoolfactory-vertx</artifactId>
Expand Down Expand Up @@ -7554,6 +7569,11 @@
<artifactId>camel-quarkus-jta-deployment</artifactId>
<version>2.11.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-junit5</artifactId>
<version>2.11.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-kafka</artifactId>
Expand Down
20 changes: 20 additions & 0 deletions poms/bom/src/main/generated/flattened-reduced-verbose-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>camel-dataset</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<version>3.18.0-SNAPSHOT</version><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
</dependency>
<dependency>
<groupId>org.apache.camel</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>camel-datasonnet</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
Expand Down Expand Up @@ -1786,6 +1791,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>camel-directvm</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<version>3.18.0-SNAPSHOT</version><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
</dependency>
<dependency>
<groupId>org.apache.camel</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>camel-disruptor</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
Expand Down Expand Up @@ -5253,6 +5263,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>camel-test-junit5</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<version>3.18.0-SNAPSHOT</version><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
</dependency>
<dependency>
<groupId>org.apache.camel</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>camel-threadpoolfactory-vertx</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
Expand Down Expand Up @@ -7554,6 +7569,11 @@
<artifactId>camel-quarkus-jta-deployment</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<version>2.11.0-SNAPSHOT</version><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>camel-quarkus-junit5</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<version>2.11.0-SNAPSHOT</version><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>camel-quarkus-kafka</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
Expand Down
67 changes: 67 additions & 0 deletions test-framework/junit5/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-test-framework</artifactId>
<version>2.11.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>camel-quarkus-junit5</artifactId>
<name>Camel Quarkus :: Test Framework :: Junit5</name>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-junit5</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-bean</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-management</artifactId>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit f9c3dec

Please sign in to comment.