Skip to content

Commit 5611f26

Browse files
committed
Added tests for specification pattern
1 parent d0cdf84 commit 5611f26

File tree

5 files changed

+227
-0
lines changed

5 files changed

+227
-0
lines changed

specification/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: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.iluwatar.specification.creature;
2+
3+
import com.iluwatar.specification.property.Color;
4+
import com.iluwatar.specification.property.Movement;
5+
import com.iluwatar.specification.property.Size;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.junit.runners.Parameterized;
10+
11+
import java.util.Arrays;
12+
import java.util.Collection;
13+
14+
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.assertNotNull;
16+
17+
/**
18+
* Date: 12/29/15 - 7:47 PM
19+
*
20+
* @author Jeroen Meulemeester
21+
*/
22+
@RunWith(Parameterized.class)
23+
public class CreatureTest {
24+
25+
/**
26+
* @return The tested {@link Creature} instance and its expected specs
27+
*/
28+
@Parameterized.Parameters
29+
public static Collection<Object[]> data() {
30+
return Arrays.asList(
31+
new Object[]{new Dragon(), "Dragon", Size.LARGE, Movement.FLYING, Color.RED},
32+
new Object[]{new Goblin(), "Goblin", Size.SMALL, Movement.WALKING, Color.GREEN},
33+
new Object[]{new KillerBee(), "KillerBee", Size.SMALL, Movement.FLYING, Color.LIGHT},
34+
new Object[]{new Octopus(), "Octopus", Size.NORMAL, Movement.SWIMMING, Color.DARK},
35+
new Object[]{new Shark(), "Shark", Size.NORMAL, Movement.SWIMMING, Color.LIGHT},
36+
new Object[]{new Troll(), "Troll", Size.LARGE, Movement.WALKING, Color.DARK}
37+
);
38+
}
39+
40+
/**
41+
* The tested creature
42+
*/
43+
private final Creature testedCreature;
44+
45+
/**
46+
* The expected name of the tested creature
47+
*/
48+
private final String name;
49+
50+
/**
51+
* The expected size of the tested creature
52+
*/
53+
private final Size size;
54+
55+
/**
56+
* The expected movement type of the tested creature
57+
*/
58+
private final Movement movement;
59+
60+
/**
61+
* The expected color of the tested creature
62+
*/
63+
private final Color color;
64+
65+
/**
66+
* @param testedCreature The tested creature
67+
* @param name The expected name of the creature
68+
* @param size The expected size of the creature
69+
* @param movement The expected movement type of the creature
70+
* @param color The expected color of the creature
71+
*/
72+
public CreatureTest(final Creature testedCreature, final String name, final Size size,
73+
final Movement movement, final Color color) {
74+
this.testedCreature = testedCreature;
75+
this.name = name;
76+
this.size = size;
77+
this.movement = movement;
78+
this.color = color;
79+
}
80+
81+
82+
@Test
83+
public void testGetName() throws Exception {
84+
assertEquals(this.name, this.testedCreature.getName());
85+
}
86+
87+
@Test
88+
public void testGetSize() throws Exception {
89+
assertEquals(this.size, this.testedCreature.getSize());
90+
}
91+
92+
@Test
93+
public void testGetMovement() throws Exception {
94+
assertEquals(this.movement, this.testedCreature.getMovement());
95+
}
96+
97+
@Test
98+
public void testGetColor() throws Exception {
99+
assertEquals(this.color, this.testedCreature.getColor());
100+
}
101+
102+
@Test
103+
public void testToString() throws Exception {
104+
final String toString = this.testedCreature.toString();
105+
assertNotNull(toString);
106+
assertEquals(
107+
String.format("%s [size=%s, movement=%s, color=%s]", name, size, movement, color),
108+
toString
109+
);
110+
}
111+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.iluwatar.specification.selector;
2+
3+
import com.iluwatar.specification.creature.Creature;
4+
import com.iluwatar.specification.property.Color;
5+
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertFalse;
9+
import static org.junit.Assert.assertTrue;
10+
import static org.mockito.Mockito.mock;
11+
import static org.mockito.Mockito.when;
12+
13+
/**
14+
* Date: 12/29/15 - 7:35 PM
15+
*
16+
* @author Jeroen Meulemeester
17+
*/
18+
public class ColorSelectorTest {
19+
20+
/**
21+
* Verify if the color selector gives the correct results
22+
*/
23+
@Test
24+
public void testColor() {
25+
final Creature greenCreature = mock(Creature.class);
26+
when(greenCreature.getColor()).thenReturn(Color.GREEN);
27+
28+
final Creature redCreature = mock(Creature.class);
29+
when(redCreature.getColor()).thenReturn(Color.RED);
30+
31+
final ColorSelector greenSelector = new ColorSelector(Color.GREEN);
32+
assertTrue(greenSelector.test(greenCreature));
33+
assertFalse(greenSelector.test(redCreature));
34+
35+
}
36+
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.iluwatar.specification.selector;
2+
3+
import com.iluwatar.specification.creature.Creature;
4+
import com.iluwatar.specification.property.Color;
5+
import com.iluwatar.specification.property.Movement;
6+
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertFalse;
10+
import static org.junit.Assert.assertTrue;
11+
import static org.mockito.Mockito.mock;
12+
import static org.mockito.Mockito.when;
13+
14+
/**
15+
* Date: 12/29/15 - 7:37 PM
16+
*
17+
* @author Jeroen Meulemeester
18+
*/
19+
public class MovementSelectorTest {
20+
21+
/**
22+
* Verify if the movement selector gives the correct results
23+
*/
24+
@Test
25+
public void testMovement() {
26+
final Creature swimmingCreature = mock(Creature.class);
27+
when(swimmingCreature.getMovement()).thenReturn(Movement.SWIMMING);
28+
29+
final Creature flyingCreature = mock(Creature.class);
30+
when(flyingCreature.getMovement()).thenReturn(Movement.FLYING);
31+
32+
final MovementSelector swimmingSelector = new MovementSelector(Movement.SWIMMING);
33+
assertTrue(swimmingSelector.test(swimmingCreature));
34+
assertFalse(swimmingSelector.test(flyingCreature));
35+
36+
}
37+
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.iluwatar.specification.selector;
2+
3+
import com.iluwatar.specification.creature.Creature;
4+
import com.iluwatar.specification.property.Size;
5+
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertFalse;
9+
import static org.junit.Assert.assertTrue;
10+
import static org.mockito.Mockito.mock;
11+
import static org.mockito.Mockito.when;
12+
13+
/**
14+
* Date: 12/29/15 - 7:43 PM
15+
*
16+
* @author Jeroen Meulemeester
17+
*/
18+
public class SizeSelectorTest {
19+
20+
/**
21+
* Verify if the size selector gives the correct results
22+
*/
23+
@Test
24+
public void testMovement() {
25+
final Creature normalCreature = mock(Creature.class);
26+
when(normalCreature.getSize()).thenReturn(Size.NORMAL);
27+
28+
final Creature smallCreature = mock(Creature.class);
29+
when(smallCreature.getSize()).thenReturn(Size.SMALL);
30+
31+
final SizeSelector normalSelector = new SizeSelector(Size.NORMAL);
32+
assertTrue(normalSelector.test(normalCreature));
33+
assertFalse(normalSelector.test(smallCreature));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)