vendredi 26 février 2016

J-Unit testing java eclipse

I am in Eclipse Java Mars and I am having a few difficulties with J-unit testing. I am testing for my QueueADT class that I implemented with a Linked List. I will attach all 3 codes.

The problem is that I keep getting a NullPointer Exception for all of the methods in the J-Unit test.

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

The QueueADT code:

/**
* 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:

/**
 * 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;
    }
  } 
}

J-Unit test code:

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();
        }


}

Aucun commentaire:

Enregistrer un commentaire