|
| 1 | +package com.iluwatar.mediator; |
| 2 | + |
| 3 | +import org.junit.After; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.runner.RunWith; |
| 7 | +import org.junit.runners.Parameterized; |
| 8 | + |
| 9 | +import java.io.PrintStream; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Collection; |
| 12 | +import java.util.function.Supplier; |
| 13 | + |
| 14 | +import static org.junit.Assert.assertEquals; |
| 15 | +import static org.mockito.Mockito.mock; |
| 16 | +import static org.mockito.Mockito.verify; |
| 17 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 18 | +import static org.mockito.Mockito.verifyZeroInteractions; |
| 19 | + |
| 20 | +/** |
| 21 | + * Date: 12/19/15 - 10:13 PM |
| 22 | + * |
| 23 | + * @author Jeroen Meulemeester |
| 24 | + */ |
| 25 | +@RunWith(Parameterized.class) |
| 26 | +public class PartyMemberTest { |
| 27 | + |
| 28 | + @Parameterized.Parameters |
| 29 | + public static Collection<Supplier<PartyMember>[]> data() { |
| 30 | + return Arrays.asList( |
| 31 | + new Supplier[]{Hobbit::new}, |
| 32 | + new Supplier[]{Hunter::new}, |
| 33 | + new Supplier[]{Rogue::new}, |
| 34 | + new Supplier[]{Wizard::new} |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * The mocked standard out {@link PrintStream}, required since some actions on a {@link |
| 40 | + * PartyMember} have any influence on any other accessible objects, except for writing to std-out |
| 41 | + * using {@link System#out} |
| 42 | + */ |
| 43 | + private final PrintStream stdOutMock = mock(PrintStream.class); |
| 44 | + |
| 45 | + /** |
| 46 | + * Keep the original std-out so it can be restored after the test |
| 47 | + */ |
| 48 | + private final PrintStream stdOutOrig = System.out; |
| 49 | + |
| 50 | + /** |
| 51 | + * Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test |
| 52 | + */ |
| 53 | + @Before |
| 54 | + public void setUp() { |
| 55 | + System.setOut(this.stdOutMock); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Removed the mocked std-out {@link PrintStream} again from the {@link System} class |
| 60 | + */ |
| 61 | + @After |
| 62 | + public void tearDown() { |
| 63 | + System.setOut(this.stdOutOrig); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * The factory, used to create a new instance of the tested party member |
| 68 | + */ |
| 69 | + private final Supplier<PartyMember> memberSupplier; |
| 70 | + |
| 71 | + /** |
| 72 | + * Create a new test instance, using the given {@link PartyMember} factory |
| 73 | + * |
| 74 | + * @param memberSupplier The party member factory |
| 75 | + */ |
| 76 | + public PartyMemberTest(final Supplier<PartyMember> memberSupplier) { |
| 77 | + this.memberSupplier = memberSupplier; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Verify if a party action triggers the correct output to the std-Out |
| 82 | + */ |
| 83 | + @Test |
| 84 | + public void testPartyAction() { |
| 85 | + final PartyMember member = this.memberSupplier.get(); |
| 86 | + |
| 87 | + for (final Action action : Action.values()) { |
| 88 | + member.partyAction(action); |
| 89 | + verify(this.stdOutMock).println(member.toString() + " " + action.getDescription()); |
| 90 | + } |
| 91 | + |
| 92 | + verifyNoMoreInteractions(this.stdOutMock); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Verify if a member action triggers the expected interactions with the party class |
| 97 | + */ |
| 98 | + @Test |
| 99 | + public void testAct() { |
| 100 | + final PartyMember member = this.memberSupplier.get(); |
| 101 | + |
| 102 | + member.act(Action.GOLD); |
| 103 | + verifyZeroInteractions(this.stdOutMock); |
| 104 | + |
| 105 | + final Party party = mock(Party.class); |
| 106 | + member.joinedParty(party); |
| 107 | + verify(this.stdOutMock).println(member.toString() + " joins the party"); |
| 108 | + |
| 109 | + for (final Action action : Action.values()) { |
| 110 | + member.act(action); |
| 111 | + verify(this.stdOutMock).println(member.toString() + " " + action.toString()); |
| 112 | + verify(party).act(member, action); |
| 113 | + } |
| 114 | + |
| 115 | + verifyNoMoreInteractions(party, this.stdOutMock); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Verify if {@link PartyMember#toString()} generate the expected output |
| 120 | + */ |
| 121 | + @Test |
| 122 | + public void testToString() throws Exception { |
| 123 | + final PartyMember member = this.memberSupplier.get(); |
| 124 | + final Class<? extends PartyMember> memberClass = member.getClass(); |
| 125 | + assertEquals(memberClass.getSimpleName(), member.toString()); |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments