1
\$\begingroup\$

I have written BST in Java for practice. How can I write better unit test cases. I am sure the way I have written them is not proper.

Code for BST

package com.r.basics.tree;
import org.junit.Before;
import org.junit.Test;

public class BinarySearchTreeTest{
private BinarySearchTree<Integer> binarySearchTreeSUT;

    @Before
    public void setUp() {
        binarySearchTreeSUT =  new BinarySearchTree<>();
    }

    @Test
    public void testShouldInsertItemToBST(){

        binarySearchTreeSUT.insert(4);
        binarySearchTreeSUT.insert(8);
        binarySearchTreeSUT.insert(2);
        binarySearchTreeSUT.insert(3);
    }

    @Test
    public void testShouldGetMaxItemFromBST(){

        binarySearchTreeSUT.insert(4);
        binarySearchTreeSUT.insert(8);
        binarySearchTreeSUT.insert(2);
        binarySearchTreeSUT.insert(3);

        assert(binarySearchTreeSUT.getMax()  == 8);
    }

    @Test
    public void testShouldGetMinItemFromBST(){

        binarySearchTreeSUT.insert(4);
        binarySearchTreeSUT.insert(8);
        binarySearchTreeSUT.insert(2);
        binarySearchTreeSUT.insert(3);

        assert(binarySearchTreeSUT.getMin()  == 2);
    }
    @Test
    public void testShouldTraverseItemsFromBST(){

        binarySearchTreeSUT.insert(4);
        binarySearchTreeSUT.insert(8);
        binarySearchTreeSUT.insert(2);
        binarySearchTreeSUT.insert(3);

        binarySearchTreeSUT.traversal();
    }

    @Test
    public void testShouldDeleteLeafNOdeItemsFromBST(){

        binarySearchTreeSUT.insert(4);
        binarySearchTreeSUT.insert(8);
        binarySearchTreeSUT.insert(2);
        binarySearchTreeSUT.insert(3);

        binarySearchTreeSUT.traversal();

        binarySearchTreeSUT.delete(3);

        binarySearchTreeSUT.traversal();
    }

    @Test
    public void testShouldDeleteParentNodeWithBothChildrenItemsFromBST(){

        binarySearchTreeSUT.insert(4);
        binarySearchTreeSUT.insert(8);
        binarySearchTreeSUT.insert(2);
        binarySearchTreeSUT.insert(3);

        binarySearchTreeSUT.traversal();

        binarySearchTreeSUT.delete(4);

        binarySearchTreeSUT.traversal();
    }

    @Test
    public void testShouldDeleteParentNodeWithBothChildrenItemsFromBSTWithSuccessor(){

        binarySearchTreeSUT.insert(4);
        binarySearchTreeSUT.insert(8);
        binarySearchTreeSUT.insert(2);
        binarySearchTreeSUT.insert(7);

        binarySearchTreeSUT.traversal();

        binarySearchTreeSUT.delete(4);

        binarySearchTreeSUT.traversal();
    }

    @Test
    public void testShouldDeleteParentNodeWithOneChildrenItemsFromBST(){

        binarySearchTreeSUT.insert(4);
        binarySearchTreeSUT.insert(8);
        binarySearchTreeSUT.insert(2);
        binarySearchTreeSUT.insert(3);

        binarySearchTreeSUT.traversal();

        binarySearchTreeSUT.delete(2);

        binarySearchTreeSUT.traversal();
    }

}
\$\endgroup\$
5
\$\begingroup\$

Your tests seem to be of appropriate size and scope but there are a couple things I'd address.

Assertions First and foremost I don't see your code asserting on anything.
You manipulate the binarySearch tree, which is the first step, but then you don't check the state of it afterward. As a reviewer I'd ask "what are your tests actually testing?"

Magic numbers It's clear you thought about what each test was supposed to accomplish but the intent of the test could be clearer to the reader. You could try adding meaning to the numbers you are inserting to the tree, adding comments, or some other way of making the purpose of each test very clear to the reader.

\$\endgroup\$
2
\$\begingroup\$

When testing functions that accept primitive number types you must always test the limits of the value space. Add tests for Integer.MAX_VALUE and Integer.MIN_VALUE in combination with each other and with smaller numbers. The point of these tests is to verify that any possible arithmetic operation there may be does not suffer from over- and underflow.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.