All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mp.reactivemessaging.mock.adoc Maven / Gradle / Ivy

The newest version!
///////////////////////////////////////////////////////////////////////////////

    Copyright (c) 2022, 2024 Oracle and/or its affiliates.

    Licensed 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.

///////////////////////////////////////////////////////////////////////////////

= Mock Connector
:description: Reactive Messaging Mock connector for testing
:keywords: helidon, mp, messaging, test, mock
:feature-name: Mock Connector
:microprofile-bundle: false
:rootdir: {docdir}/../..

include::{rootdir}/includes/mp.adoc[]

== Contents

- <>
- <>
- <>
- <>
- <>

== Overview
Mock connector is a simple application scoped bean that can be used for emitting to a channel
or asserting received data in a test environment. All data received are kept in memory only.

include::{rootdir}/includes/dependencies.adoc[]

[source,xml]
----

    io.helidon.messaging.mock
    helidon-messaging-mock

----

== Usage

WARNING: Mock connector should be used in the test environment only!

For injecting Mock Connector use `@TestConnector` qualifier:
[source,java]
----
include::{sourcedir}/mp/reactivemessaging/MockSnippets.java[tag=snippet_1, indent=0]
----

=== Emitting Data

.Emitting String values `a`, `b`, `c`
[source,java]
----
include::{sourcedir}/mp/reactivemessaging/MockSnippets.java[tag=snippet_2, indent=0]
----
<1> Get incoming channel of given name and payload type

=== Asserting Data

.Awaiting and asserting payloads with custom mapper
[source,java]
----
include::{sourcedir}/mp/reactivemessaging/MockSnippets.java[tag=snippet_3, indent=0]
----
<1> Get outgoing channel of given name and payload type
<2> Request number of expected items and block the thread until items arrive then assert the payloads

== Configuration
|===
|Key            |Default value   | Description
|mock-data      |                | Initial data emitted to the channel immediately after subscription
|mock-data-type |java.lang.String| Type of the emitted initial data to be emitted
|===

== Helidon Test with Mock Connector
Mock connector works great with built-in Helidon test support for
link:/{rootdir}/testing/testing.adoc[JUnit 5] or link:/{rootdir}/testing/testing-ng.adoc[TestNG].

As Helidon test support makes a bean out of your test, you can inject MockConnector directly into it.

[source,java]
----
include::{sourcedir}/mp/reactivemessaging/MockSnippets.java[tag=snippet_4, indent=0]
----

<1> If you want to add all the beans manually
<2> Manually add MockConnector bean, so it is accessible by messaging for constructing the channels
<3> Messaging support in Helidon MP is provided by this CDI extension
<4> Instruct messaging to use `mock-connector` as an upstream for channel `test-channel-in`
<5> Generate mock data of `java.lang.Integer`, String is default
<6> Generate mock data
<7> Instruct messaging to use `mock-connector` as a downstream for channel `test-channel-out`
<8> Inject mock connector so we can access publishers and subscribers registered within the mock connector
<9> Messaging processing method connecting together channels `test-channel-in` and `test-channel-out`
<10> Actual JUnit 5 test method which is going to block the thread until 3 items are intercepted on `test-channel-out`
channel's downstream and assert those with expected values.





© 2015 - 2024 Weber Informatics LLC | Privacy Policy