Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Martyn

Thread: JUnit Testing enQueue & deQueue Methods

  1. #1
    Martyn is offline Member
    Join Date
    Nov 2011
    Location
    Belfast
    Posts
    37
    Rep Power
    0

    Default JUnit Testing enQueue & deQueue Methods

    Hello,

    Can enQueue & deQueue methods be JUnit tested?

    This is my enQueue

    Java Code:
        public void enQueue(String aircraftNum, String airline, String departCity, int passengers) {
    
            PlaneNode newPlaneNode = new PlaneNode(aircraftNum, airline, departCity, passengers, null, null);//creates a new planeNode object i.e. the first or next object
            if (planeNode != null) {// check if queue is not empty
    
                PlaneNode lastPlaneNode = getLastNode();// get last node in the queue and assign to another PlaneNode object
                insert(lastPlaneNode, newPlaneNode);//call insert method and pass lastPlaneNode object and newPlaneNode object
    
            } else {
                planeNode = newPlaneNode;// assign new PlaneNode object as first node of the queue
            }
        }
    This is my deQueue

    Java Code:
    public void deQueue(final int queueNo) {
    
            if (planeNode != null && this.getSize() > 1) {//check if queue size is not equal to null and queue size must be greater than 1
                int index = 0;
                PlaneNode currentNode = planeNode;// assign first node as current node
    
                if (index + 1 == queueNo) {// compare the queue position of the current node is equal to the param queue position
                    planeNode = planeNode.getNext();// if it is get the 2nd node and assign as first node
                    planeNode.setPrevious(null);//set previous node of the first node to null
                } else {//not in the first node
    
                    while (currentNode.getNext() != null) {//While the next node is not empty
                        index++;//increment node count x 1
                        currentNode = currentNode.getNext();// assign the 2nd node as current node
    
    
                        if (index + 1 == queueNo) {// compare the queue position of the current node and see if it is equal to the current param queue position
                            PlaneNode previousNode = currentNode.getPrevious();// if it is get the previous node of the current node
                            PlaneNode nextNode = currentNode.getNext();// get the next node before the current node and make it the current node
    
                            previousNode.setNext(nextNode);// assign nextNode of the current node to the nextNode of previous node of the current node
                            if (nextNode != null) {
                                nextNode.setPrevious(previousNode);//assign previous node of the current node to previousNode of the nextNode of the current node
                            }
                        }
                    }
                }
            } else {
                planeNode = null;// assign null to queue if node is equal to null or queue size is 1
            }
        }
    And this is the automatically generated NetBeans Code

    Java Code:
    }
    
        /**
         * Test of enQueue method, of class IFMQueue.
         */
        @Test
        public void testEnQueue() {
            System.out.println("enQueue");
            String aircraftNum = "";
            String airline = "";
            String departCity = "";
            int passengers = 0;
            IFMQueue instance = new IFMQueue();
            instance.enQueue(aircraftNum, airline, departCity, passengers);
            // TODO review the generated test code and remove the default call to fail.
            fail("The test case is a prototype.");
        }
    Any help would be greatly appreciated on this

    Thanks
    Martyn

  2. #2
    Martyn is offline Member
    Join Date
    Nov 2011
    Location
    Belfast
    Posts
    37
    Rep Power
    0

    Smile Re: JUnit Testing enQueue & deQueue Methods

    The pennies have dropped , please note I am not saying this is the complete 100% correct or best way to go about this it is just the way I went about it. I found that in order to test if an object/s has been added to the queue in the JUnit testing I had to create objects (x3 in my case) in the JUnit test class and then call the enQueue method from the IFMQueue class to send them to the current queue, this in turn showed that the enQueue method of the IFMQueue class was working.

    Java Code:
    IFMQueue q = new IFMQueue();
        PlaneNode BA100 = new PlaneNode("BA100", "British Airways", "London", 100);
        PlaneNode BA200 = new PlaneNode("FB200", "FlyBe", "Belfast", 150);
        PlaneNode BA300 = new PlaneNode("EZ300", "Easy Jet", "Belfast", 200);
    
        public IFMQueueTest() {
        }
    Then I had to populate each test with these objects

    Java Code:
    }
    
        /**
         * Test of enQueue method, of class IFMQueue.
         */
        @Test
        public void testEnQueue() {
            System.out.println();
            System.out.println("________________________________________________________________________________________");
            System.out.println("Testing the enQueue method within the IFMQueue class ");
            System.out.println("Current Queue Size " + q.getSize());
            System.out.println("Now adding the following 3 plane objects to the queue\n ");
            System.out.println();
    
            q.enQueue(BA100);
            System.out.println(BA100);
            q.enQueue(BA200);
            System.out.println(BA200);
            q.enQueue(BA300);
            System.out.println(BA300);
    
            System.out.println();
            System.out.println("The size of the queue is currently " + q.getSize() + " - Now calling view all method\n");
            q.viewAll();
    
            int expected = 3;
            int equals = q.getSize();
    
            System.out.println();
            System.out.println("Queue size = " + q.getSize());
            System.out.println();
            assertEquals(expected, equals);
            System.out.println();
        }
    enQueue Results

    __________________________________________________ ______________________________________
    Testing the enQueue method within the IFMQueue class
    Current Queue Size 0
    Now adding the following 3 plane objects to the queue


    BA100 British Airways London 100
    FB200 FlyBe Belfast 150
    EZ300 Easy Jet Belfast 200

    The size of the queue is currently 3 - Now calling view all method

    ================================================== ==============================
    Queue# Aircraft# Airline Company Departure Passengers
    ================================================== ==============================
    1 BA100 British Airways London 100
    2 FB200 FlyBe Belfast 150
    3 EZ300 Easy Jet Belfast 200
    ================================================== ==============================

    Queue size = 3

    For deQueue

    Java Code:
    }
        /**
         * Test of deQueue method, of class IFMQueue. As this method does not return
         * anything I will populate a queue with three planes using the enQueue
         * method and display these planes with the viewAll method. I will then
         * deQueue one of the planes and initiate the viewAll method to show that
         * the plane object has been removed.
         *
         * Assert equals will be satisfied using the getSize (queue size/length)
         * method from before and after
         */
        @Test
        public void testDeQueue() {
            System.out.println();
            System.out.println("Testing the deQueue method within the IFMQueue class ");
            System.out.println("Current Queue Size " + q.getSize());
            System.out.println("Now adding the following 3 plane objects to the queue\n ");
            System.out.println();
    
            q.enQueue(BA100);
            System.out.println(BA100);
            q.enQueue(BA200);
            System.out.println(BA200);
            q.enQueue(BA300);
            System.out.println(BA300);
    
            System.out.println();
            System.out.println("The size of the queue is currently " + q.getSize() + " - Now calling viewAll method\n");
            q.viewAll();
    
            System.out.println("deQueue method will now remove a Plane object/node 1 from the queue and re-shuffle objects ");
            q.deQueue(1);
            System.out.println("Calling viewAll method\n ");
            q.viewAll();
            int expected = 2;
            int equals = q.getSize();
            System.out.println("Queue size is now " + q.getSize());
            assertEquals(expected, equals);
            System.out.println("________________________________________________________________________________________");
            System.out.println();
        }
    deQueue Results ---

    Testing the deQueue method within the IFMQueue class
    Current Queue Size 0
    Now adding the following 3 plane objects to the queue


    BA100 British Airways London 100
    FB200 FlyBe Belfast 150
    EZ300 Easy Jet Belfast 200

    The size of the queue is currently 3 - Now calling viewAll method

    ================================================== ==============================
    Queue# Aircraft# Airline Company Departure Passengers
    ================================================== ==============================
    1 BA100 British Airways London 100
    2 FB200 FlyBe Belfast 150
    3 EZ300 Easy Jet Belfast 200
    ================================================== ==============================
    deQueue method will now remove a Plane object/node 1 from the queue and reshuffle objects
    Calling viewAll method

    ================================================== ==============================
    Queue# Aircraft# Airline Company Departure Passengers
    ================================================== ==============================
    1 FB200 FlyBe Belfast 150
    2 EZ300 Easy Jet Belfast 200
    ================================================== ==============================
    Queue size is now 2

    And thats that, hope it helps someone
    Last edited by Martyn; 01-10-2013 at 12:31 AM.
    DarrylBurke likes this.

Similar Threads

  1. Junit Testing within local classes
    By danboy4 in forum New To Java
    Replies: 3
    Last Post: 10-08-2012, 01:44 PM
  2. Junit testing, testing list<e> interface
    By mackavelirip in forum New To Java
    Replies: 0
    Last Post: 10-05-2011, 06:08 AM
  3. JUnit testing
    By hedonist in forum Advanced Java
    Replies: 4
    Last Post: 10-04-2009, 12:06 PM
  4. Replies: 1
    Last Post: 04-17-2008, 02:30 PM
  5. Testing JUnit PDE with Ant
    By keynan in forum Eclipse
    Replies: 0
    Last Post: 02-14-2008, 11:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •