简体   繁体   中英

Junit Testing- Only one instance is recognised in test run

I am testing a method for my queue and it is only recognising one instance. So when I run the test it says the result should be "Number of passengers is: 3.0", instead of "Number of passengers is 13.0" Any help would be great, thanks.

This is my method for totalPassengers()

public String totalPassengers() {
        double total = 0;
        if (isEmpty()) {
            return ("There are no vehicles in the queue");
        } else {
            VehicleNode temp = start;
            while (temp != null) {
                total = total += temp.getPassengers();
                temp = temp.getPrevious();
            }//while
        }//else
        return ("Number of passengers is: " + total);

and this is the JUnit Test

@Test
public void testTotalPassengers() {
    System.out.println("totalPassengers");
    CVMQueue instance = new CVMQueue();
    VehicleNode newnode1 = new VehicleNode("OXB 123", "Car", "British", 3, 1.2);
    VehicleNode newnode2 = new VehicleNode("BR 22", "Mini-Bus", "Italian", 10, 2.2);
    instance.enqueue(newnode1);
    instance.enqueue(newnode2);
    String expResult = "Number of passengers is: 13.0";
    String result = instance.totalPassengers();
    assertEquals(expResult, result);
}

I am testing a method for my queue and it is only recognising one instance. So when I run the test it says the result should be "Number of passengers is: 3.0", instead of "Number of passengers is 13.0" Any help would be great, thanks.

This is my method for totalPassengers()

public String totalPassengers() {
        double total = 0;
        if (isEmpty()) {
            return ("There are no vehicles in the queue");
        } else {
            VehicleNode temp = start;
            while (temp != null) {
                total = total += temp.getPassengers();
                temp = temp.getPrevious();
            }//while
        }//else
        return ("Number of passengers is: " + total);

and this is the JUnit Test

@Test
public void testTotalPassengers() {
    System.out.println("totalPassengers");
    CVMQueue instance = new CVMQueue();
    VehicleNode newnode1 = new VehicleNode("OXB 123", "Car", "British", 3, 1.2);
    VehicleNode newnode2 = new VehicleNode("BR 22", "Mini-Bus", "Italian", 10, 2.2);
    instance.enqueue(newnode1);
    instance.enqueue(newnode2);
    String expResult = "Number of passengers is: 13.0";
    String result = instance.totalPassengers();
    assertEquals(expResult, result);
}

Seems there is a bug here and now fixed;

 public String totalPassengers() { double total = 0; if (isEmpty()) { return ("There are no vehicles in the queue"); } else { VehicleNode temp = start; while (temp != null) { total += temp.getPassengers(); temp = temp.getPrevious(); }//while }//else return ("Number of passengers is: " + total);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM