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.
Maven Coordinates
To enable Mock Connector add the following dependency to your project’s pom.xml (see Managing Dependencies).
<dependency>
<groupId>io.helidon.messaging.mock</groupId>
<artifactId>helidon-messaging-mock</artifactId>
</dependency>content_copy
Usage
Mock connector should be used in the test environment only!
For injecting Mock Connector use @TestConnector qualifier:
@Inject
@TestConnector
MockConnector mockConnector;content_copy
Emitting Data
Emitting String values
a, b, cmockConnector
.incoming("my-incoming-channel", String.class)
.emit("a", "b", "c");content_copy
- Get incoming channel of given name and payload type
Asserting Data
Awaiting and asserting payloads with custom mapper
mockConnector
.outgoing("my-outgoing-channel", String.class)
.awaitData(TIMEOUT, Message::getPayload, "a", "b", "c"); content_copy
- Get outgoing channel of given name and payload type
- 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 JUnit 5 or TestNG.
As Helidon test support makes a bean out of your test, you can inject MockConnector directly into it.
@HelidonTest
@DisableDiscovery
@AddBean(MockConnector.class)
@AddExtension(MessagingCdiExtension.class)
@AddConfig(key = "mp.messaging.incoming.test-channel-in.connector", value = MockConnector.CONNECTOR_NAME)
@AddConfig(key = "mp.messaging.incoming.test-channel-in.mock-data-type", value = "java.lang.Integer")
@AddConfig(key = "mp.messaging.incoming.test-channel-in.mock-data", value = "6,7,8")
@AddConfig(key = "mp.messaging.outgoing.test-channel-out.connector", value = MockConnector.CONNECTOR_NAME)
public class MessagingTest {
private static final Duration TIMEOUT = Duration.ofSeconds(15);
@Inject
@TestConnector
private MockConnector mockConnector;
@Incoming("test-channel-in")
@Outgoing("test-channel-out")
int multiply(int payload) {
return payload * 10;
}
@Test
void testMultiplyChannel() {
mockConnector.outgoing("test-channel-out", Integer.TYPE)
.awaitPayloads(TIMEOUT, 60, 70, 80);
}
}content_copy
- If you want to add all the beans manually
- Manually add MockConnector bean, so it is accessible by messaging for constructing the channels
- Messaging support in Helidon MP is provided by this CDI extension
- Instruct messaging to use
mock-connectoras an upstream for channeltest-channel-in - Generate mock data of
java.lang.Integer, String is default - Generate mock data
- Instruct messaging to use
mock-connectoras a downstream for channeltest-channel-out - Inject mock connector so we can access publishers and subscribers registered within the mock connector
- Messaging processing method connecting together channels
test-channel-inandtest-channel-out - Actual JUnit 5 test method which is going to block the thread until 3 items are intercepted on
test-channel-outchannel’s downstream and assert those with expected values.