|
| 1 | +package com.iluwatar.visitor; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.Optional; |
| 6 | + |
| 7 | +import static org.mockito.Mockito.verify; |
| 8 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 9 | + |
| 10 | +/** |
| 11 | + * Date: 12/30/15 - 18:59 PM |
| 12 | + * |
| 13 | + * @author Jeroen Meulemeester |
| 14 | + */ |
| 15 | +public abstract class VisitorTest<V extends UnitVisitor> extends StdOutTest { |
| 16 | + |
| 17 | + /** |
| 18 | + * The tested visitor instance |
| 19 | + */ |
| 20 | + private final V visitor; |
| 21 | + |
| 22 | + /** |
| 23 | + * The optional expected response when being visited by a commander |
| 24 | + */ |
| 25 | + private final Optional<String> commanderResponse; |
| 26 | + |
| 27 | + /** |
| 28 | + * The optional expected response when being visited by a sergeant |
| 29 | + */ |
| 30 | + private final Optional<String> sergeantResponse; |
| 31 | + |
| 32 | + /** |
| 33 | + * The optional expected response when being visited by a soldier |
| 34 | + */ |
| 35 | + private final Optional<String> soldierResponse; |
| 36 | + |
| 37 | + /** |
| 38 | + * Create a new test instance for the given visitor |
| 39 | + * |
| 40 | + * @param commanderResponse The optional expected response when being visited by a commander |
| 41 | + * @param sergeantResponse The optional expected response when being visited by a sergeant |
| 42 | + * @param soldierResponse The optional expected response when being visited by a soldier |
| 43 | + */ |
| 44 | + public VisitorTest(final V visitor, final Optional<String> commanderResponse, |
| 45 | + final Optional<String> sergeantResponse, final Optional<String> soldierResponse) { |
| 46 | + |
| 47 | + this.visitor = visitor; |
| 48 | + this.commanderResponse = commanderResponse; |
| 49 | + this.sergeantResponse = sergeantResponse; |
| 50 | + this.soldierResponse = soldierResponse; |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testVisitCommander() { |
| 55 | + this.visitor.visitCommander(new Commander()); |
| 56 | + if (this.commanderResponse.isPresent()) { |
| 57 | + verify(getStdOutMock()).println(this.commanderResponse.get()); |
| 58 | + } |
| 59 | + verifyNoMoreInteractions(getStdOutMock()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testVisitSergeant() { |
| 64 | + this.visitor.visitSergeant(new Sergeant()); |
| 65 | + if (this.sergeantResponse.isPresent()) { |
| 66 | + verify(getStdOutMock()).println(this.sergeantResponse.get()); |
| 67 | + } |
| 68 | + verifyNoMoreInteractions(getStdOutMock()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testVisitSoldier() { |
| 73 | + this.visitor.visitSoldier(new Soldier()); |
| 74 | + if (this.soldierResponse.isPresent()) { |
| 75 | + verify(getStdOutMock()).println(this.soldierResponse.get()); |
| 76 | + } |
| 77 | + verifyNoMoreInteractions(getStdOutMock()); |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments