Added tests for prototype pattern

This commit is contained in:
Jeroen Meulemeester 2015-12-28 21:01:25 +01:00
parent a3b1265921
commit 9d4c3154b1
3 changed files with 110 additions and 0 deletions

View File

@ -14,5 +14,10 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,39 @@
package com.iluwatar.prototype;
import org.junit.Test;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
/**
* Date: 12/28/15 - 8:34 PM
*
* @author Jeroen Meulemeester
*/
public class HeroFactoryImplTest {
@Test
public void testFactory() throws Exception {
final Mage mage = mock(Mage.class);
final Warlord warlord = mock(Warlord.class);
final Beast beast = mock(Beast.class);
when(mage.clone()).thenThrow(CloneNotSupportedException.class);
when(warlord.clone()).thenThrow(CloneNotSupportedException.class);
when(beast.clone()).thenThrow(CloneNotSupportedException.class);
final HeroFactoryImpl factory = new HeroFactoryImpl(mage, warlord, beast);
assertNull(factory.createMage());
assertNull(factory.createWarlord());
assertNull(factory.createBeast());
verify(mage).clone();
verify(warlord).clone();
verify(beast).clone();
verifyNoMoreInteractions(mage, warlord, beast);
}
}

View File

@ -0,0 +1,66 @@
package com.iluwatar.prototype;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
/**
* Date: 12/28/15 - 8:45 PM
*
* @author Jeroen Meulemeester
*/
@RunWith(Parameterized.class)
public class PrototypeTest<P extends Prototype> {
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(
new Object[]{new OrcBeast(), "Orcish wolf"},
new Object[]{new OrcMage(), "Orcish mage"},
new Object[]{new OrcWarlord(), "Orcish warlord"},
new Object[]{new ElfBeast(), "Elven eagle"},
new Object[]{new ElfMage(), "Elven mage"},
new Object[]{new ElfWarlord(), "Elven warlord"}
);
}
/**
* The tested prototype instance
*/
private final Prototype testedPrototype;
/**
* The expected {@link Prototype#toString()} value
*/
private final String expectedToString;
/**
* Create a new test instance, using the given test object and expected value
*
* @param testedPrototype The tested prototype instance
* @param expectedToString The expected {@link Prototype#toString()} value
*/
public PrototypeTest(final Prototype testedPrototype, final String expectedToString) {
this.expectedToString = expectedToString;
this.testedPrototype = testedPrototype;
}
@Test
public void testPrototype() throws Exception {
assertEquals(this.expectedToString, this.testedPrototype.toString());
final Object clone = this.testedPrototype.clone();
assertNotNull(clone);
assertNotSame(clone, this.testedPrototype);
assertSame(this.testedPrototype.getClass(), clone.getClass());
}
}