Skip to content

Commit 8f6f171

Browse files
committed
Added tests for null-object pattern
1 parent b4dcec4 commit 8f6f171

4 files changed

Lines changed: 203 additions & 0 deletions

File tree

null-object/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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.iluwatar.nullobject;
2+
3+
import org.junit.Test;
4+
import org.mockito.Mockito;
5+
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertNotNull;
8+
import static org.junit.Assert.assertNull;
9+
import static org.junit.Assert.assertSame;
10+
11+
/**
12+
* Date: 12/26/15 - 11:47 PM
13+
*
14+
* @author Jeroen Meulemeester
15+
*/
16+
public class NullNodeTest extends StdOutTest {
17+
18+
/**
19+
* Verify if {@link NullNode#getInstance()} actually returns the same object instance
20+
*/
21+
@Test
22+
public void testGetInstance() {
23+
final NullNode instance = NullNode.getInstance();
24+
assertNotNull(instance);
25+
assertSame(instance, NullNode.getInstance());
26+
}
27+
28+
@Test
29+
public void testFields() {
30+
final NullNode node = NullNode.getInstance();
31+
assertEquals(0, node.getTreeSize());
32+
assertNull(node.getName());
33+
assertNull(node.getLeft());
34+
assertNull(node.getRight());
35+
}
36+
37+
@Test
38+
public void testWalk() throws Exception {
39+
NullNode.getInstance().walk();
40+
Mockito.verifyZeroInteractions(getStdOutMock());
41+
}
42+
43+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.iluwatar.nullobject;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
6+
import java.io.PrintStream;
7+
8+
import static org.mockito.Mockito.mock;
9+
10+
/**
11+
* Date: 12/10/15 - 8:37 PM
12+
*
13+
* @author Jeroen Meulemeester
14+
*/
15+
public abstract class StdOutTest {
16+
17+
/**
18+
* The mocked standard out {@link PrintStream}, required since walking through the tree has no
19+
* influence on any other accessible object, except for writing to std-out using {@link
20+
* System#out}
21+
*/
22+
private final PrintStream stdOutMock = mock(PrintStream.class);
23+
24+
/**
25+
* Keep the original std-out so it can be restored after the test
26+
*/
27+
private final PrintStream stdOutOrig = System.out;
28+
29+
/**
30+
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
31+
*/
32+
@Before
33+
public void setUp() {
34+
System.setOut(this.stdOutMock);
35+
}
36+
37+
/**
38+
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
39+
*/
40+
@After
41+
public void tearDown() {
42+
System.setOut(this.stdOutOrig);
43+
}
44+
45+
/**
46+
* Get the mocked stdOut {@link PrintStream}
47+
*
48+
* @return The stdOut print stream mock, renewed before each test
49+
*/
50+
final PrintStream getStdOutMock() {
51+
return this.stdOutMock;
52+
}
53+
54+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.iluwatar.nullobject;
2+
3+
import org.junit.Test;
4+
import org.mockito.InOrder;
5+
import org.mockito.Mockito;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertNotNull;
9+
import static org.junit.Assert.assertSame;
10+
11+
/**
12+
* Date: 12/26/15 - 11:44 PM
13+
*
14+
* @author Jeroen Meulemeester
15+
*/
16+
public class TreeTest extends StdOutTest {
17+
18+
/**
19+
* During the tests, the same tree structure will be used, shown below. End points will be
20+
* terminated with the {@link NullNode} instance.
21+
*
22+
* <pre>
23+
* root
24+
* ├── level1_a
25+
* │   ├── level2_a
26+
* │   │   ├── level3_a
27+
* │   │   └── level3_b
28+
* │   └── level2_b
29+
* └── level1_b
30+
* </pre>
31+
*/
32+
private static final Node TREE_ROOT;
33+
34+
static {
35+
final NodeImpl level1_b = new NodeImpl("level1_b", NullNode.getInstance(), NullNode.getInstance());
36+
final NodeImpl level2_b = new NodeImpl("level2_b", NullNode.getInstance(), NullNode.getInstance());
37+
final NodeImpl level3_a = new NodeImpl("level3_a", NullNode.getInstance(), NullNode.getInstance());
38+
final NodeImpl level3_b = new NodeImpl("level3_b", NullNode.getInstance(), NullNode.getInstance());
39+
final NodeImpl level2_a = new NodeImpl("level2_a", level3_a, level3_b);
40+
final NodeImpl level1_a = new NodeImpl("level1_a", level2_a, level2_b);
41+
TREE_ROOT = new NodeImpl("root", level1_a, level1_b);
42+
}
43+
44+
/**
45+
* Verify the number of items in the tree. The root has 6 children so we expect a {@link
46+
* Node#getTreeSize()} of 7 {@link Node}s in total.
47+
*/
48+
@Test
49+
public void testTreeSize() {
50+
assertEquals(7, TREE_ROOT.getTreeSize());
51+
}
52+
53+
/**
54+
* Walk through the tree and verify if every item is handled
55+
*/
56+
@Test
57+
public void testWalk() {
58+
TREE_ROOT.walk();
59+
60+
final InOrder inOrder = Mockito.inOrder(getStdOutMock());
61+
inOrder.verify(getStdOutMock()).println("root");
62+
inOrder.verify(getStdOutMock()).println("level1_a");
63+
inOrder.verify(getStdOutMock()).println("level2_a");
64+
inOrder.verify(getStdOutMock()).println("level3_a");
65+
inOrder.verify(getStdOutMock()).println("level3_b");
66+
inOrder.verify(getStdOutMock()).println("level2_b");
67+
inOrder.verify(getStdOutMock()).println("level1_b");
68+
inOrder.verifyNoMoreInteractions();
69+
}
70+
71+
@Test
72+
public void testGetLeft() throws Exception {
73+
final Node level1 = TREE_ROOT.getLeft();
74+
assertNotNull(level1);
75+
assertEquals("level1_a", level1.getName());
76+
assertEquals(5, level1.getTreeSize());
77+
78+
final Node level2 = level1.getLeft();
79+
assertNotNull(level2);
80+
assertEquals("level2_a", level2.getName());
81+
assertEquals(3, level2.getTreeSize());
82+
83+
final Node level3 = level2.getLeft();
84+
assertNotNull(level3);
85+
assertEquals("level3_a", level3.getName());
86+
assertEquals(1, level3.getTreeSize());
87+
assertSame(NullNode.getInstance(), level3.getRight());
88+
assertSame(NullNode.getInstance(), level3.getLeft());
89+
}
90+
91+
@Test
92+
public void testGetRight() throws Exception {
93+
final Node level1 = TREE_ROOT.getRight();
94+
assertNotNull(level1);
95+
assertEquals("level1_b", level1.getName());
96+
assertEquals(1, level1.getTreeSize());
97+
assertSame(NullNode.getInstance(), level1.getRight());
98+
assertSame(NullNode.getInstance(), level1.getLeft());
99+
}
100+
101+
}

0 commit comments

Comments
 (0)