-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Mockito mocked EmailSender in tests
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... 🤔
- Loading branch information
Showing
13 changed files
with
494 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
server/src/test/java/com/objectcomputing/checkins/services/MailJetFactoryReplacement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.objectcomputing.checkins.services; | ||
|
||
import com.objectcomputing.checkins.notifications.email.EmailSender; | ||
import com.objectcomputing.checkins.notifications.email.MailJetFactory; | ||
import io.micronaut.context.annotation.Factory; | ||
import io.micronaut.context.annotation.Replaces; | ||
import io.micronaut.context.annotation.Requires; | ||
import io.micronaut.core.util.StringUtils; | ||
import jakarta.inject.Named; | ||
import jakarta.inject.Singleton; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* This class is a replacement for the MailJetFactory class. It provides a mock implementation of the EmailSender. | ||
* The mock implementation records the events that occur when the sendEmail, sendEmailReceivesStatus, and setEmailFormat | ||
* methods are called. | ||
* <p> | ||
* The mock implementation is used when the "replace.mailjet.factory" property is set to true. | ||
* <p> | ||
* This is used instead of Mockito so that it works when running nativeTest under GraalVM. | ||
*/ | ||
@Factory | ||
@Replaces(MailJetFactory.class) | ||
@Requires(property = "replace.mailjet.factory", value = StringUtils.TRUE) | ||
public class MailJetFactoryReplacement { | ||
|
||
@Singleton | ||
@Named(MailJetFactory.HTML_FORMAT) | ||
@Replaces(value = EmailSender.class, named = MailJetFactory.HTML_FORMAT) | ||
MockEmailSender getHtmlSender() { | ||
return new MockEmailSender(); | ||
} | ||
|
||
@Singleton | ||
@Named(MailJetFactory.TEXT_FORMAT) | ||
@Replaces(value = EmailSender.class, named = MailJetFactory.TEXT_FORMAT) | ||
MockEmailSender getTextSender() { | ||
return new MockEmailSender(); | ||
} | ||
|
||
public static class MockEmailSender implements EmailSender { | ||
|
||
public final List<List<String>> events = new ArrayList<>(); | ||
private RuntimeException exception; | ||
|
||
static String nullSafe(String s) { | ||
return s == null ? "null" : s; | ||
} | ||
|
||
public void reset() { | ||
exception = null; | ||
events.clear(); | ||
} | ||
|
||
public void setException(RuntimeException exception) { | ||
this.exception = exception; | ||
} | ||
|
||
private void maybeThrow() { | ||
if (exception != null) { | ||
throw exception; | ||
} | ||
} | ||
|
||
@Override | ||
public void sendEmail(String fromName, String fromAddress, String subject, String content, String... recipients) { | ||
events.add(List.of( | ||
"SEND_EMAIL", | ||
nullSafe(fromName), | ||
nullSafe(fromAddress), | ||
nullSafe(subject), | ||
nullSafe(content), | ||
Arrays.stream(recipients).map(MockEmailSender::nullSafe).collect(Collectors.joining(",")) | ||
)); | ||
maybeThrow(); | ||
} | ||
|
||
@Override | ||
public boolean sendEmailReceivesStatus(String fromName, String fromAddress, String subject, String content, String... recipients) { | ||
events.add(List.of( | ||
"SEND_EMAIL_RECEIVES_STATUS", | ||
nullSafe(fromName), | ||
nullSafe(fromAddress), | ||
nullSafe(subject), | ||
nullSafe(content), | ||
Arrays.stream(recipients).map(MockEmailSender::nullSafe).collect(Collectors.joining(",")) | ||
)); | ||
maybeThrow(); | ||
return true; | ||
} | ||
|
||
@Override | ||
public void setEmailFormat(String format) { | ||
events.add(List.of( | ||
"SET_EMAIL_FORMAT", | ||
nullSafe(format) | ||
)); | ||
maybeThrow(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.