Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How implement Tests for Depth First Traversal using lists?

Tags:

java

I am trying to test my DepthFirstTraversal. Just, how would I test it based on the code that I have written it for.

This is the methods I am using,(The add method is from another class which it is extending from, and traverse method is being implemented from another class):

public class DepthFirstTraversal  {
        private List traversal = new ArrayList();

This is how I am trying to test it using a List since that is what I declared in the depth first traversal class:

@Test
        void willItDepthFirst(){
                DepthFirstTraversal b = new DepthFirstTraversal();
                b.add(1);//add node
                b.add(0);
                b.add(2);
                b.add(3);
                b.add(4);
                b.add(0,1);//connect nodes with edges
                b.add(1,2);
                b.add(2,3);
                b.add(0,4);

                b.traverse();
                List<Integer> c= Arrays.asList(0,1,4,2,3);

                assertEquals(c,b);
        }

However this is the error I am getting. :

org.opentest4j.AssertionFailedError: 
Expected :[0, 1, 4, 2, 3]
Actual   :graph.DepthFirstTraversal@6536e911

Edit: Removed unnecessary methods

like image 258
foobar Avatar asked Mar 25 '26 01:03

foobar


1 Answers

As mentioned in the comment, you need to check the return value of the traverse() method, not the object itself:

List<Integer> result = b.traverse();
List<Integer> expected = Arrays.asList(0,1,4,2,3);

assertEquals(expected, result);
like image 142
vatbub Avatar answered Mar 27 '26 14:03

vatbub



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!