1

I have the following bit of code below and need to test the email is sent when a user is suspended.

  def suspendClient(client: Client, event: Event): EventResult = {
    Logger.debug(String.format(s"Found Client[${client.getName}]"));
    subService.suspend(client)
    Mailer.sendEmailClientSuspended(client)
    WebHookEventDAO.completeEvent(event.getId)
    EventResult.ok
  }

The main bit of logic i'm trying to test is Mailer.sendEmailClientSuspended(client) is invoked with the correct args e.g the correct Client is passed. Is it worth splitting it up into a seperate function and how difficult is it to test a 'Object' in Scala since Mailer is an Object.

unleashed
  • 915
  • 2
  • 16
  • 35

1 Answers1

0

Assuming you're writing your tests in Scala with MockitoSugar and ScalaTest you want to be using an ArgumentCaptor from the Mockito library. This allows you to capture the value of the client object passed as the parameter to the fundtion sendEmailClientSuspended(client).

See this stackoverflow post for a worked example you can follow. You'll need to specify the package your Client class is in, so something like this...

val capturedClient = ArgumentCaptor.forClass(classOf[com.acme.app.Client])

If your Mailer object doesn't extent a Trait currently, add one so you can mock the Trait and call verify() on it.

If you don't own the code to Mailer, you can move the call out into it's own helper class that you write, and you can then mock the new Trait. Something like this...

trait MailerTrait {
  def sendEmailClientSuspended(client: Client): Unit
}

object MyMailer extends MailerTrait () {
    def sendEmailClientSuspended(client: Client): Unit = {
        Mailer.sendEmailClientSuspended(client)
    }
}
Community
  • 1
  • 1
Brad
  • 15,186
  • 11
  • 60
  • 74