此测试验证了运输机从军机列表中接收的方法的正确性。如在测试中一样,请勿使用“ for”和“ if”,而应使用assertTrue。也不能使用FLAG和lambda。在所有这些旁边应该是一个断言。
@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);
}
我这样做了:
@Test
public void testGetTransportMilitaryPlanes() {
Airport airport = new Airport(planes);
List<MilitaryPlane> transportMilitaryPlanes = airport.getTransportMilitaryPlanes();
MilitaryPlane militaryPlane = (MilitaryPlane) transportMilitaryPlanes;
Assert.assertTrue(militaryPlane.getMilitaryType() == MilitaryType.TRANSPORT);
}
但是测试失败了。并且在原始版本中是真的。
答案 0 :(得分:4)
使用流将使它更加优雅:
Assert.assertTrue
(transportMilitaryPlanes.stream()
.anyMatch(p -> p.getMilitaryType() == MilitaryType.TRANSPORT));