[英]JUnit testing java eclipse
I am in Eclipse Java Mars and I am having a few difficulties with JUnit testing. 我在Eclipse Java Mars中工作,在进行JUnit测试时遇到了一些困难。 I am testing for my QueueADT class that I implemented with a Linked List.
我正在测试我用链接列表实现的QueueADT类。 I will attach all 3 codes.
我将附上所有3个代码。
The problem is that I keep getting a NullPointer Exception for all of the methods in the JUnit test. 问题是我一直在为JUnit测试中的所有方法获取NullPointer异常。
The lines that give the errors in Queue Testing are: All are lines with "q.enqueue" Line 29 Line 56 Line 67 Line 45 Line 12 Line 73 Line 36 Line 20 在队列测试中给出错误的行是:所有行都带有“ q.enqueue”行29行56行67行45行12行73行36行20行
The QueueADT code: 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 code: 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 test code: 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();
}
}
You are getting NullPointerException
because your protected QueueADT q;
您正在获取
NullPointerException
因为protected QueueADT q;
object is never initialized in QueueT
class. 永远不会在
QueueT
类中初始化对象。
Create a set up method 创建设置方法
@Before
public void setUp() {
q = new LinkedQueue();
}
which will initialize you q object before each test, or 它将在每次测试之前初始化q对象,或者
@BeforeClass
public static void setUp() {
q = new LinkedQueue();
}
if you want only one shared object between all tests. 如果您只想在所有测试之间共享一个对象。
On a side note: I don't really see any gains from your custom collection, as a LinkedList seems to fit your goals. 附带说明一下:我确实看不到您的自定义集合有什么好处,因为LinkedList似乎符合您的目标。
您缺少setup()和takedown()方法!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.