需要创建单元测试请帮忙 当我创建单元测试时,它会显示错误"测试中不允许参数"
package MergeDoublyLL;
import java.util.ArrayList;
import java.util.Iterator;
public class doublell {
static Node head;
static class Node {
int data;
Node next, prev;
Node(int d) {
data = d;
next = prev = null;
}
}
Node split(Node head) {
Node fast = head, slow = head;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
Node temp = slow.next;
slow.next = null;
return temp;
}
Node mergeSort(Node node) {
if (node == null || node.next == null) {
return node;
}
Node second = split(node);
node = mergeSort(node);
second = mergeSort(second);
return merge(node, second);
}
Node merge(Node first, Node second) {
if (first == null) {
return second;
}
if (second == null) {
return first;
}
if (first.data < second.data) {
first.next = merge(first.next, second);
first.next.prev = first;
first.prev = null;
return first;
} else {
second.next = merge(first, second.next);
second.next.prev = second;
second.prev = null;
return second;
}
}
int[] print(Node node) {
int a;
ArrayList<Integer> al = new ArrayList<Integer>();
Node temp = node;
System.out.println("Forward Traversal using next pointer");
while (temp != null) {
a=(temp.data);
al.add(a);
temp = temp.next;
}
int[] ret = new int[al.size()];
Iterator<Integer> iterator = al.iterator();
for (int i = 0; i < ret.length; i++)
{
ret[i] = iterator.next().intValue();
}
return ret;
}
public static void main(String[] args) {
doublell list = new doublell();
doublell.head = new Node(10);
doublell.head.next = new Node(30);
doublell.head.next.next = new Node(3);
doublell.head.next.next.next = new Node(4);
doublell.head.next.next.next.next = new Node(20);
doublell.head.next.next.next.next.next = new Node(5);
Node node;
node=head;
node = list.mergeSort(head);
System.out.println("Linked list after sorting :");
int[] result =list.print(node);
for (int j=0; j<result.length;j++)
System.out.print(result[j]);
}
}
单元测试:
// Unit Test
package MergeDoublyLL;
import static org.junit.Assert.assertArrayEquals;
import org.junit.Test;
import MergeDoublyLL.doublell.Node;
public class testdoublell {
@Test
public void test(Node head) {
doublell list = new doublell();
doublell.head = new Node(10);
doublell.head.next = new Node(30);
doublell.head.next.next = new Node(3);
doublell.head.next.next.next = new Node(4);
doublell.head.next.next.next.next = new Node(20);
doublell.head.next.next.next.next.next = new Node(5);
Node node=head;
node = list.mergeSort(head);
int result[] =list.print(node);
int expectedArray[] = {3,4,5,10,20,30};
assertArrayEquals(expectedArray, result);
}
}
答案 0 :(得分:1)
单元测试应该独立于其他所有单元测试单个代码单元。你正在为你的考试传递一个论点:
@Test
public void test(Node head) {
相反,只需在测试中创建头节点,并且没有参数:
@Test
public void test() {