2016-02-26 175 views
0

我在Eclipse Java Mars中,我在JUnit测试中遇到了一些困难。我正在测试我使用链接列表实现的QueueADT类。我将附上所有3个代码。JUnit测试java eclipse

问题是我一直在为JUnit测试中的所有方法获取NullPointer异常。

,给在队列测试中的错误的行是: 所有与 “q.enqueue” 第29行 线56 线67 线45 第12行 线73 线36 线线20

的QueueADT代码:

/** 
* QueueADT defines the interface to a queue collection. 
*/ 
public interface QueueADT<String> 
{ 
/** 
* Adds one element to the rear of this queue. 
* @param element the element to be added to the rear of the queue 
*/ 
public void enqueue(String element); 

/** 
* Removes and returns the element at the front of this queue. 
* @return the element at the front of the queue 
*/ 
public String dequeue() throws CollectionUnderflowException; 

/** 
* Returns without removing the element at the front of this queue. 
* @return the first element in the queue 
*/ 
public String first() throws CollectionUnderflowException; 

/** 
* Returns true if this queue contains no elements. 
* @return true if this queue is empty 
*/ 
public boolean isEmpty(); 

/** 
* Returns the number of elements in this queue. 
* @return the integer representation of the size of the queue 
*/ 
public int size(); 

/** 
* Returns a string representation of this queue. 
* @return the string representation of the queue 
*/ 
public String toString(); 
} 

LinkedListQueue代码:

/** 
* LinkedQueue represents a singly linked list implementation of a Queue. 

*/ 
import java.util.*; 

public class LinkedQueue<String> implements QueueADT<String> 
{ 
private int count;    // the number of items in the queue 
private Node<String> head, tail; // references to the first and last nodes in the queue 

/** 
* Creates an empty queue. 
*/ 
public LinkedQueue() 
{ 
    count = 0; 
    head = tail = null; 
} 

/** 
* Adds the specified element to the tail of this queue. 
* @param element the element to be added to the tail of the queue 
*/ 
public void enqueue(String element) 
{ 
    Node<String> node = new Node<String>(element); 

    if (isEmpty()) 
     head = node; 
    else 
     tail.next = node; 

    tail = node; 
    count++; 
} 

/** 
* Removes the element at the head of this queue and returns a 
* reference to it. 
* @return the element at the head of this queue 
* @throws CollectionUnderflowException if the queue is empty 
*/ 
public String dequeue() throws CollectionUnderflowException 
{ 
    if (isEmpty()) 
     throw new CollectionUnderflowException("Queue is empty."); 

    String result = head.element; 
    head = head.next; 
    count--; 

    if (isEmpty()) 
     tail = null; 

    return result; 
} 

/** 
* Returns a reference to the element at the head of this queue. 
* The element is not removed from the queue. 
* @return a reference to the first element in this queue 
* @throws CollectionUnderflowException if the queue is empty 
*/ 
public String first() throws CollectionUnderflowException 
{ 
    if (isEmpty()) 
    throw new UnsupportedOperationException("Not yet implemented."); 

    return head.getElement(); 
} 

/** 
* Returns true if this queue is empty and false otherwise. 
* @return true if this queue is empty 
*/ 
public boolean isEmpty() 
{ 
    return(count == 0); 
    //throw new UnsupportedOperationException("Not yet implemented."); 
} 

/** 
* Returns the number of elements currently in this queue. 
* @return the number of elements in the queue 
*/ 
public int size() 
{ 
    return count; 
    //throw new UnsupportedOperationException("Not yet implemented."); 

} 

/** 
* Returns a string representation of this queue. 
* @return the string representation of the queue 
*/ 
public String toString() 
{ 
    String finish = " "; 
    Node<String> recent = head; 

    while (recent != null){ 
     finish = finish + (recent.getElement()).tostring() + "\n"; 
     recent = recent.getNext(); 
    } 
    //throw new UnsupportedOperationException("Not yet implemented."); 
} 


    private static class Node<E> { 
E element; 
Node<E> next; 

public Node(E element) { 
    this.element = element; 
    } 
    } 
} 

JUnit测试代码:

import junit.framework.TestCase; 

public class QueueT extends TestCase { 

     /** 
     * The queue to use in all the tests: set this in subclasses. 
     */ 
     protected QueueADT q; 

     @Test 
     public void testNewQueueIsEmpty() { 
      assertTrue(q.isEmpty()); 
      assertEquals(q.size(), 0); 
     } 

     // @Test 
     public void testInsertsToEmptyQueue() { 
      int numberOfInserts = 6; 
      for (int i = 0; i < numberOfInserts; i++) { 
       q.enqueue("zzz"); 
      } 
      assertTrue(!q.isEmpty()); 
      assertEquals(q.size(), numberOfInserts); 
     } 

     //@Test 
     public void testEnqueueThenDequeue() { 
      String message = "hello"; 
      q.enqueue(message); 
      assertEquals(q.dequeue(), message); 
     } 

     // @Test 
     public void testEnqueueThenPeek() { 
      String message = "hello"; 
      q.enqueue(message); 
      int size = q.size(); 
      assertEquals(q.first(), message); 
      assertEquals(q.size(), size); 
     } 

     // @Test 
     public void testFiftyInThenFiftyOut() { 
      for (int i = 0; i < 50; i++) { 
       q.enqueue(i); 
      } 
      for (int i = 0; i < 50; i++) { 
       assertEquals(((Integer)q.dequeue()).intValue(), i); 
      } 
     } 

     // @Test 
     public void testRemovingDownToEmpty() { 
      int numberOfRemoves = (int)(Math.random() * 20 + 1); 
      for (int i = 0; i < numberOfRemoves; i++) { 
       q.enqueue("zzz"); 
      } 
      for (int i = 0; i < numberOfRemoves; i++) { 
       q.dequeue(); 
      } 
      assertTrue(q.isEmpty()); 
      assertEquals(q.size(), 0); 
     } 

     // @Test(expected=NoSuchElementException.class) 
     public void testRemoveOnEmptyQueue() { 
      assertTrue(q.isEmpty()); 
      q.dequeue(); 
     } 

     //@Test(expected=NoSuchElementException.class) 
     public void testPeekIntoEmptyQueue() { 
      assertTrue(q.isEmpty()); 
      q.dequeue(); 
     } 


} 
+0

什么行会抛出异常? – TangledUpInBlue

+0

哦对不起。我将编辑帖子,并把行! – isabella

+0

好的。完成,对此感到遗憾。 – isabella

回答

1

创建设置方法

@Before 
public void setUp() { 
    q = new LinkedQueue(); 
} 

这将在每次测试之前初始化你q对象,或

@BeforeClass 
public static void setUp() { 
    q = new LinkedQueue(); 
} 

,如果你想所有的测试之间只有一个共享对象。

附注:我没有看到自定义集合的任何收益,因为LinkedList似乎适合您的目标。

0

你错过了设置()和移除()方法!

1

您正在获取NullPointerException,因为您的protected QueueADT q;对象从未在QueueT类中初始化。