0
votes

This test verifies the correctness of the method that transport planes receive from the list of military planes. As in the test not to use "for" and "if", and to use an assertTrue. Also can not use FLAG and lambda. Next to all this should be one Assert.

@Test
public void testGetTransportMilitaryPlanes() {
    Airport airport = new Airport(planes);
    List<MilitaryPlane> transportMilitaryPlanes = airport.getTransportMilitaryPlanes();

    boolean flag = false;
    for (MilitaryPlane militaryPlane : transportMilitaryPlanes) {
        if ((militaryPlane.getMilitaryType() == MilitaryType.TRANSPORT)) {
            flag = true;
            break;
        }
    }
    Assert.assertEquals(flag, true);
}

I did so:

@Test
public void testGetTransportMilitaryPlanes() {
    Airport airport = new Airport(planes);
    List<MilitaryPlane> transportMilitaryPlanes = airport.getTransportMilitaryPlanes();

    MilitaryPlane militaryPlane = (MilitaryPlane) transportMilitaryPlanes;
    Assert.assertTrue(militaryPlane.getMilitaryType() == MilitaryType.TRANSPORT);

}

But the test fails like that. And in the original version was true.

1
Have you tried doing this using Stream API?iamrajshah
transportMilitaryPlanes include objects of MilitaryPlane and you are trying to cast this list to a single object. It is not true.Y.Kakdas
are you sure you don't need to test if ALL returned planes are of type TRANSPORT? The actual test will pass if just any one of the returned planes is of type TRANSPORT.user85421

1 Answers

4
votes

Using streams would make this much more elegant:

Assert.assertTrue
    (transportMilitaryPlanes.stream()
                            .anyMatch(p -> p.getMilitaryType() == MilitaryType.TRANSPORT));