Skip to content

Commit ced317b

Browse files
mfaridmfarid
mfarid
authored and
mfarid
committed
Added UnitTest cases for factory method.
1 parent e5614e5 commit ced317b

File tree

6 files changed

+99
-28
lines changed

6 files changed

+99
-28
lines changed

factory-method/pom.xml

+5
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>

factory-method/src/main/java/com/iluwatar/factory/method/ElfWeapon.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.iluwatar.factory.method;
22

33
/**
4-
*
5-
* ElfWeapon
6-
*
4+
* ElfWeapon.
75
*/
86
public class ElfWeapon implements Weapon {
97

@@ -17,4 +15,9 @@ public ElfWeapon(WeaponType weaponType) {
1715
public String toString() {
1816
return "Elven " + weaponType;
1917
}
18+
19+
@Override
20+
public WeaponType getWeaponType() {
21+
return weaponType;
22+
}
2023
}

factory-method/src/main/java/com/iluwatar/factory/method/OrcWeapon.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.iluwatar.factory.method;
22

33
/**
4-
*
5-
* OrcWeapon
6-
*
4+
* OrcWeapon.
75
*/
86
public class OrcWeapon implements Weapon {
97

@@ -17,4 +15,9 @@ public OrcWeapon(WeaponType weaponType) {
1715
public String toString() {
1816
return "Orcish " + weaponType;
1917
}
18+
19+
@Override
20+
public WeaponType getWeaponType() {
21+
return weaponType;
22+
}
2023
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.iluwatar.factory.method;
22

33
/**
4-
*
5-
* Weapon interface
6-
*
4+
* Weapon interface.
75
*/
86
public interface Weapon {
97

8+
WeaponType getWeaponType();
9+
1010
}

factory-method/src/test/java/com/iluwatar/factory/method/AppTest.java

-19
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.iluwatar.factory.method;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import org.junit.Test;
7+
8+
/**
9+
* The Factory Method is a creational design pattern which uses factory methods to deal with the
10+
* problem of creating objects without specifying the exact class of object that will be created.
11+
* This is done by creating objects via calling a factory method either specified in an interface
12+
* and implemented by child classes, or implemented in a base class and optionally overridden by
13+
* derived classes—rather than by calling a constructor.
14+
*
15+
* <p>Factory produces the object of its liking.
16+
* The weapon {@link Weapon} manufactured by the
17+
* blacksmith depends on the kind of factory implementation it is referring to.
18+
* </p>
19+
*/
20+
public class FactoryMethodTest {
21+
22+
/**
23+
* Testing {@link OrcBlacksmith} to produce a SPEAR asserting that the Weapon is an instance
24+
* of {@link OrcWeapon}.
25+
*/
26+
@Test
27+
public void testOrcBlacksmithWithSpear() {
28+
Blacksmith blacksmith = new OrcBlacksmith();
29+
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
30+
verifyWeapon(weapon, WeaponType.SPEAR, OrcWeapon.class);
31+
}
32+
33+
/**
34+
* Testing {@link OrcBlacksmith} to produce a AXE asserting that the Weapon is an instance
35+
* of {@link OrcWeapon}.
36+
*/
37+
@Test
38+
public void testOrcBlacksmithWithAxe() {
39+
Blacksmith blacksmith = new OrcBlacksmith();
40+
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
41+
verifyWeapon(weapon, WeaponType.AXE, OrcWeapon.class);
42+
}
43+
44+
/**
45+
* Testing {@link ElfBlacksmith} to produce a SHORT_SWORD asserting that the Weapon is an
46+
* instance of {@link ElfWeapon}.
47+
*/
48+
@Test
49+
public void testElfBlacksmithWithShortSword() {
50+
Blacksmith blacksmith = new ElfBlacksmith();
51+
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD);
52+
verifyWeapon(weapon, WeaponType.SHORT_SWORD, ElfWeapon.class);
53+
}
54+
55+
/**
56+
* Testing {@link ElfBlacksmith} to produce a SPEAR asserting that the Weapon is an instance
57+
* of {@link ElfWeapon}.
58+
*/
59+
@Test
60+
public void testElfBlacksmithWithSpear() {
61+
Blacksmith blacksmith = new ElfBlacksmith();
62+
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
63+
verifyWeapon(weapon, WeaponType.SPEAR, ElfWeapon.class);
64+
}
65+
66+
/**
67+
* This method asserts that the weapon object that is passed is an instance of the clazz and the
68+
* weapon is of type expectedWeaponType.
69+
*
70+
* @param weapon weapon object which is to be verified
71+
* @param expectedWeaponType expected WeaponType of the weapon
72+
* @param clazz expected class of the weapon
73+
*/
74+
private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class clazz) {
75+
assertTrue("Weapon must be an object of: " + clazz.getName(), clazz.isInstance(weapon));
76+
assertEquals("Weapon must be of weaponType: " + clazz.getName(), expectedWeaponType,
77+
weapon.getWeaponType());
78+
}
79+
}

0 commit comments

Comments
 (0)