3 Replies - 233 Views - Last Post: 01 February 2019 - 01:41 AM Rate Topic: -----

#1 ivankorj   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-January 19

Assert instead of if and for

Posted 28 January 2019 - 06:58 AM

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.getType() == 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();

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



But in the test can not use the lambda. How to do it without lambda?

This post has been edited by macosxnerd101: 28 January 2019 - 07:13 AM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Assert instead of if and for

#2 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3617
  • View blog
  • Posts: 16,595
  • Joined: 20-September 08

Re: Assert instead of if and for

Posted 28 January 2019 - 08:52 AM

Use a loop and call getMilitaryType in it?
Was This Post Helpful? 0
  • +
  • -

#3 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,832
  • Joined: 13-June 14

Re: Assert instead of if and for

Posted 31 January 2019 - 01:08 PM

Why do you need a loop at all? Why aren't you checking that the List you get back contains the elements you expect?
Was This Post Helpful? 1
  • +
  • -

#4 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6739
  • View blog
  • Posts: 27,750
  • Joined: 12-December 12

Re: Assert instead of if and for

Posted 01 February 2019 - 01:41 AM

If you could write down

  • A description of the methods' purpose and output
  • An example of correct behaviour
  • A counter example of incorrect behaviour

You will be nearer to being able to write meaningful test(s).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1