// Do not edit directly!
// This file was generated by camel-quarkus-maven-plugin:update-extension-doc-page
[id="extensions-mock"]
= Mock
:page-aliases: extensions/mock.adoc
:linkattrs:
:cq-artifact-id: camel-quarkus-mock
:cq-native-supported: true
:cq-status: Stable
:cq-status-deprecation: Stable
:cq-description: Test routes and mediation rules using mocks.
:cq-deprecated: false
:cq-jvm-since: 1.0.0
:cq-native-since: 1.0.0

ifeval::[{doc-show-badges} == true]
[.badges]
[.badge-key]##JVM since##[.badge-supported]##1.0.0## [.badge-key]##Native since##[.badge-supported]##1.0.0##
endif::[]

Test routes and mediation rules using mocks.

[id="extensions-mock-whats-inside"]
== What's inside

* xref:{cq-camel-components}::mock-component.adoc[Mock component], URI syntax: `mock:name`

Please refer to the above link for usage and configuration details.

[id="extensions-mock-maven-coordinates"]
== Maven coordinates

https://{link-quarkus-code-generator}/?extension-search=camel-quarkus-mock[Create a new project with this extension on {link-quarkus-code-generator}, window="_blank"]

Or add the coordinates to your existing project:

[source,xml]
----
<dependency>
    <groupId>org.apache.camel.quarkus</groupId>
    <artifactId>camel-quarkus-mock</artifactId>
</dependency>
----
ifeval::[{doc-show-user-guide-link} == true]
Check the xref:user-guide/index.adoc[User guide] for more information about writing Camel Quarkus applications.
endif::[]

[id="extensions-mock-usage"]
== 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");
    }
}
----


[id="extensions-mock-camel-quarkus-limitations"]
== 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]).

