如何使用Junit测试二叉树?

时间:2017-04-04 16:52:15

标签: java junit binary-tree

如何为我编写的BinaryTree课程开发JUnit测试?

请提供建议或提供示例,以便我更好地了解如何在Junit中测试二叉树。

package binaryTree;

import javax.xml.soap.Node;

public class BinaryTree<T extends Comparable<T>> implements BTree<T> {
    private TreeNode root;
    Node current = (Node) root;
    @Override

    public void insert(T value) {
        if (root == null) {
            root = new TreeNode(value);
            } else if (value.compareTo(value()) < 0) {
            root.getleft().insert(value);
            } else {
            root.right().insert(value);
            }
    }

    @Override
    public T value() {
        if (this.root != null) {
            return (T) this.root.value();
        } else {
            return null;
        }
    }

    @Override
    public BTree<T> left() {
        if (this.root != null) {
            return this.root.getleft();
        } else {
            return null;
        }
    }

    @Override
    public BTree<T> right() {
        if (this.root != null) {
            return this.root.right();
        } else {
            return null;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

绝对阅读@ tpitsch帖子中的文档。但这是一个让你入门的简单例子。

import static org.junit.Assert.*;
import org.junit.Test;

// This test class contains two test methods
public class SimpleTest {

    private int add(int a, int b) {
        return a + b;
    }

    @Test public void test1() throws Exception
    {
        System.out.println("@Test1");
        assertEquals(add(1, 1), 2);
    }

    @Test public void test2() throws Exception
    {
        System.out.println("@Test2");
        assertEquals(add(100, -30), 70);
    }
}

我们正在测试函数add

具有@Test注释的每个函数都是JUnit测试方法。每个测试方法都作为单独的JUnit测试运行。函数名test1()test2()并不重要。

在测试方法中,您可以放置​​assertions,例如assertEquals(),以确保add函数按预期运行。