Skip to content

Instantly share code, notes, and snippets.

@jaredsburrows
Created January 28, 2018 21:27
Show Gist options
  • Save jaredsburrows/32ee89f2834abac0c266fd42cf1d2dbd to your computer and use it in GitHub Desktop.
Save jaredsburrows/32ee89f2834abac0c266fd42cf1d2dbd to your computer and use it in GitHub Desktop.
Unit test with System.out
import api.TreeNode;
public final class BstPreOrder {
public static void printPreOrder(TreeNode<Integer> node) {
if (node == null) {
return;
}
System.out.print(node.value + " ");
printPreOrder(node.left);
printPreOrder(node.right);
}
}
import api.TreeNode
import test.BaseSpec
final class BstPreOrderSpock extends BaseSpec {
def outContent = new ByteArrayOutputStream()
def errContent = new ByteArrayOutputStream()
def tree = new TreeNode<>(8)
// (8)
// / \
// (2) (21)
// / \ /
// (1) (5) (13)
// /
// (3)
def "setup"() {
System.setOut(new PrintStream(outContent))
System.setErr(new PrintStream(errContent))
tree.right = new TreeNode<>(21)
tree.right.left = new TreeNode<>(13)
tree.left = new TreeNode<>(2)
tree.left.left = new TreeNode<>(1)
tree.left.right = new TreeNode<>(5)
tree.left.right.left = new TreeNode<>(3)
}
def "cleanup"() {
System.setOut(null)
System.setErr(null)
}
def "printPreOrder"() {
when:
BstPreOrder.printPreOrder(tree)
then:
outContent.toString().trim() == "8 2 1 5 3 21 13"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment