Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a List in JUnit Test Case

Tags:

java

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.

like image 950
theTuxRacer Avatar asked Feb 27 '23 18:02

theTuxRacer


1 Answers

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.
like image 144
Pascal Thivent Avatar answered Mar 01 '23 08:03

Pascal Thivent