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>
Usage
|
Warning
|
Mock connector should be used in the test environment only! |
For injecting Mock Connector use @TestConnector qualifier:
@Inject
@TestConnector
MockConnector mockConnector;
Emitting Data
a, b, cmockConnector.incoming("my-incoming-channel", String.class) // (1)
.emit("a", "b", "c");
-
Get incoming channel of given name and payload type
Asserting Data
mockConnector
.outgoing("my-outgoing-channel", String.class) // (1)
.awaitData(TIMEOUT, Message::getPayload, "a", "b", "c"); // (2)
-
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
As Helidon test support makes a bean out of your test, you can inject MockConnector directly into it.
@HelidonTest
@DisableDiscovery // (1)
@AddBean(MockConnector.class) // (2)
@AddExtension(MessagingCdiExtension.class) // (3)
@AddConfig(key = "mp.messaging.incoming.test-channel-in.connector", value = MockConnector.CONNECTOR_NAME) // (4)
@AddConfig(key = "mp.messaging.incoming.test-channel-in.mock-data-type", value = "java.lang.Integer") // (5)
@AddConfig(key = "mp.messaging.incoming.test-channel-in.mock-data", value = "6,7,8") // (6)
@AddConfig(key = "mp.messaging.outgoing.test-channel-out.connector", value = MockConnector.CONNECTOR_NAME) // (7)
public class MessagingTest {
private static final Duration TIMEOUT = Duration.ofSeconds(15);
@Inject
@TestConnector
private MockConnector mockConnector; // (8)
@Incoming("test-channel-in")
@Outgoing("test-channel-out")
int multiply(int payload) { // (9)
return payload * 10;
}
@Test
void testMultiplyChannel() {
mockConnector.outgoing("test-channel-out", Integer.TYPE) // (10)
.awaitPayloads(TIMEOUT, 60, 70, 80);
}
}
-
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.