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.git
The following commit(s) were added to refs/heads/main by this push:
new 69e6510 Polish and cleanup documentation
69e6510 is described below
commit 69e6510caa15977e4d50c80b8f2a37fb63cce15c
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Aug 26 15:11:09 2021 +0200
Polish and cleanup documentation
---
.../src/main/docs/test-junit5.adoc | 17 +-
.../src/main/docs/test-spring-junit5.adoc | 75 ++--
.../src/main/docs/test-spring.adoc | 406 +--------------------
.../camel-test/camel-test/src/main/docs/test.adoc | 2 +
.../modules/others/pages/test-junit5.adoc | 17 +-
.../modules/others/pages/test-spring-junit5.adoc | 75 ++--
.../modules/others/pages/test-spring.adoc | 406 +--------------------
docs/components/modules/others/pages/test.adoc | 2 +
8 files changed, 108 insertions(+), 892 deletions(-)
diff --git
a/components/camel-test/camel-test-junit5/src/main/docs/test-junit5.adoc
b/components/camel-test/camel-test-junit5/src/main/docs/test-junit5.adoc
index dac0b78..f6ec5db 100644
--- a/components/camel-test/camel-test-junit5/src/main/docs/test-junit5.adoc
+++ b/components/camel-test/camel-test-junit5/src/main/docs/test-junit5.adoc
@@ -53,10 +53,12 @@ public class SimpleMockTest extends CamelTestSupport {
----
== Migrating Camel Tests from JUnit 4 to JUnit 5
+
Find below some hints to help in migrating camel tests from JUnit 4 to JUnit 5.
-=== Referencing the Camel Test JUnit5 library in your project
-Projects using `camel-test` would need to use `camel-test-junit5`. For
instance, maven users would update their pom.xml file as below:
+Projects using `camel-test` would need to use `camel-test-junit5`. For
instance, maven users would update their `pom.xml` file as below:
+
+[source,xml]
----
<dependency>
<groupId>org.apache.camel</groupId>
@@ -65,10 +67,11 @@ Projects using `camel-test` would need to use
`camel-test-junit5`. For instance,
</dependency>
----
-Tips: It's possible to run JUnit4 & JUnit5 based camel tests side by side
including the following dependencies `camel-test`,
-`camel-test-junit5` and `junit-vintage-engine`. This configuration allows to
migrate a camel test at once.
+TIP: It's possible to run JUnit4 & JUnit5 based Camel tests side by side
including the following dependencies `camel-test`,
+`camel-test-junit5` and `junit-vintage-engine`. This configuration allows
migrating Camel tests one by one.
+
+=== Migration Steps
-=== Typical migration steps linked to JUnit 5 support in Camel Test
* Imports of `org.apache.camel.test.junit4.\*` should be replaced with
`org.apache.camel.test.junit5.*`
* `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);`
@@ -76,8 +79,6 @@ Tips: It's possible to run JUnit4 & JUnit5 based camel tests
side by side includ
* 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:
* New JUnit 5 assertions should be imported where needed, for instance `import
static org.junit.jupiter.api.Assertions.assertEquals`
@@ -90,4 +91,4 @@ Once Camel related steps have been performed, there are still
typical JUnit 5 mi
* `org.junit.AfterClass` should be changed in favor of `import
org.junit.jupiter.api.AfterAll`
* Built-in `assertThat` from third-party assertion libraries should be used.
For instance, use `org.hamcrest.MatcherAssert.assertThat` from `java-hamcrest`
-Depending on the context, more involved tips might be needed, please check the
https://junit.org/junit5/docs/current/user-guide/[JUnit 5 User Guide] then.
+Depending on the context, more involved tips might be needed, please check the
https://junit.org/junit5/docs/current/user-guide/[JUnit 5 User Guide].
diff --git
a/components/camel-test/camel-test-spring-junit5/src/main/docs/test-spring-junit5.adoc
b/components/camel-test/camel-test-spring-junit5/src/main/docs/test-spring-junit5.adoc
index 122635f..9ae2670 100644
---
a/components/camel-test/camel-test-spring-junit5/src/main/docs/test-spring-junit5.adoc
+++
b/components/camel-test/camel-test-spring-junit5/src/main/docs/test-spring-junit5.adoc
@@ -9,14 +9,15 @@
include::{cq-version}@camel-quarkus:ROOT:partial$reference/others/test-spring-ju
*Since Camel {since}*
-The `camel-test-spring-junit5` module makes it possible to test Camel Spring
based applications with JUnit 5.
+The `camel-test-spring-junit5` module is used for testing Camel with Spring;
+both the classic Spring XML files or Spring Boot.
-Testing is a crucial part of any development or integration work. The Spring
Framework offers a number of features that makes it easy to test while using
Spring for Inversion of Control.
+== Testing classic Spring XML
There are multiple approaches to test Camel Spring 5.x based routes with JUnit
5.
-
-== Extending the CamelSpringTestSupport class
An approach is to extend
`org.apache.camel.test.spring.junit5.CamelSpringTestSupport`, for instance:
+
+[source,java]
----
public class SimpleMockTest extends CamelSpringTestSupport {
@@ -28,6 +29,7 @@ public class SimpleMockTest extends CamelSpringTestSupport {
@Override
protected AbstractApplicationContext createApplicationContext() {
+ // loads a Spring XML file
return new
ClassPathXmlApplicationContext("org/apache/camel/test/patterns/SimpleMockTest.xml");
}
@@ -41,17 +43,30 @@ public class SimpleMockTest extends CamelSpringTestSupport {
}
----
-This approach provides feature parity with
`org.apache.camel.test.junit5.CamelTestSupport` from
xref:test-junit5.adoc[camel-test-junit5] but does not support Spring
annotations on the test class such as `@Autowired`, `@DirtiesContext`, and
`@ContextConfiguration`.
+The example above is loading a classic Spring XML file (has `<beans>` as root
tag).
+
+This approach provides feature parity with
`org.apache.camel.test.junit5.CamelTestSupport`
+from xref:components:others:test-junit5.adoc[camel-test-junit5] but does not
support Spring annotations
+on the test class such as `@Autowired`, `@DirtiesContext`, and
`@ContextConfiguration`.
+
+Instead of instantiating the `CamelContext` and routes programmatically,
+this class relies on a Spring context to wire the needed components together.
+If your test extends this class, you must provide the Spring context by
implementing the following method:
-Instead of instantiating the `CamelContext` and routes programmatically, this
class relies on a Spring context to wire the needed components together. If
your test extends this class, you must provide the Spring context by
implementing the following method.
[source,java]
----
protected abstract AbstractApplicationContext createApplicationContext();
----
-== Using the @CamelSpringTest annotation
-Another approach involves the usage of the
`org.apache.camel.test.spring.junit5.CamelSpringTest` annotation, for instance:
+=== Using the @CamelSpringTest annotation
+
+A better and recommended approach involves the usage of the
+`org.apache.camel.test.spring.junit5.CamelSpringTest` annotation, as shown:
+
+[source,java]
----
+package com.foo;
+
@CamelSpringTest
@ContextConfiguration
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@@ -80,15 +95,15 @@ public class CamelSpringPlainTest {
MockEndpoint.assertIsSatisfied(camelContext);
}
-
- @Test
- public void testAnotherCase() throws Exception {
- ...
- }
}
----
-The above test will load the route from
`org/apache/camel/test/spring/CamelSpringPlainTest-context.xml` which looks
like below:
+The above test will by default load a Spring XML file using the naming pattern
_className_-context.xml, which means
+the example above loads the file `com/foo/CamelSpringPlainTest-context.xml`.
+
+This XML file is Spring XML file as shown:
+
+[source,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
@@ -97,27 +112,16 @@ The above test will load the route from
`org/apache/camel/test/spring/CamelSprin
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
- <camelContext id="camelContext"
- xmlns="http://camel.apache.org/schema/spring" trace="true"
- autoStartup="true">
- <packageScan>
- <package>org.apache.camel.test.spring</package>
- </packageScan>
+ <camelContext id="camelContext"
xmlns="http://camel.apache.org/schema/spring">
<route>
- <from uri="direct:start" />
- <to uri="mock:a" />
+ <from uri="direct:start"/>
+ <to uri="mock:a"/>
<transform>
<simple>Hello ${body}</simple>
</transform>
- <to uri="mock:b" />
+ <to uri="mock:b"/>
</route>
</camelContext>
-
- <bean id="bridgePropertyPlaceholder"
-
class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
- <property name="location"
-
value="classpath:org/apache/camel/test/spring/test.properties" />
- </bean>
</beans>
----
@@ -125,10 +129,12 @@ This approach supports both Camel and Spring annotations,
such as `@Autowired`,
However, it does NOT have feature parity with
`org.apache.camel.test.junit5.CamelTestSupport`.
== Migrating Camel Spring Tests from JUnit 4 to JUnit 5
+
Find below some hints to help in migrating Camel Spring tests from JUnit 4 to
JUnit 5.
-=== Referencing the Camel Test Spring JUnit 5 library in your project
Projects using `camel-test-spring` would need to use
`camel-test-spring-junit5`. For instance, maven users would update their
pom.xml file as below:
+
+[source,xml]
----
<dependency>
<groupId>org.apache.camel</groupId>
@@ -137,11 +143,12 @@ Projects using `camel-test-spring` would need to use
`camel-test-spring-junit5`.
</dependency>
----
-Tips: It's possible to run JUnit 4 & JUnit 5 based Camel Spring tests side by
side including the following dependencies `camel-test-spring`,
-`camel-test-spring-junit5` and `junit-vintage-engine`. This configuration
allows to migrate a Camel Spring test at once.
+TIP: It's possible to run JUnit 4 & JUnit 5 based Camel Spring tests side by
side including the following dependencies `camel-test-spring`,
+`camel-test-spring-junit5` and `junit-vintage-engine`. This configuration
allows migrating Camel tests one by one.
+
+=== Migration steps
-=== Typical migration steps linked to JUnit 5 support in Camel Test Spring
-* Migration steps linked to xref:test-junit5.adoc[JUnit 5 support in Camel
Test itself] should have been applied first
+* Migration steps from
xref:components:others:test-junit5.adoc[camel-test-junit5] should have been
applied first
* Imports of `org.apache.camel.test.spring.\*` should be replaced with
`org.apache.camel.test.spring.junit5.*`
* Usage of `@RunWith(CamelSpringRunner.class)` should be replaced with
`@CamelSpringTest`
* Usage of `@BootstrapWith(CamelTestContextBootstrapper.class)` should be
replaced with `@CamelSpringTest`
diff --git
a/components/camel-test/camel-test-spring/src/main/docs/test-spring.adoc
b/components/camel-test/camel-test-spring/src/main/docs/test-spring.adoc
index 6997df8..e015f7d 100644
--- a/components/camel-test/camel-test-spring/src/main/docs/test-spring.adoc
+++ b/components/camel-test/camel-test-spring/src/main/docs/test-spring.adoc
@@ -10,409 +10,7 @@
include::{cq-version}@camel-quarkus:ROOT:partial$reference/others/test-spring.ad
*Since Camel {since}*
-Allows to test Camel with Spring applications such as Spring XML files or
Spring-Boot.
+Allows to test Camel with Spring applications such as Spring XML files.
-[IMPORTANT]
-====
-This documentation is old and needs to be updated
-====
+WARNING: This module is deprecated. Use
xref:components:others:test-spring-junit5.adoc[camel-test-spring-junit5]
instead.
-xref:manual::testing.adoc[Testing] is a crucial part of any development or
integration work. The Spring Framework offers a number of features that makes
it easy to test while using Spring for Inversion of Control which works with
JUnit 3.x and JUnit 4.x.
-
-We can use Spring for IoC and the Camel xref:ROOT:mock-component.adoc[Mock]
and xref:test.adoc[Test] endpoints to create sophisticated integration/unit
tests that are easy to run and debug inside your IDE. There are three
supported approaches for testing with Spring in Camel.
-[width="100%",cols="1,1,4,1",options="header",]
-|=======================================================================
-|Name |Testing Frameworks Supported |Description |Required Camel Test
Dependencies
-|<<camel-spring-test-support,Camel Spring Test Support>> a|
-* JUnit 3.x (deprecated)
-* JUnit 4.x
-
-a|
-Provided by:
-
-* `org.apache.camel.test.CamelSpringTestSupport`
-* `org.apache.camel.test.junit4.CamelSpringTestSupport`
-
-These base classes provide feature parity with the simple `CamelTestSupport`
classes from xref:test.adoc[Camel Test] but do not support Spring annotations
on the test class such as `@Autowired`, `@DirtiesContext`, and
`@ContextConfiguration`.
-
-a|
-* JUnit 3.x (deprecated) - camel-test-spring
-* JUnit 4.x - camel-test-spring
-
-|<<plain-spring-test, Plain Spring Test>> a|
-* JUnit 3.x
-* JUnit 4.x
-
-a|
-Either extend the abstract base classes:
-
-* `org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests`
-* `org.springframework.test.context.junit38.AbstractJUnit4SpringContextTests`
-* etc.
-
-provided in Spring Test or use the Spring Test JUnit4 runner.
-
-These approaches support both the Camel annotations and Spring annotations.
However, they do NOT have
http://camel.apache.org/camel-test.html#CamelTest-FeaturesProvidedbyCamelTestSupport[feature
parity] with:
-
-* `org.apache.camel.test.CamelTestSupport`
-* `org.apache.camel.test.junit4.CamelTestSupport`
-
-a|
-* JUnit 3.x (deprecated) - None
-* JUnit 4.x - None
-
-|<<camel-enhanced-spring-test,Camel Enhanced Spring Test>> a|
-* JUnit 4.x
-
-a|
-* use the `org.apache.camel.test.junit4.CamelSpringJUnit4ClassRunner` runner
with the `@RunWith` annotation to enable
http://camel.apache.org/camel-test.html#CamelTest-FeaturesProvidedbyCamelTestSupport[feature
parity] with `org.apache.camel.test.CamelTestSupport` and
`org.apache.camel.test.junit4.CamelTestSupport`. These classes support the full
suite of Spring Test annotations such as `@Autowired`, `@DirtiesContext`, and
`@ContextConfiguration`.
-
-a|
-* JUnit 3.x (deprecated) - camel-test-spring
-* JUnit 4.x - camel-test-spring
-|=======================================================================
-
-[#camel-spring-test-support]
-== CamelSpringTestSupport
-
-The following Spring test support classes:
-
-* `org.apache.camel.test.CamelSpringTestSupport`
-* `org.apache.camel.test.junit4.CamelSpringTestSupport`
-
-extend their non-Spring aware counterparts:
-
-* `org.apache.camel.test.CamelTestSupport`
-* `org.apache.camel.test.junit4.CamelTestSupport`
-
-and deliver integration with Spring into your test classes.
-
-Instead of instantiating the `CamelContext` and routes programmatically, these
classes rely on a Spring context to wire the needed components together. If
your test extends one of these classes, you must provide the Spring context by
implementing the following method.
-[source,java]
-----
-protected abstract AbstractApplicationContext createApplicationContext();
-----
-You are responsible for the instantiation of the Spring context in the method
implementation. All of the features available in the non-Spring aware
counterparts from http://camel.apache.org/camel-test.html[Camel Test] are
available in your test.
-
-[#plain-spring-test]
-== Plain Spring Test
-
-In this approach, your test classes directly inherit from the Spring Test
abstract test classes or use the JUnit 4.x test runner provided in Spring Test.
This approach supports dependency injection into your test class and the full
suite of Spring Test annotations. However, it does not support the features
provided by the `CamelSpringTestSupport` classes.
-
-=== Plain Spring Test using JUnit 4.x with XML Config Example
-
-Here is a simple unit test using JUnit 4.x support from Spring Test:
-[source,java]
-----
-@ContextConfiguration
-public class FilterTest extends SpringRunWithTestSupport {
-
- @EndpointInject("mock:result")
- protected MockEndpoint resultEndpoint;
-
- @Produce("direct:start")
- protected ProducerTemplate template;
-
- @DirtiesContext
- @Test
- public void testSendMatchingMessage() throws Exception {
- String expectedBody = "<matched/>";
-
- resultEndpoint.expectedBodiesReceived(expectedBody);
-
- template.sendBodyAndHeader(expectedBody, "foo", "bar");
-
- resultEndpoint.assertIsSatisfied();
- }
-
- @DirtiesContext
- @Test
- public void testSendNotMatchingMessage() throws Exception {
- resultEndpoint.expectedMessageCount(0);
-
- template.sendBodyAndHeader("<notMatched/>", "foo",
"notMatchedHeaderValue");
-
- resultEndpoint.assertIsSatisfied();
- }
-}
-----
-Notice that we use `@DirtiesContext` on the test methods to force Spring
Testing to automatically reload the
http://camel.apache.org/camelcontext.html[CamelContext] after each test method
- this ensures that the tests don't clash with each other, e.g., one test
method sending to an endpoint that is then reused in another test method.
-
-Also notice the use of `@ContextConfiguration` to indicate that by default we
should look for the file
-`FilterTest-context.xml` to configure the test case. The test context looks
like:
-[source,xml]
-----
-<beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
- http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
- ">
-
- <camelContext xmlns="http://camel.apache.org/schema/spring">
- <route>
- <from uri="direct:start"/>
- <filter>
- <xpath>$foo = 'bar'</xpath>
- <to uri="mock:result"/>
- </filter>
- </route>
- </camelContext>
-
-</beans>
-----
-This test will load a Spring XML configuration file called
`FilterTest-context.xml` from the classpath in the same package structure as
the `FilterTest` class and initialize it along with any Camel routes we define
inside it, then inject the `CamelContext` instance into our test case.
-
-For instance, like this maven folder layout:
-[source,text]
-----
-src/test/java/org/apache/camel/spring/patterns/FilterTest.java
-src/test/resources/org/apache/camel/spring/patterns/FilterTest-context.xml
-----
-
-=== Plain Spring Test Using JUnit 4.x With Java Config Example
-
-You can completely avoid using an XML configuration file by using Spring Java
Config.
-
-[source,java]
-----
-@RunWith(CamelSpringJUnit4ClassRunner.class)
-@ContextConfiguration(classes = {FilterTest.ContextConfig.class}, loader =
CamelSpringDelegatingTestContextLoader.class)
-public class FilterTest extends AbstractJUnit4SpringContextTests {
-
- @EndpointInject("mock:result")
- protected MockEndpoint resultEndpoint;
-
- @Produce("direct:start")
- protected ProducerTemplate template;
-
- @DirtiesContext
- @Test
- public void testSendMatchingMessage() throws Exception {
- String expectedBody = "<matched/>";
-
- resultEndpoint.expectedBodiesReceived(expectedBody);
-
- template.sendBodyAndHeader(expectedBody, "foo", "bar");
-
- resultEndpoint.assertIsSatisfied();
- }
-
- @DirtiesContext
- @Test
- public void testSendNotMatchingMessage() throws Exception {
- resultEndpoint.expectedMessageCount(0);
-
- template.sendBodyAndHeader("<notMatched/>", "foo",
"notMatchedHeaderValue");
-
- resultEndpoint.assertIsSatisfied();
- }
-
- @Configuration
- public static class ContextConfig extends SingleRouteCamelConfiguration {
- @Bean
- public RouteBuilder route() {
- return new RouteBuilder() {
- public void configure() {
-
from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");
- }
- };
- }
- }
-}
-----
-
-This is similar to the XML Config example above except that there is no XML
file and instead the nested `ContextConfig` class does all of the
configuration; so your entire test case is contained in a single Java class. We
currently have to reference by class name this class in the
`@ContextConfiguration` which is a bit ugly. Please vote for
http://jira.springframework.org/browse/SJC-238[SJC-238] to address this and
make Spring Test work more cleanly with Spring JavaConfig.
-
-=== Plain Spring Test Using JUnit Runner With XML Config
-
-You can avoid extending Spring classes by using the `SpringJUnit4ClassRunner`
provided by Spring Test. This custom JUnit runner means you are free to choose
your own class hierarchy while retaining all the capabilities of Spring Test.
-
-[source,java]
-----
-@RunWith(CamelSpringJUnit4ClassRunner.class)
-@BootstrapWith(CamelTestContextBootstrapper.class)
-@ContextConfiguration
-public class MyCamelTest {
- @Autowired
- protected CamelContext camelContext;
-
- @EndpointInject("mock:foo")
- protected MockEndpoint foo;
-
- @Test
- @DirtiesContext
- public void testMocksAreValid() throws Exception {
- // ...
-
- foo.message(0).header("bar").isEqualTo("ABC");
- MockEndpoint.assertIsSatisfied(camelContext);
- }
-}
-----
-
-[#camel-enhanced-spring-test]
-== Camel Enhanced Spring Test
-
-Using the `org.apache.camel.test.junit4.CamelSpringJUnit4ClassRunner` runner
with the `@RunWith` annotation provides the full feature set of Spring Test
with support for the feature set provided in the `CamelTestSupport` classes.
-
-A number of Camel specific annotations have been developed in order to provide
for declarative manipulation of the Camel context(s) involved in the test.
These annotations free your test classes from having to inherit from the
`CamelSpringTestSupport` classes and also reduce the amount of code required to
customize the tests.
-[width="100%",cols="1,1,4,1,1",options="header",]
-|=======================================================================
-|Annotation Class |Applies To |Description |Default Behaviour If Not Present
|Default Behavior If Present
-|org.apache.camel.test.spring.DisableJmx a|Class a|
-Indicates if JMX should be globally disabled in the CamelContexts that are
bootstrapped during the test through the use of Spring Test loaded application
contexts. a|
-JMX is disabled a|JMX is disabled
-
-|org.apache.camel.test.spring.ExcludeRoutes a|Class a|
-Indicates if certain route builder classes should be excluded from discovery.
Initializes a `org.apache.camel.spi.PackageScanClassResolver` to exclude a set
of given classes from being resolved. Typically this is used at test time to
exclude certain routes, which might otherwise be just noisy, from being
discovered and initialized. a|
-Not enabled and no routes are excluded a|No routes are excluded
-
-|org.apache.camel.test.spring.MockEndpoints a|Class a|
-Triggers the auto-mocking of endpoints whose URIs match the provided filter.
The default filter is `"*"` which matches all endpoints. See
`org.apache.camel.impl.InterceptSendToMockEndpointStrategy` for more details on
the registration of the mock endpoints. a|
-Not enabled a|All endpoints are sniffed and recorded in a mock endpoint.
-
-|org.apache.camel.test.spring.MockEndpointsAndSkip a|Class a|
-Triggers the auto-mocking of endpoints whose URIs match the provided filter.
The default filter is `"*"`, which matches all endpoints.
-This annotation will also skip sending the message to matched endpoints as
well. a|
-Not enabled a|All endpoints are sniffed and recorded in a mock endpoint. The
original endpoint is not invoked.
-
-|org.apache.camel.test.spring.ProvidesBreakpoint a|Method a|
-Indicates that the annotated method returns an
`org.apache.camel.spi.Breakpoint` for use in the test. Useful for intercepting
traffic to all endpoints or simply for setting a break point in an IDE for
debugging. The method must be public, static, take no arguments, and return
`org.apache.camel.spi.Breakpoint`. a|
-N/A a|
-The returned `Breakpoint` is registered in the CamelContext(s)
-
-|org.apache.camel.test.spring.ShutdownTimeout a|Class a|
-Indicates to set the shutdown timeout of all CamelContexts instantiated
through the use of Spring Test loaded application contexts. If no annotation
is used, the timeout is automatically reduced to 10 seconds by the test
framework. a|
-10 seconds a|10 seconds
-
-|org.apache.camel.test.spring.UseAdviceWith a|Class a|
-Indicates the use of `adviceWith()` within the test class. If a class is
annotated with this annotation and `UseAdviceWith#value()` returns true, any
CamelContexts bootstrapped during the test through the use of Spring Test
loaded application contexts will not be started automatically.
-
-The test author is responsible for injecting the Camel contexts into the test
and executing `CamelContext#start()` on them at the appropriate time after any
advice has been applied to the routes in the CamelContext(s). a|
-CamelContexts do not automatically start. a|CamelContexts do not automatically
start.
-
-|org.apache.camel.test.spring.UseOverridePropertiesWithPropertiesComponent
a|Method a|
-Indicates that the annotated method returns a `java.util.Properties` for use
in the test, and that those properties override any existing properties
configured on the `PropertiesComponent`. a|
-a|Override properties
-|=======================================================================
-
-The following example illustrates the use of the `@MockEndpoints` annotation
in order to setup mock endpoints as interceptors on all endpoints using the
Camel Log component and the `@DisableJmx` annotation to enable JMX which is
disabled during tests by default.
-
-[TIP]
-====
-We still use the `@DirtiesContext` annotation to ensure that the CamelContext,
routes, and mock endpoints are reinitialized between test methods.
-====
-
-[source,java]
-----
-@RunWith(CamelSpringJUnit4ClassRunner.class)
-@BootstrapWith(CamelTestContextBootstrapper.class)
-@ContextConfiguration
-@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
-@MockEndpoints("log:*")
-@DisableJmx(false)
-public class CamelSpringJUnit4ClassRunnerPlainTest {
- @Autowired
- protected CamelContext camelContext2;
-
- protected MockEndpoint mockB;
-
- @EndpointInject(value = "mock:c", context = "camelContext2")
- protected MockEndpoint mockC;
-
- @Produce("direct:start2", context = "camelContext2")
- protected ProducerTemplate start2;
-
- @EndpointInject(value = "mock:log:org.apache.camel.test.junit4.spring",
context = "camelContext2")
- protected MockEndpoint mockLog;
-
- @Test
- public void testPositive() throws Exception {
- mockC.expectedBodiesReceived("David");
- mockLog.expectedBodiesReceived("Hello David");
-
- start2.sendBody("David");
-
- MockEndpoint.assertIsSatisfied(camelContext);
- }
-}
-----
-
-== Adding More Mock Expectations
-
-If you wish to add any new assertions to your test you can easily do so with
the following. Notice how we use `@EndpointInject` to inject a Camel endpoint
into our code then the http://camel.apache.org/mock.html[Mock] API to add an
expectation on a specific message.
-[source,java]
-----
-@ContextConfiguration
-public class MyCamelTest extends AbstractJUnit38SpringContextTests {
- @Autowired
- protected CamelContext camelContext;
-
- @EndpointInject("mock:foo")
- protected MockEndpoint foo;
-
- public void testMocksAreValid() throws Exception {
- // lets add more expectations
- foo.message(0).header("bar").isEqualTo("ABC");
-
- MockEndpoint.assertIsSatisfied(camelContext);
- }
-}
-----
-
-
-== Further Processing the Received Messages
-
-Sometimes once a http://camel.apache.org/mock.html[Mock] endpoint has received
some messages you want to then process them further to add further assertions
that your test case worked as you expect.
-
-So you can then process the received message exchanges if you like...
-[source,java]
-----
-@ContextConfiguration
-public class MyCamelTest extends AbstractJUnit38SpringContextTests {
- @Autowired
- protected CamelContext camelContext;
-
- @EndpointInject("mock:foo")
- protected MockEndpoint foo;
-
- public void testMocksAreValid() throws Exception {
- // lets add more expectations...
-
- MockEndpoint.assertIsSatisfied(camelContext);
-
- // now lets do some further assertions
- List<Exchange> list = foo.getReceivedExchanges();
- for (Exchange exchange : list) {
- Message in = exchange.getIn();
- // ...
- }
- }
-}
-----
-
-== Sending and Receiving Messages
-
-It might be that the
http://camel.apache.org/enterprise-integration-patterns.html[Enterprise
Integration Patterns] you have defined in either
http://camel.apache.org/spring.html[Spring] XML or using the Java
http://camel.apache.org/dsl.html[DSL] do all of the sending and receiving and
you might just work with the http://camel.apache.org/mock.html[Mock] endpoints
as described above. However sometimes in a test case its useful to explicitly
send or receive messages directly.
-
-To send or receive messages you should use the
http://camel.apache.org/bean-integration.html[Bean Integration] mechanism. For
example to send messages inject a `ProducerTemplate` using the
`@EndpointInject` annotation then call the various send methods on this object
to send a message to an endpoint. To consume messages use the `@MessageDriven`
annotation on a method to have the method invoked when a message is received.
-[source,java]
-----
-public class Foo {
- @EndpointInject("activemq:foo.bar")
- ProducerTemplate producer;
-
- public void doSomething() {
- // lets send a message!
- producer.sendBody("<hello>world!</hello>");
- }
-
- // lets consume messages from the 'cheese' queue
- @MessageDriven(uri="activemq:cheese")
- public void onCheese(String name) {
- // ...
- }
-}
-----
diff --git a/components/camel-test/camel-test/src/main/docs/test.adoc
b/components/camel-test/camel-test/src/main/docs/test.adoc
index eb7ec22..7908559 100644
--- a/components/camel-test/camel-test/src/main/docs/test.adoc
+++ b/components/camel-test/camel-test/src/main/docs/test.adoc
@@ -12,6 +12,8 @@
include::{cq-version}@camel-quarkus:ROOT:partial$reference/others/test.adoc[opts
The `camel-test` module is used for unit testing Camel.
+WARNING: This module is deprecated. Use
xref:components:others:test-junit5.adoc[camel-test-junit5] instead.
+
The class `org.apache.camel.test.junit4.CamelTestSupport` provides a base
JUnit class which you would extend
and implement your Camel unit test.
diff --git a/docs/components/modules/others/pages/test-junit5.adoc
b/docs/components/modules/others/pages/test-junit5.adoc
index ae264b5..0f159c3 100644
--- a/docs/components/modules/others/pages/test-junit5.adoc
+++ b/docs/components/modules/others/pages/test-junit5.adoc
@@ -55,10 +55,12 @@ public class SimpleMockTest extends CamelTestSupport {
----
== Migrating Camel Tests from JUnit 4 to JUnit 5
+
Find below some hints to help in migrating camel tests from JUnit 4 to JUnit 5.
-=== Referencing the Camel Test JUnit5 library in your project
-Projects using `camel-test` would need to use `camel-test-junit5`. For
instance, maven users would update their pom.xml file as below:
+Projects using `camel-test` would need to use `camel-test-junit5`. For
instance, maven users would update their `pom.xml` file as below:
+
+[source,xml]
----
<dependency>
<groupId>org.apache.camel</groupId>
@@ -67,10 +69,11 @@ Projects using `camel-test` would need to use
`camel-test-junit5`. For instance,
</dependency>
----
-Tips: It's possible to run JUnit4 & JUnit5 based camel tests side by side
including the following dependencies `camel-test`,
-`camel-test-junit5` and `junit-vintage-engine`. This configuration allows to
migrate a camel test at once.
+TIP: It's possible to run JUnit4 & JUnit5 based Camel tests side by side
including the following dependencies `camel-test`,
+`camel-test-junit5` and `junit-vintage-engine`. This configuration allows
migrating Camel tests one by one.
+
+=== Migration Steps
-=== Typical migration steps linked to JUnit 5 support in Camel Test
* Imports of `org.apache.camel.test.junit4.\*` should be replaced with
`org.apache.camel.test.junit5.*`
* `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);`
@@ -78,8 +81,6 @@ Tips: It's possible to run JUnit4 & JUnit5 based camel tests
side by side includ
* 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:
* New JUnit 5 assertions should be imported where needed, for instance `import
static org.junit.jupiter.api.Assertions.assertEquals`
@@ -92,4 +93,4 @@ Once Camel related steps have been performed, there are still
typical JUnit 5 mi
* `org.junit.AfterClass` should be changed in favor of `import
org.junit.jupiter.api.AfterAll`
* Built-in `assertThat` from third-party assertion libraries should be used.
For instance, use `org.hamcrest.MatcherAssert.assertThat` from `java-hamcrest`
-Depending on the context, more involved tips might be needed, please check the
https://junit.org/junit5/docs/current/user-guide/[JUnit 5 User Guide] then.
+Depending on the context, more involved tips might be needed, please check the
https://junit.org/junit5/docs/current/user-guide/[JUnit 5 User Guide].
diff --git a/docs/components/modules/others/pages/test-spring-junit5.adoc
b/docs/components/modules/others/pages/test-spring-junit5.adoc
index 8812881..cba8474 100644
--- a/docs/components/modules/others/pages/test-spring-junit5.adoc
+++ b/docs/components/modules/others/pages/test-spring-junit5.adoc
@@ -11,14 +11,15 @@
include::{cq-version}@camel-quarkus:ROOT:partial$reference/others/test-spring-ju
*Since Camel {since}*
-The `camel-test-spring-junit5` module makes it possible to test Camel Spring
based applications with JUnit 5.
+The `camel-test-spring-junit5` module is used for testing Camel with Spring;
+both the classic Spring XML files or Spring Boot.
-Testing is a crucial part of any development or integration work. The Spring
Framework offers a number of features that makes it easy to test while using
Spring for Inversion of Control.
+== Testing classic Spring XML
There are multiple approaches to test Camel Spring 5.x based routes with JUnit
5.
-
-== Extending the CamelSpringTestSupport class
An approach is to extend
`org.apache.camel.test.spring.junit5.CamelSpringTestSupport`, for instance:
+
+[source,java]
----
public class SimpleMockTest extends CamelSpringTestSupport {
@@ -30,6 +31,7 @@ public class SimpleMockTest extends CamelSpringTestSupport {
@Override
protected AbstractApplicationContext createApplicationContext() {
+ // loads a Spring XML file
return new
ClassPathXmlApplicationContext("org/apache/camel/test/patterns/SimpleMockTest.xml");
}
@@ -43,17 +45,30 @@ public class SimpleMockTest extends CamelSpringTestSupport {
}
----
-This approach provides feature parity with
`org.apache.camel.test.junit5.CamelTestSupport` from
xref:test-junit5.adoc[camel-test-junit5] but does not support Spring
annotations on the test class such as `@Autowired`, `@DirtiesContext`, and
`@ContextConfiguration`.
+The example above is loading a classic Spring XML file (has `<beans>` as root
tag).
+
+This approach provides feature parity with
`org.apache.camel.test.junit5.CamelTestSupport`
+from xref:components:others:test-junit5.adoc[camel-test-junit5] but does not
support Spring annotations
+on the test class such as `@Autowired`, `@DirtiesContext`, and
`@ContextConfiguration`.
+
+Instead of instantiating the `CamelContext` and routes programmatically,
+this class relies on a Spring context to wire the needed components together.
+If your test extends this class, you must provide the Spring context by
implementing the following method:
-Instead of instantiating the `CamelContext` and routes programmatically, this
class relies on a Spring context to wire the needed components together. If
your test extends this class, you must provide the Spring context by
implementing the following method.
[source,java]
----
protected abstract AbstractApplicationContext createApplicationContext();
----
-== Using the @CamelSpringTest annotation
-Another approach involves the usage of the
`org.apache.camel.test.spring.junit5.CamelSpringTest` annotation, for instance:
+=== Using the @CamelSpringTest annotation
+
+A better and recommended approach involves the usage of the
+`org.apache.camel.test.spring.junit5.CamelSpringTest` annotation, as shown:
+
+[source,java]
----
+package com.foo;
+
@CamelSpringTest
@ContextConfiguration
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@@ -82,15 +97,15 @@ public class CamelSpringPlainTest {
MockEndpoint.assertIsSatisfied(camelContext);
}
-
- @Test
- public void testAnotherCase() throws Exception {
- ...
- }
}
----
-The above test will load the route from
`org/apache/camel/test/spring/CamelSpringPlainTest-context.xml` which looks
like below:
+The above test will by default load a Spring XML file using the naming pattern
_className_-context.xml, which means
+the example above loads the file `com/foo/CamelSpringPlainTest-context.xml`.
+
+This XML file is Spring XML file as shown:
+
+[source,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
@@ -99,27 +114,16 @@ The above test will load the route from
`org/apache/camel/test/spring/CamelSprin
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
- <camelContext id="camelContext"
- xmlns="http://camel.apache.org/schema/spring" trace="true"
- autoStartup="true">
- <packageScan>
- <package>org.apache.camel.test.spring</package>
- </packageScan>
+ <camelContext id="camelContext"
xmlns="http://camel.apache.org/schema/spring">
<route>
- <from uri="direct:start" />
- <to uri="mock:a" />
+ <from uri="direct:start"/>
+ <to uri="mock:a"/>
<transform>
<simple>Hello ${body}</simple>
</transform>
- <to uri="mock:b" />
+ <to uri="mock:b"/>
</route>
</camelContext>
-
- <bean id="bridgePropertyPlaceholder"
-
class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
- <property name="location"
-
value="classpath:org/apache/camel/test/spring/test.properties" />
- </bean>
</beans>
----
@@ -127,10 +131,12 @@ This approach supports both Camel and Spring annotations,
such as `@Autowired`,
However, it does NOT have feature parity with
`org.apache.camel.test.junit5.CamelTestSupport`.
== Migrating Camel Spring Tests from JUnit 4 to JUnit 5
+
Find below some hints to help in migrating Camel Spring tests from JUnit 4 to
JUnit 5.
-=== Referencing the Camel Test Spring JUnit 5 library in your project
Projects using `camel-test-spring` would need to use
`camel-test-spring-junit5`. For instance, maven users would update their
pom.xml file as below:
+
+[source,xml]
----
<dependency>
<groupId>org.apache.camel</groupId>
@@ -139,11 +145,12 @@ Projects using `camel-test-spring` would need to use
`camel-test-spring-junit5`.
</dependency>
----
-Tips: It's possible to run JUnit 4 & JUnit 5 based Camel Spring tests side by
side including the following dependencies `camel-test-spring`,
-`camel-test-spring-junit5` and `junit-vintage-engine`. This configuration
allows to migrate a Camel Spring test at once.
+TIP: It's possible to run JUnit 4 & JUnit 5 based Camel Spring tests side by
side including the following dependencies `camel-test-spring`,
+`camel-test-spring-junit5` and `junit-vintage-engine`. This configuration
allows migrating Camel tests one by one.
+
+=== Migration steps
-=== Typical migration steps linked to JUnit 5 support in Camel Test Spring
-* Migration steps linked to xref:test-junit5.adoc[JUnit 5 support in Camel
Test itself] should have been applied first
+* Migration steps from
xref:components:others:test-junit5.adoc[camel-test-junit5] should have been
applied first
* Imports of `org.apache.camel.test.spring.\*` should be replaced with
`org.apache.camel.test.spring.junit5.*`
* Usage of `@RunWith(CamelSpringRunner.class)` should be replaced with
`@CamelSpringTest`
* Usage of `@BootstrapWith(CamelTestContextBootstrapper.class)` should be
replaced with `@CamelSpringTest`
diff --git a/docs/components/modules/others/pages/test-spring.adoc
b/docs/components/modules/others/pages/test-spring.adoc
index 311a17b..404006e 100644
--- a/docs/components/modules/others/pages/test-spring.adoc
+++ b/docs/components/modules/others/pages/test-spring.adoc
@@ -12,409 +12,7 @@
include::{cq-version}@camel-quarkus:ROOT:partial$reference/others/test-spring.ad
*Since Camel {since}*
-Allows to test Camel with Spring applications such as Spring XML files or
Spring-Boot.
+Allows to test Camel with Spring applications such as Spring XML files.
-[IMPORTANT]
-====
-This documentation is old and needs to be updated
-====
+WARNING: This module is deprecated. Use
xref:components:others:test-spring-junit5.adoc[camel-test-spring-junit5]
instead.
-xref:manual::testing.adoc[Testing] is a crucial part of any development or
integration work. The Spring Framework offers a number of features that makes
it easy to test while using Spring for Inversion of Control which works with
JUnit 3.x and JUnit 4.x.
-
-We can use Spring for IoC and the Camel xref:ROOT:mock-component.adoc[Mock]
and xref:test.adoc[Test] endpoints to create sophisticated integration/unit
tests that are easy to run and debug inside your IDE. There are three
supported approaches for testing with Spring in Camel.
-[width="100%",cols="1,1,4,1",options="header",]
-|=======================================================================
-|Name |Testing Frameworks Supported |Description |Required Camel Test
Dependencies
-|<<camel-spring-test-support,Camel Spring Test Support>> a|
-* JUnit 3.x (deprecated)
-* JUnit 4.x
-
-a|
-Provided by:
-
-* `org.apache.camel.test.CamelSpringTestSupport`
-* `org.apache.camel.test.junit4.CamelSpringTestSupport`
-
-These base classes provide feature parity with the simple `CamelTestSupport`
classes from xref:test.adoc[Camel Test] but do not support Spring annotations
on the test class such as `@Autowired`, `@DirtiesContext`, and
`@ContextConfiguration`.
-
-a|
-* JUnit 3.x (deprecated) - camel-test-spring
-* JUnit 4.x - camel-test-spring
-
-|<<plain-spring-test, Plain Spring Test>> a|
-* JUnit 3.x
-* JUnit 4.x
-
-a|
-Either extend the abstract base classes:
-
-* `org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests`
-* `org.springframework.test.context.junit38.AbstractJUnit4SpringContextTests`
-* etc.
-
-provided in Spring Test or use the Spring Test JUnit4 runner.
-
-These approaches support both the Camel annotations and Spring annotations.
However, they do NOT have
http://camel.apache.org/camel-test.html#CamelTest-FeaturesProvidedbyCamelTestSupport[feature
parity] with:
-
-* `org.apache.camel.test.CamelTestSupport`
-* `org.apache.camel.test.junit4.CamelTestSupport`
-
-a|
-* JUnit 3.x (deprecated) - None
-* JUnit 4.x - None
-
-|<<camel-enhanced-spring-test,Camel Enhanced Spring Test>> a|
-* JUnit 4.x
-
-a|
-* use the `org.apache.camel.test.junit4.CamelSpringJUnit4ClassRunner` runner
with the `@RunWith` annotation to enable
http://camel.apache.org/camel-test.html#CamelTest-FeaturesProvidedbyCamelTestSupport[feature
parity] with `org.apache.camel.test.CamelTestSupport` and
`org.apache.camel.test.junit4.CamelTestSupport`. These classes support the full
suite of Spring Test annotations such as `@Autowired`, `@DirtiesContext`, and
`@ContextConfiguration`.
-
-a|
-* JUnit 3.x (deprecated) - camel-test-spring
-* JUnit 4.x - camel-test-spring
-|=======================================================================
-
-[#camel-spring-test-support]
-== CamelSpringTestSupport
-
-The following Spring test support classes:
-
-* `org.apache.camel.test.CamelSpringTestSupport`
-* `org.apache.camel.test.junit4.CamelSpringTestSupport`
-
-extend their non-Spring aware counterparts:
-
-* `org.apache.camel.test.CamelTestSupport`
-* `org.apache.camel.test.junit4.CamelTestSupport`
-
-and deliver integration with Spring into your test classes.
-
-Instead of instantiating the `CamelContext` and routes programmatically, these
classes rely on a Spring context to wire the needed components together. If
your test extends one of these classes, you must provide the Spring context by
implementing the following method.
-[source,java]
-----
-protected abstract AbstractApplicationContext createApplicationContext();
-----
-You are responsible for the instantiation of the Spring context in the method
implementation. All of the features available in the non-Spring aware
counterparts from http://camel.apache.org/camel-test.html[Camel Test] are
available in your test.
-
-[#plain-spring-test]
-== Plain Spring Test
-
-In this approach, your test classes directly inherit from the Spring Test
abstract test classes or use the JUnit 4.x test runner provided in Spring Test.
This approach supports dependency injection into your test class and the full
suite of Spring Test annotations. However, it does not support the features
provided by the `CamelSpringTestSupport` classes.
-
-=== Plain Spring Test using JUnit 4.x with XML Config Example
-
-Here is a simple unit test using JUnit 4.x support from Spring Test:
-[source,java]
-----
-@ContextConfiguration
-public class FilterTest extends SpringRunWithTestSupport {
-
- @EndpointInject("mock:result")
- protected MockEndpoint resultEndpoint;
-
- @Produce("direct:start")
- protected ProducerTemplate template;
-
- @DirtiesContext
- @Test
- public void testSendMatchingMessage() throws Exception {
- String expectedBody = "<matched/>";
-
- resultEndpoint.expectedBodiesReceived(expectedBody);
-
- template.sendBodyAndHeader(expectedBody, "foo", "bar");
-
- resultEndpoint.assertIsSatisfied();
- }
-
- @DirtiesContext
- @Test
- public void testSendNotMatchingMessage() throws Exception {
- resultEndpoint.expectedMessageCount(0);
-
- template.sendBodyAndHeader("<notMatched/>", "foo",
"notMatchedHeaderValue");
-
- resultEndpoint.assertIsSatisfied();
- }
-}
-----
-Notice that we use `@DirtiesContext` on the test methods to force Spring
Testing to automatically reload the
http://camel.apache.org/camelcontext.html[CamelContext] after each test method
- this ensures that the tests don't clash with each other, e.g., one test
method sending to an endpoint that is then reused in another test method.
-
-Also notice the use of `@ContextConfiguration` to indicate that by default we
should look for the file
-`FilterTest-context.xml` to configure the test case. The test context looks
like:
-[source,xml]
-----
-<beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
- http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
- ">
-
- <camelContext xmlns="http://camel.apache.org/schema/spring">
- <route>
- <from uri="direct:start"/>
- <filter>
- <xpath>$foo = 'bar'</xpath>
- <to uri="mock:result"/>
- </filter>
- </route>
- </camelContext>
-
-</beans>
-----
-This test will load a Spring XML configuration file called
`FilterTest-context.xml` from the classpath in the same package structure as
the `FilterTest` class and initialize it along with any Camel routes we define
inside it, then inject the `CamelContext` instance into our test case.
-
-For instance, like this maven folder layout:
-[source,text]
-----
-src/test/java/org/apache/camel/spring/patterns/FilterTest.java
-src/test/resources/org/apache/camel/spring/patterns/FilterTest-context.xml
-----
-
-=== Plain Spring Test Using JUnit 4.x With Java Config Example
-
-You can completely avoid using an XML configuration file by using Spring Java
Config.
-
-[source,java]
-----
-@RunWith(CamelSpringJUnit4ClassRunner.class)
-@ContextConfiguration(classes = {FilterTest.ContextConfig.class}, loader =
CamelSpringDelegatingTestContextLoader.class)
-public class FilterTest extends AbstractJUnit4SpringContextTests {
-
- @EndpointInject("mock:result")
- protected MockEndpoint resultEndpoint;
-
- @Produce("direct:start")
- protected ProducerTemplate template;
-
- @DirtiesContext
- @Test
- public void testSendMatchingMessage() throws Exception {
- String expectedBody = "<matched/>";
-
- resultEndpoint.expectedBodiesReceived(expectedBody);
-
- template.sendBodyAndHeader(expectedBody, "foo", "bar");
-
- resultEndpoint.assertIsSatisfied();
- }
-
- @DirtiesContext
- @Test
- public void testSendNotMatchingMessage() throws Exception {
- resultEndpoint.expectedMessageCount(0);
-
- template.sendBodyAndHeader("<notMatched/>", "foo",
"notMatchedHeaderValue");
-
- resultEndpoint.assertIsSatisfied();
- }
-
- @Configuration
- public static class ContextConfig extends SingleRouteCamelConfiguration {
- @Bean
- public RouteBuilder route() {
- return new RouteBuilder() {
- public void configure() {
-
from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");
- }
- };
- }
- }
-}
-----
-
-This is similar to the XML Config example above except that there is no XML
file and instead the nested `ContextConfig` class does all of the
configuration; so your entire test case is contained in a single Java class. We
currently have to reference by class name this class in the
`@ContextConfiguration` which is a bit ugly. Please vote for
http://jira.springframework.org/browse/SJC-238[SJC-238] to address this and
make Spring Test work more cleanly with Spring JavaConfig.
-
-=== Plain Spring Test Using JUnit Runner With XML Config
-
-You can avoid extending Spring classes by using the `SpringJUnit4ClassRunner`
provided by Spring Test. This custom JUnit runner means you are free to choose
your own class hierarchy while retaining all the capabilities of Spring Test.
-
-[source,java]
-----
-@RunWith(CamelSpringJUnit4ClassRunner.class)
-@BootstrapWith(CamelTestContextBootstrapper.class)
-@ContextConfiguration
-public class MyCamelTest {
- @Autowired
- protected CamelContext camelContext;
-
- @EndpointInject("mock:foo")
- protected MockEndpoint foo;
-
- @Test
- @DirtiesContext
- public void testMocksAreValid() throws Exception {
- // ...
-
- foo.message(0).header("bar").isEqualTo("ABC");
- MockEndpoint.assertIsSatisfied(camelContext);
- }
-}
-----
-
-[#camel-enhanced-spring-test]
-== Camel Enhanced Spring Test
-
-Using the `org.apache.camel.test.junit4.CamelSpringJUnit4ClassRunner` runner
with the `@RunWith` annotation provides the full feature set of Spring Test
with support for the feature set provided in the `CamelTestSupport` classes.
-
-A number of Camel specific annotations have been developed in order to provide
for declarative manipulation of the Camel context(s) involved in the test.
These annotations free your test classes from having to inherit from the
`CamelSpringTestSupport` classes and also reduce the amount of code required to
customize the tests.
-[width="100%",cols="1,1,4,1,1",options="header",]
-|=======================================================================
-|Annotation Class |Applies To |Description |Default Behaviour If Not Present
|Default Behavior If Present
-|org.apache.camel.test.spring.DisableJmx a|Class a|
-Indicates if JMX should be globally disabled in the CamelContexts that are
bootstrapped during the test through the use of Spring Test loaded application
contexts. a|
-JMX is disabled a|JMX is disabled
-
-|org.apache.camel.test.spring.ExcludeRoutes a|Class a|
-Indicates if certain route builder classes should be excluded from discovery.
Initializes a `org.apache.camel.spi.PackageScanClassResolver` to exclude a set
of given classes from being resolved. Typically this is used at test time to
exclude certain routes, which might otherwise be just noisy, from being
discovered and initialized. a|
-Not enabled and no routes are excluded a|No routes are excluded
-
-|org.apache.camel.test.spring.MockEndpoints a|Class a|
-Triggers the auto-mocking of endpoints whose URIs match the provided filter.
The default filter is `"*"` which matches all endpoints. See
`org.apache.camel.impl.InterceptSendToMockEndpointStrategy` for more details on
the registration of the mock endpoints. a|
-Not enabled a|All endpoints are sniffed and recorded in a mock endpoint.
-
-|org.apache.camel.test.spring.MockEndpointsAndSkip a|Class a|
-Triggers the auto-mocking of endpoints whose URIs match the provided filter.
The default filter is `"*"`, which matches all endpoints.
-This annotation will also skip sending the message to matched endpoints as
well. a|
-Not enabled a|All endpoints are sniffed and recorded in a mock endpoint. The
original endpoint is not invoked.
-
-|org.apache.camel.test.spring.ProvidesBreakpoint a|Method a|
-Indicates that the annotated method returns an
`org.apache.camel.spi.Breakpoint` for use in the test. Useful for intercepting
traffic to all endpoints or simply for setting a break point in an IDE for
debugging. The method must be public, static, take no arguments, and return
`org.apache.camel.spi.Breakpoint`. a|
-N/A a|
-The returned `Breakpoint` is registered in the CamelContext(s)
-
-|org.apache.camel.test.spring.ShutdownTimeout a|Class a|
-Indicates to set the shutdown timeout of all CamelContexts instantiated
through the use of Spring Test loaded application contexts. If no annotation
is used, the timeout is automatically reduced to 10 seconds by the test
framework. a|
-10 seconds a|10 seconds
-
-|org.apache.camel.test.spring.UseAdviceWith a|Class a|
-Indicates the use of `adviceWith()` within the test class. If a class is
annotated with this annotation and `UseAdviceWith#value()` returns true, any
CamelContexts bootstrapped during the test through the use of Spring Test
loaded application contexts will not be started automatically.
-
-The test author is responsible for injecting the Camel contexts into the test
and executing `CamelContext#start()` on them at the appropriate time after any
advice has been applied to the routes in the CamelContext(s). a|
-CamelContexts do not automatically start. a|CamelContexts do not automatically
start.
-
-|org.apache.camel.test.spring.UseOverridePropertiesWithPropertiesComponent
a|Method a|
-Indicates that the annotated method returns a `java.util.Properties` for use
in the test, and that those properties override any existing properties
configured on the `PropertiesComponent`. a|
-a|Override properties
-|=======================================================================
-
-The following example illustrates the use of the `@MockEndpoints` annotation
in order to setup mock endpoints as interceptors on all endpoints using the
Camel Log component and the `@DisableJmx` annotation to enable JMX which is
disabled during tests by default.
-
-[TIP]
-====
-We still use the `@DirtiesContext` annotation to ensure that the CamelContext,
routes, and mock endpoints are reinitialized between test methods.
-====
-
-[source,java]
-----
-@RunWith(CamelSpringJUnit4ClassRunner.class)
-@BootstrapWith(CamelTestContextBootstrapper.class)
-@ContextConfiguration
-@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
-@MockEndpoints("log:*")
-@DisableJmx(false)
-public class CamelSpringJUnit4ClassRunnerPlainTest {
- @Autowired
- protected CamelContext camelContext2;
-
- protected MockEndpoint mockB;
-
- @EndpointInject(value = "mock:c", context = "camelContext2")
- protected MockEndpoint mockC;
-
- @Produce("direct:start2", context = "camelContext2")
- protected ProducerTemplate start2;
-
- @EndpointInject(value = "mock:log:org.apache.camel.test.junit4.spring",
context = "camelContext2")
- protected MockEndpoint mockLog;
-
- @Test
- public void testPositive() throws Exception {
- mockC.expectedBodiesReceived("David");
- mockLog.expectedBodiesReceived("Hello David");
-
- start2.sendBody("David");
-
- MockEndpoint.assertIsSatisfied(camelContext);
- }
-}
-----
-
-== Adding More Mock Expectations
-
-If you wish to add any new assertions to your test you can easily do so with
the following. Notice how we use `@EndpointInject` to inject a Camel endpoint
into our code then the http://camel.apache.org/mock.html[Mock] API to add an
expectation on a specific message.
-[source,java]
-----
-@ContextConfiguration
-public class MyCamelTest extends AbstractJUnit38SpringContextTests {
- @Autowired
- protected CamelContext camelContext;
-
- @EndpointInject("mock:foo")
- protected MockEndpoint foo;
-
- public void testMocksAreValid() throws Exception {
- // lets add more expectations
- foo.message(0).header("bar").isEqualTo("ABC");
-
- MockEndpoint.assertIsSatisfied(camelContext);
- }
-}
-----
-
-
-== Further Processing the Received Messages
-
-Sometimes once a http://camel.apache.org/mock.html[Mock] endpoint has received
some messages you want to then process them further to add further assertions
that your test case worked as you expect.
-
-So you can then process the received message exchanges if you like...
-[source,java]
-----
-@ContextConfiguration
-public class MyCamelTest extends AbstractJUnit38SpringContextTests {
- @Autowired
- protected CamelContext camelContext;
-
- @EndpointInject("mock:foo")
- protected MockEndpoint foo;
-
- public void testMocksAreValid() throws Exception {
- // lets add more expectations...
-
- MockEndpoint.assertIsSatisfied(camelContext);
-
- // now lets do some further assertions
- List<Exchange> list = foo.getReceivedExchanges();
- for (Exchange exchange : list) {
- Message in = exchange.getIn();
- // ...
- }
- }
-}
-----
-
-== Sending and Receiving Messages
-
-It might be that the
http://camel.apache.org/enterprise-integration-patterns.html[Enterprise
Integration Patterns] you have defined in either
http://camel.apache.org/spring.html[Spring] XML or using the Java
http://camel.apache.org/dsl.html[DSL] do all of the sending and receiving and
you might just work with the http://camel.apache.org/mock.html[Mock] endpoints
as described above. However sometimes in a test case its useful to explicitly
send or receive messages directly.
-
-To send or receive messages you should use the
http://camel.apache.org/bean-integration.html[Bean Integration] mechanism. For
example to send messages inject a `ProducerTemplate` using the
`@EndpointInject` annotation then call the various send methods on this object
to send a message to an endpoint. To consume messages use the `@MessageDriven`
annotation on a method to have the method invoked when a message is received.
-[source,java]
-----
-public class Foo {
- @EndpointInject("activemq:foo.bar")
- ProducerTemplate producer;
-
- public void doSomething() {
- // lets send a message!
- producer.sendBody("<hello>world!</hello>");
- }
-
- // lets consume messages from the 'cheese' queue
- @MessageDriven(uri="activemq:cheese")
- public void onCheese(String name) {
- // ...
- }
-}
-----
diff --git a/docs/components/modules/others/pages/test.adoc
b/docs/components/modules/others/pages/test.adoc
index be93e9d..18aa083 100644
--- a/docs/components/modules/others/pages/test.adoc
+++ b/docs/components/modules/others/pages/test.adoc
@@ -14,6 +14,8 @@
include::{cq-version}@camel-quarkus:ROOT:partial$reference/others/test.adoc[opts
The `camel-test` module is used for unit testing Camel.
+WARNING: This module is deprecated. Use
xref:components:others:test-junit5.adoc[camel-test-junit5] instead.
+
The class `org.apache.camel.test.junit4.CamelTestSupport` provides a base
JUnit class which you would extend
and implement your Camel unit test.