Mocking mail-send connector in JUnit tests

Hello,

I want to write a JUnit test for a process which contains several service tasks sending an e-mail using the connector with the id ‘mail-send’. I have tested the connector separately and want to mock the connector in the process test so that no mails are send, i.e. the service tasks with connectors just do nothing.

How can I achieve that?

Thanks!

Best regards,
Kerstin

Hi Kerstin,

add
Mocks.register("createEmailDelegate", new LoggerDelegate());

and use an expression delegate ${createEmailDelegate} in the process definition.

Or inject the the mailService in the delegate and mock this service with a framework of your choice.

Hope this helps,

Ingo

1 Like

Hi Ingo,

thanks for your fast reply!

Actually, I do not want to change the process definition just for test purposes. The JUnit test runs every time I deploy the process on the server. So that test must work with the “real” process definition. In the process definition I have defined “Connector” as implementation for the service task and configured the connector to send mails correctly. In the test I need a way to e.g. mock the call of SendMailConnector.execute, so that no mail is sent.

I hope that described my problem more clearly.

Best regards,
Kerstin

Oh, sorry, didn’t found that you use the connector.

They are hard to mock, but you can try to resolve the url by an expression that points to a dummy-server in your jUnit test.

Cheers, Ingo

I agree that connectors are not easy to mock in their current implementation.

One idea could be: Add a maven profile that excludes the actual mail-connector and includes a mock connector that you implement yourself and that registers with the same name as the mail connector. Of course, that requires your test to always run with that Maven profile and other tests that actually use the mail connector cannot be run in the same reactor. Let me know if you need pointers on how to develop a custom connector.

Cheers,
Thorben

1 Like

Hi,

in addition to Thorben’s way, you can also use a mock mail server, like GreenMail. I know that it’s maybe not so good as a mock connector but easy to start with. You can have a look at the test case in the mail connector project.

Beside that, I would be a good feature for the mockito extension.

Best regards,
Philipp

I’ve chosen the solution with GreenMail. My tests are working now without sending “real” e-mails.

Thanks!

1 Like