You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2020/07/07 06:21:46 UTC

[camel-quarkus] branch master updated: Document how to use mock in JVM mode tests #1449

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

jamesnetherton pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/master by this push:
     new 1e6a366  Document how to use mock in JVM mode tests #1449
1e6a366 is described below

commit 1e6a3666d9859b1028add13ffdd2919747c57785
Author: JiriOndrusek <on...@gmail.com>
AuthorDate: Fri Jul 3 12:51:30 2020 +0200

    Document how to use mock in JVM mode tests #1449
---
 docs/modules/ROOT/pages/extensions/mock.adoc       | 64 ++++++++++++++++++++++
 .../mock/runtime/src/main/doc/limitations.adoc     |  4 ++
 extensions/mock/runtime/src/main/doc/usage.adoc    | 52 ++++++++++++++++++
 3 files changed, 120 insertions(+)

diff --git a/docs/modules/ROOT/pages/extensions/mock.adoc b/docs/modules/ROOT/pages/extensions/mock.adoc
index fc9cc61..dd26adf 100644
--- a/docs/modules/ROOT/pages/extensions/mock.adoc
+++ b/docs/modules/ROOT/pages/extensions/mock.adoc
@@ -26,3 +26,67 @@ Please refer to the above link for usage and configuration details.
 ----
 
 Check the xref:user-guide/index.adoc[User guide] for more information about writing Camel Quarkus applications.
+
+== Usage
+
+To use camel-mock capabilities in tests it is required to get access to MockEndpoint instances.
+
+CDI injection could be used for accessing instances (see https://quarkus.io/guides/getting-started-testing#injection-into-tests[Quarkus documentation]).
+You can inject camelContext into test using `@Inject` annotation. Camel context can be then used for obtaining mock endpoints.
+See the following example:
+
+----
+import javax.inject.Inject;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+import io.quarkus.test.junit.QuarkusTest;
+
+@QuarkusTest
+public class MockJvmTest {
+
+    @Inject
+    CamelContext camelContext;
+
+    @Inject
+    ProducerTemplate producerTemplate;
+
+    @Test
+    public void test() throws InterruptedException {
+
+        producerTemplate.sendBody("direct:start", "Hello World");
+
+        MockEndpoint mockEndpoint = camelContext.getEndpoint("mock:result", MockEndpoint.class);
+        mockEndpoint.expectedBodiesReceived("Hello World");
+
+        mockEndpoint.assertIsSatisfied();
+    }
+}
+----
+Route used for the example test:
+----
+import javax.enterprise.context.ApplicationScoped;
+
+import org.apache.camel.builder.RouteBuilder;
+
+@ApplicationScoped
+public class MockRoute extends RouteBuilder {
+
+    @Override
+    public void configure() throws Exception {
+        from("direct:start").to("mock:result");
+    }
+}
+----
+
+
+== Camel Quarkus limitations
+
+Injection of CDI beans (described in Usage) does not work in native mode.
+
+In the native mode the test and the application under test are running in two different processes and it is not possible
+to share a mock bean between them (see https://quarkus.io/guides/getting-started-testing#native-executable-testing[Quarkus documentation]).
+
diff --git a/extensions/mock/runtime/src/main/doc/limitations.adoc b/extensions/mock/runtime/src/main/doc/limitations.adoc
new file mode 100644
index 0000000..a7022d5
--- /dev/null
+++ b/extensions/mock/runtime/src/main/doc/limitations.adoc
@@ -0,0 +1,4 @@
+Injection of CDI beans (described in Usage) does not work in native mode.
+
+In the native mode the test and the application under test are running in two different processes and it is not possible
+to share a mock bean between them (see https://quarkus.io/guides/getting-started-testing#native-executable-testing[Quarkus documentation]).
diff --git a/extensions/mock/runtime/src/main/doc/usage.adoc b/extensions/mock/runtime/src/main/doc/usage.adoc
new file mode 100644
index 0000000..bdd3dd0
--- /dev/null
+++ b/extensions/mock/runtime/src/main/doc/usage.adoc
@@ -0,0 +1,52 @@
+To use camel-mock capabilities in tests it is required to get access to MockEndpoint instances.
+
+CDI injection could be used for accessing instances (see https://quarkus.io/guides/getting-started-testing#injection-into-tests[Quarkus documentation]).
+You can inject camelContext into test using `@Inject` annotation. Camel context can be then used for obtaining mock endpoints.
+See the following example:
+
+----
+import javax.inject.Inject;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+import io.quarkus.test.junit.QuarkusTest;
+
+@QuarkusTest
+public class MockJvmTest {
+
+    @Inject
+    CamelContext camelContext;
+
+    @Inject
+    ProducerTemplate producerTemplate;
+
+    @Test
+    public void test() throws InterruptedException {
+
+        producerTemplate.sendBody("direct:start", "Hello World");
+
+        MockEndpoint mockEndpoint = camelContext.getEndpoint("mock:result", MockEndpoint.class);
+        mockEndpoint.expectedBodiesReceived("Hello World");
+
+        mockEndpoint.assertIsSatisfied();
+    }
+}
+----
+Route used for the example test:
+----
+import javax.enterprise.context.ApplicationScoped;
+
+import org.apache.camel.builder.RouteBuilder;
+
+@ApplicationScoped
+public class MockRoute extends RouteBuilder {
+
+    @Override
+    public void configure() throws Exception {
+        from("direct:start").to("mock:result");
+    }
+}
+----