java - 链表测试....返回类型问题

标签 java class junit linked-list singly-linked-list

我有一个 IntLinkedList 类的代码

public class IntLinkedList {

    private Node head;

    public void addFirst(int data) {
        head = new Node(data, head);
    }

    public Node copy(){
       Node current = head; // used to iterate over original list
       Node newList = null; // head of the new list
       Node tail = null;    // point to last node in new list

       while (current != null)
       {
        // special case for the first new node
          if (newList == null)
          {
              newList = new Node(current.data, null);
              tail = newList;
          }
          else
          {
              tail.next = new Node(current.data, null);
              tail = tail.next;
          }
          current = current.next;
       }
       return newList;
    }


    private class Node  {
            int data;
            Node next;

        Node(int data, Node next) {
            this.data = data;
            this.next = next;
        }
    }
}

我正在尝试使用以下 JUnit 代码测试复制方法

public class IntLinkedListTest {

    /** Reference to linked list under test */
    private IntLinkedList lst;

    /** Creates a linked list for testing. */
    @Before
    public void setUp() {
        lst = new IntLinkedList();

        lst.addFirst(30);
        lst.addFirst(10);
        lst.addFirst(40);
        lst.addFirst(20);
    }

    /** Tests copying a non-empty list. */
    @Test
    public void testCopy() {
        IntLinkedList cpy = lst.copy();
        assertEquals(lst.toString(), cpy.toString());
    }
}

我想要从 Copy() 方法的 IntLinkedList 类返回一个列表,并在 JUnit 中进行测试。我也尝试返回类型 IntLinkedList 和 Object,但我不断收到诸如“类型不匹配:无法从 IntLinkedList.Node 转换为 IntLinkedList”之类的错误。我对 LinkedList 的经验很少,但我对 java 类、对象引用有经验,但这对我来说是新领域。有人可以帮忙吗?

最佳答案

解决方案:- 您正在将 Node 类 toString 与 IntLinkedList 类 toString 进行比较,因此 Junit 失败,尝试覆盖 toString( ) Node 和 IntLinkedList 类的方法,您将清楚地看到堆栈跟踪为

org.junit.ComparisonFailure: expected:<[IntLinkedList [head=Node [data=20, next=Node [data=40, next=Node [data=10, next=Node [data=30, next=null]]]]]]> but was:<[Node [data=20, next=Node [data=40, next=Node [data=10, next=Node [data=30, next=null]]]]]>

这个 Junit 正在按预期工作

 @Test
    public void testCopy() {
        IntLinkedList.Node cpy = lst.copy();
        assertEquals(lst.copy().toString(), cpy.toString());
    }

编辑:- 由于您的 Node 类是私有(private)的,我做了一个小小的更改以使 IntLinkedList.Node 工作,因此我将签名更改为 static 以使 junit 工作,即

static class Node  {

关于java - 链表测试....返回类型问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58743250/

相关文章:

javascript - jQuery : change <div> Background on . 选择输入的change()

swift - 为什么我的类变量不会更改为文本框的文本?

junit - 如何查看 Jenkins 构建测试结果的历史记录?

java - GData 错误 : Intermittent "Invalid root element"

java - 使用Map reduce查找最低编号

java - jxta:jxse是否提供nat tcp穿越解决方案?

java spring单元测试失败,无法加载ApplicationContext

java - GWT DataGrid 标题,可过滤和排序

c++ - 错误 "C++ requires a type specifier for all declarations whilst defining methods"

java - 使用静态方法处理最终类