[英]unit test failing in java and not sure why
I have a method in class TreeUtils that checks for simalarity of structure between two binary search trees 我在类TreeUtils中有一个方法可以检查两个二进制搜索树之间结构的奇异性
if(root1 == null&& root2 == null)
{
return true;
}
if(root1 == null && root2!=null){
return false;
}
if(root1 != null && root2 == null)
{
return false;
}
boolean leftRecurse = similar(root1.getLeft(), root2.getLeft());
boolean rightRecurse = similar(root1.getRight(), root2.getRight());
return leftRecurse && rightRecurse;
however when I run a unit test on this file it fails. 但是,当我对此文件运行单元测试时,它会失败。 But if I call this method from the main method, it works. 但是,如果我从main方法调用此方法,它将起作用。 Its not a package or scope issue, because the similar method works in the main method. 它不是包或范围的问题,因为类似的方法在main方法中起作用。 I think it has something to do with 我认为这与
public BinaryTreeNode getLeft() {
assert(this.hasLeft());
return this.left;
}
maybe because this is now a unit test it is calling the assert? 也许因为这是现在的单元测试,所以它调用了断言? how should I modify my similar method to avoid this. 我应该如何修改类似的方法来避免这种情况。
This is my Unit test 这是我的单元测试
public void testSimilar() {
System.out.println("Test similarity");
SimpleBST tree = new SimpleBST();
tree.insert(1);
tree.insert(2);
SimpleBST tree2 = new SimpleBST();
tree2.insert(1);
tree2.insert(3);
assertEquals(true, tree.similar(tree2));
}
thanks. 谢谢。
Assuming you have asserts enabled with -ea argument the program will throw an AssertionError if this does not have a left child. 假设您已使用-ea参数启用了断言,如果该程序没有左子级,则该程序将引发AssertionError。 But by the setup you have, recursively you want to evaluate the left and right child even if they are null. 但是通过设置,您需要递归计算左,右子级,即使它们为空。 Simply removing the assertion will work. 只需删除断言即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.