theTuxRacer
theTuxRacer

Reputation: 13869

Access a List in JUnit Test Case

I have this ParkingLot.java

 public class ParkingLot {

private final int size;
private Car[] slots = null;

List<String> list = new ArrayList<String>();

public ParkingLot(int size) {
    this.size = size;
    this.slots = new Car[size];
}

public List licenseWithAParticularColour(String colour) {
    for (int i = 0; i < slots.length; i++) {
        if (slots[i].getColour() == colour) {
            System.out.println(slots[i].getLicense());
            list.add(slots[i].getLicense());
            return list;
        }
    }
    return null;
}

}

I have created a ParkingLotTest.java as follows

public class ParkingLotTest {

private Car car1;
private Car car2;
private Car car3;

private Ticket ticket1;
private Ticket ticket2;
private Ticket ticket3;

private ParkingLot parkingLot;

private List<String> list = new ArrayList<String>();

@Before
public void intializeTestEnvironment() throws Exception {
    this.car1 = new Car("1234", "White");
    this.car2 = new Car("4567", "Black");
    this.car3 = new Car("0000", "Red");

    this.parkingLot = new ParkingLot(2);

    this.ticket1 = parkingLot.park(car1);
    this.ticket2 = parkingLot.park(car2);
    this.ticket3 = parkingLot.park(car3);
    this.list = parkingLot.list;


}

@Test
public void shouldGetLicensesWithAParticularColour() throws Exception {
    assertEquals(, parkingLot.licenseWithAParticularColour("White"));

}

}

In the above Test Case, I want to check that the List is filled with the correct Licenses. 1. How do i create a field in the ParkingLotTest.java so that the List in the first class is same as list in the second class file.

Upvotes: 4

Views: 14972

Answers (3)

theTuxRacer
theTuxRacer

Reputation: 13869

Pascal's Answer worked for me.

@Pascal Again, I made this function:

public List getSlotNumbersWithAParticularColour(String colour) {
        List<Integer> listOfTicketsWithAColour = new ArrayList<Integer>();
        for (int i = 0; i < slots.length;) {
            if (slots[i].getColour() == colour) {
                listOfTicketsWithAColour.add(i);

            }
            return listOfTicketsWithAColour;

        }
        return null;
    }

The fault is not in the for loop, adding an i++ is "dead-code" acc to Eclipse. Adding the i++ doesnt cause any difference.

And the corresponding test-case:

public void getSlotNumbersWithAGivenColour() throws Exception {
        List<String> expected = new ArrayList<String>();
        expected.add("0");
        expected.add("3");

        assertEquals(expected, parkingLot.getSlotNumbersWithAParticularColour("White"));
    }

The test fails. The function only returns 0, instead of 0,3. Any idea why?

Upvotes: 1

Carl
Carl

Reputation: 7554

However you want?

That's somewhat in jest, but however you normally build a List will do just fine - as long as it's consistent with what you want your tested interface list to be.

For this particular case, I'd recommend building a List<Car> as your test reference, then visiting each Car and parking it. You can then build the licenses list from that reference list, and compare it to the parking lot one. Just make sure your iteration direction is correct.

BTW, from what I see, I don't think things work the way they're supposed to work - good thing you're testing it.

Upvotes: 2

Pascal Thivent
Pascal Thivent

Reputation: 570545

First, I don't think you need a list on ParkingLot so your question actually doesn't make much sense :)

Second, just set up the expected result in each test method:

public class ParkingLotTest {

    //...

    @Test
    public void shouldGetLicensesWithAParticularColour() throws Exception {
        List<Car> expected = new ArrayList<Car>();
        expected.add(...);

        assertEquals(expected, parkingLot.licenseWithAParticularColour("White"));
    }

}

And don't forget to also test unexpected values or special cases. For example:

@Test
public void shouldNotGetLicensesWithANullColour() throws Exception {
    ...
    assertEquals(expected, parkingLot.licenseWithAParticularColour(null));
}

@Test
public void shouldNotGetLicensesWithAnUnknownColour() throws Exception {
    ...
    assertEquals(expected, parkingLot.licenseWithAParticularColour("unknown"));
}

Some additional remarks:

  • I wouldn't use a Car[] for the slots but a List<Car>.
  • You don't really need the List<String> list in ParkingLot (and the current implementation of licenseWithAParticularColour is buggy).
  • I would use an Enum for the color.

Upvotes: 5

Related Questions