Add test for email action.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 17 Mar 2014 20:50:33 +0000 (21:50 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 17 Mar 2014 20:50:33 +0000 (21:50 +0100)
src/test/java/net/pterodactylus/rhynodge/actions/EmailActionTest.java [new file with mode: 0644]

diff --git a/src/test/java/net/pterodactylus/rhynodge/actions/EmailActionTest.java b/src/test/java/net/pterodactylus/rhynodge/actions/EmailActionTest.java
new file mode 100644 (file)
index 0000000..960a235
--- /dev/null
@@ -0,0 +1,51 @@
+package net.pterodactylus.rhynodge.actions;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import javax.mail.Address;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.Transport;
+
+import net.pterodactylus.rhynodge.output.DefaultOutput;
+import net.pterodactylus.rhynodge.output.Output;
+
+import org.junit.Test;
+
+/**
+ * Unit test for {@link EmailAction}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class EmailActionTest {
+
+       private final Transport transport = mock(Transport.class);
+       private final String sender = "sender@sender.de";
+       private final String recipient = "recipient@recipient.de";
+       private final EmailAction emailAction = new EmailAction(transport, sender, recipient);
+       private final Output output = new DefaultOutput("Action triggered!").addText("text/plain", "Plain Text.").addText("text/html", "<p>HTML Text.</p>");
+
+       @Test
+       public void canCreateActionWithNormalConstructor() {
+               new EmailAction("hostname", "sender", "recipient");
+       }
+
+       @Test
+       public void emailIsGeneratedCorrectly() throws MessagingException {
+               emailAction.execute(output);
+               verify(transport).sendMessage(any(Message.class), any(Address[].class));
+       }
+
+       @Test
+       public void exceptionWhenSendingIsSwallowed() throws MessagingException {
+               doThrow(MessagingException.class).doNothing().when(transport).sendMessage(any(Message.class), any(Address[].class));
+               emailAction.execute(output);
+               emailAction.execute(output);
+               verify(transport, times(2)).sendMessage(any(Message.class), any(Address[].class));
+       }
+
+}