Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Mockito mocked EmailSender in tests #2547

Merged
merged 4 commits into from
Jul 29, 2024

Conversation

timyates
Copy link
Collaborator

As part of investigating removing the mocking from the tests (for #2534), this switches from using a mock(EmailSender) to creating a bean under test which tracks the calls made to it in a simple list.

If the replace.mailjet.factory property is set in a test then the standard MailJetFactory is replaced by our factory.

This generates EmailSender instances (for HTML and TEXT) which track the calls to their methods in a list of strings.

We can also remove the setEmailSender methods which we had to use to force the mock bean into the running services.

I believe this has uncovered a bug in the Guild Service whereby too many emails are being sent. (see com.objectcomputing.checkins.services.guild.GuildControllerTest#testEmailSentToGuildLeadWhenGuildMembersAdded) I would have expected a single email, but we are sending 2... 🤔

As part of investigating removing the mocking from the tests (for #2534), this switches from using a `mock(EmailSender)` to creating a bean under test which tracks the calls made to it in a simple list.

If the `replace.mailjet.factory` property is set in a test then the standard `MailJetFactory` is replaced by our factory.

This generates EmailSender instances (for HTML and TEXT) which track the calls to their methods in a list of strings.

We can also remove the `setEmailSender` methods which we had to use to force the mock bean into the running services.

I believe this has uncovered a bug in the Guild Service whereby too many emails are being sent.
(see `com.objectcomputing.checkins.services.guild.GuildControllerTest#testEmailSentToGuildLeadWhenGuildMembersAdded`)
I would have expected a single email, but we are sending 2... 🤔
@timyates timyates requested a review from mkimberlin July 12, 2024 11:28
@timyates timyates self-assigned this Jul 12, 2024
Comment on lines +101 to 110
assertEquals(2, emailSender.events.size());
assertEquals(
List.of(
// Email to the Guild lead
List.of("SEND_EMAIL", "null", "null", "Membership changes have been made to the Ninja guild", "<h3>Bill Charles has joined the Ninja guild.</h3><a href=\"https://checkins.objectcomputing.com/guilds\">Click here</a> to view the changes in the Check-Ins app.", memberProfile.getWorkEmail() + "," + memberProfile2.getWorkEmail()),
// Email to both the Guild leads
List.of("SEND_EMAIL", "null", "null", "Membership Changes have been made to the Ninja guild", "<h3>Changes have been made to the Ninja guild.</h3><h4>The following members have been added:</h4><ul><li>Bill Charles</li></ul><a href=\"https://checkins.objectcomputing.com/guilds\">Click here</a> to view the changes in the Check-Ins app.", memberProfile.getWorkEmail() + "," + memberProfile2.getWorkEmail())
),
emailSender.events
);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is a bug and we may be sending too many emails

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raised #2549

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potential PR to fix is in #2551

If that gets merged, we need to remove one of the Lists in this assert (and reduce the size assertion to 1)

Comment on lines +135 to 141
assertEquals(2, emailSender.events.size());
assertEquals(List.of(
List.of("SEND_EMAIL", "null", "null", "Membership Changes have been made to the Ninja guild", "<h3>Bill Charles has left the Ninja guild.</h3><a href=\"https://checkins.objectcomputing.com/guilds\">Click here</a> to view the changes in the Check-Ins app.", memberProfile.getWorkEmail()),
List.of("SEND_EMAIL", "null", "null", "Membership Changes have been made to the Ninja guild", "<h3>Changes have been made to the Ninja guild.</h3><h4>The following members have been removed:</h4><ul><li>Bill Charles</li></ul><a href=\"https://checkins.objectcomputing.com/guilds\">Click here</a> to view the changes in the Check-Ins app.", memberProfile.getWorkEmail())
),
emailSender.events
);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here... I believe this is too many emails being sent 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raised #2549

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potential PR to fix is in #2551

If that gets merged, we need to remove one of the Lists in this assert (and reduce the size assertion to 1)

@mkimberlin mkimberlin merged commit 7786a0a into develop Jul 29, 2024
1 check passed
@mkimberlin mkimberlin deleted the feature-2534/replace-email-sender-mock branch July 29, 2024 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants