Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

[SmallRye] smallrye - reactive messaging - writing test units

159 views
Skip to first unread message

Vincenzo D'Amore

unread,
Mar 26, 2024, 4:30:29 AM3/26/24
to smal...@googlegroups.com
Hi,

are there any documentation /examples that show how to write unit tests with reactive messaging?

I have a small project that explains how to run a simple unit test.
This project it is based on the current online documentation, but it raises a null pointer exception because the CDI is not going to inject the InMemoryConnector


Best regards,
Vincenzo D'Amore

--
Vincenzo D'Amore

Ozan Gunalp

unread,
Mar 26, 2024, 4:55:42 PM3/26/24
to SmallRye
If you annotate your test class with `@QuarkusTest` the CDI injection will work as expected.

We avoid adding Quarkus-specific examples in Smallrye Reactive Messaging documentation and code, but in this case I think it creates too much confusion.
There's already a proposition to amend this very example.

Cheers,
Ozan

Vincenzo D'Amore

unread,
Mar 30, 2024, 7:06:30 PM3/30/24
to smal...@googlegroups.com
Hi Ozan, 

I have annotated the test class with @QuarkusTest but a new problem arose.

I have 3 methods:

    @Outgoing("from-producer-to-processor")
    public ClassA produce() {
        return new ClassA("Hello");
    }

    @Incoming("from-producer-to-processor")
    @Outgoing("from-processor-to-consumer")
    public ClassB process(ClassA classA) {
        return new ClassB(String.format("%s-ish", classA.toString()));
    }

    @Incoming("from-processor-to-consumer")
    public void consume(ClassB classB) {
        log.infof("Consumer received %s", classB);
    }

When I start the test unit, @QuarkusTest starts Quarkus and the method produce() starts to send ClassA objects.
But I only want to test the process() method.

In other words, the test unit works only if I remove the annotation from the methods produce() and consume().

I have written a stackoverflow post and I have updated accordingly the example0 of my test repo

Can you help me?

Best regards,
Vincenzo

--
You received this message because you are subscribed to the Google Groups "SmallRye" group.
To unsubscribe from this group and stop receiving emails from it, send an email to smallrye+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/smallrye/11d423d6-f2b6-409a-908b-6c5c4044aaf1n%40googlegroups.com.


--
Vincenzo D'Amore

Ozan Gunalp

unread,
Apr 1, 2024, 3:44:56 AM4/1/24
to SmallRye
Hi Vincenzo, 

The idea behind InMemoryConnector is to replace a channel that is wired to a connector (like Kafka, AMQP 1.0, RabbitMQ etc.) so that you can simulate messages and assert received messages without needing a message broker.
Whereas in your code snippet everything happens already "in-memory": The outgoing `from-producer-to-processor` generates messages, that channel is automatically wired to the incoming with the same name which receives them in the process method and sends them to the `from-processor-to-consumer` outgoing channel. That last channel is wired to your last incoming method that logs messages.

Hope this helps,

Ozan

Vincenzo D'Amore

unread,
Apr 3, 2024, 12:01:15 PM4/3/24
to smal...@googlegroups.com
Hi Ozan,

reading again the message I realized I have mistyped the configuration, anyway I hope you understand.

I was trying to configure the outgoing connector "from-producer-to-processor" as dummy (and disable its generation), in order to have the chance to attach the smallrye-in-memory connector starting from
incoming.from-producer-to-processor to the rest of the pipeline.
 

On Tue, Apr 2, 2024 at 1:08 PM Vincenzo D'Amore <v.da...@gmail.com> wrote:
Hi Ozan,

is there a way to "selectively" attach the InMemoryConnector ?

I have tried without success with this configuration:

mp.messaging.outgoing.from-processor-to-consumer.connector=smallrye-in-memory
mp.messaging.outgoing.from-producer-to-processor.connector=smallrye-in-memory
mp.messaging.incoming.from-producer-to-processor.connector=dummy
mp.messaging.incoming.from-producer-to-processor.enabled=false

Do you think is feasible to create an EmptyConnector, a DummyConnector, something I can use as a source  for the unit test messages?




--
Vincenzo D'Amore

Vincenzo D'Amore

unread,
Apr 3, 2024, 12:01:16 PM4/3/24
to smal...@googlegroups.com
Hi Ozan,

is there a way to "selectively" attach the InMemoryConnector ?

I have tried without success with this configuration:

mp.messaging.outgoing.from-processor-to-consumer.connector=smallrye-in-memory
mp.messaging.outgoing.from-producer-to-processor.connector=smallrye-in-memory
mp.messaging.incoming.from-producer-to-processor.connector=dummy
mp.messaging.incoming.from-producer-to-processor.enabled=false

Do you think is feasible to create an EmptyConnector, a DummyConnector, something I can use as a source  for the unit test messages?


On Mon, Apr 1, 2024 at 9:45 AM Ozan Gunalp <ogu...@redhat.com> wrote:


--
Vincenzo D'Amore

Ralf Battenfeld

unread,
Dec 17, 2024, 6:47:36 AM12/17/24
to SmallRye
Dear All

I am jumping on this topic too. I am struggling with executing a simple unit test. I grabbed an example of a test case file from your repository. Including the same WELD setup and dependencies. The test file is: InMemoryConnectorTest

It seems to work except of this error:

Ralf Battenfeld

unread,
Dec 17, 2024, 6:50:26 AM12/17/24
to SmallRye
Sorry, I pressed the submit button too early. The error is:

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: java.lang.NoSuchMethodError: 'org.eclipse.microprofile.reactive.messaging.Metadata org.eclipse.microprofile.reactive.messaging.Message.getMetadata()'
at io.smallrye.reactive.messaging.providers.locals.LocalContextMetadata.invokeOnMessageContext(LocalContextMetadata.java:45)
at io.smallrye.reactive.messaging.providers.locals.LocalContextMetadata.invokeOnMessageContext(LocalContextMetadata.java:31)
at io.smallrye.reactive.messaging.providers.AbstractMediator.invokeOnMessageContext(AbstractMediator.java:169)
at io.smallrye.reactive.messaging.providers.ProcessorMediator.lambda$processMethodReturningIndividualPayloadAndConsumingIndividualItem$41(ProcessorMediator.java:373)
at io.smallrye.mutiny.operators.multi.MultiConcatMapOp$MainSubscriber.onItem(MultiConcatMapOp.java:117)

Is a depencency missing?

Any help is very helpful.

Regards, Ralf
Reply all
Reply to author
Forward
0 new messages