Skip to content

Commit 9d4c315

Browse files
committed
Added tests for prototype pattern
1 parent a3b1265 commit 9d4c315

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

prototype/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@
1414
<artifactId>junit</artifactId>
1515
<scope>test</scope>
1616
</dependency>
17+
<dependency>
18+
<groupId>org.mockito</groupId>
19+
<artifactId>mockito-core</artifactId>
20+
<scope>test</scope>
21+
</dependency>
1722
</dependencies>
1823
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.iluwatar.prototype;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertNull;
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.verify;
8+
import static org.mockito.Mockito.verifyNoMoreInteractions;
9+
import static org.mockito.Mockito.when;
10+
11+
/**
12+
* Date: 12/28/15 - 8:34 PM
13+
*
14+
* @author Jeroen Meulemeester
15+
*/
16+
public class HeroFactoryImplTest {
17+
18+
@Test
19+
public void testFactory() throws Exception {
20+
final Mage mage = mock(Mage.class);
21+
final Warlord warlord = mock(Warlord.class);
22+
final Beast beast = mock(Beast.class);
23+
24+
when(mage.clone()).thenThrow(CloneNotSupportedException.class);
25+
when(warlord.clone()).thenThrow(CloneNotSupportedException.class);
26+
when(beast.clone()).thenThrow(CloneNotSupportedException.class);
27+
28+
final HeroFactoryImpl factory = new HeroFactoryImpl(mage, warlord, beast);
29+
assertNull(factory.createMage());
30+
assertNull(factory.createWarlord());
31+
assertNull(factory.createBeast());
32+
33+
verify(mage).clone();
34+
verify(warlord).clone();
35+
verify(beast).clone();
36+
verifyNoMoreInteractions(mage, warlord, beast);
37+
}
38+
39+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.iluwatar.prototype;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.Parameterized;
6+
7+
import java.util.Arrays;
8+
import java.util.Collection;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertNotNull;
12+
import static org.junit.Assert.assertNotSame;
13+
import static org.junit.Assert.assertSame;
14+
15+
/**
16+
* Date: 12/28/15 - 8:45 PM
17+
*
18+
* @author Jeroen Meulemeester
19+
*/
20+
@RunWith(Parameterized.class)
21+
public class PrototypeTest<P extends Prototype> {
22+
23+
@Parameterized.Parameters
24+
public static Collection<Object[]> data() {
25+
return Arrays.asList(
26+
new Object[]{new OrcBeast(), "Orcish wolf"},
27+
new Object[]{new OrcMage(), "Orcish mage"},
28+
new Object[]{new OrcWarlord(), "Orcish warlord"},
29+
new Object[]{new ElfBeast(), "Elven eagle"},
30+
new Object[]{new ElfMage(), "Elven mage"},
31+
new Object[]{new ElfWarlord(), "Elven warlord"}
32+
);
33+
}
34+
35+
/**
36+
* The tested prototype instance
37+
*/
38+
private final Prototype testedPrototype;
39+
40+
/**
41+
* The expected {@link Prototype#toString()} value
42+
*/
43+
private final String expectedToString;
44+
45+
/**
46+
* Create a new test instance, using the given test object and expected value
47+
*
48+
* @param testedPrototype The tested prototype instance
49+
* @param expectedToString The expected {@link Prototype#toString()} value
50+
*/
51+
public PrototypeTest(final Prototype testedPrototype, final String expectedToString) {
52+
this.expectedToString = expectedToString;
53+
this.testedPrototype = testedPrototype;
54+
}
55+
56+
@Test
57+
public void testPrototype() throws Exception {
58+
assertEquals(this.expectedToString, this.testedPrototype.toString());
59+
60+
final Object clone = this.testedPrototype.clone();
61+
assertNotNull(clone);
62+
assertNotSame(clone, this.testedPrototype);
63+
assertSame(this.testedPrototype.getClass(), clone.getClass());
64+
}
65+
66+
}

0 commit comments

Comments
 (0)