Home > Software engineering >  How to assert a List in Java
How to assert a List in Java

Time:01-05

I have two methods in a separate class in type List(). The are both returning lists , however on my test I want to assert results of both methods. Both these methods are in a Class Called navigate. My assert statement isnt working :( - I want my test to fail if the values are the same and to pass if the values are not the same

    public List<Integer> methodA() {
        List<Integer> overallDurationAndTimeAfterWayPoint = new ArrayList<>();

        if (routeOptions.size() != 0) {
            for (int i = 0; i < routeOptionDescriptions.size(); i  ) {
                overallDurationAndTimeAfterWayPoint.add(Integer.parseInt(routeOptionDescriptions.get(i).getText()
                        .replaceAll("[^\\d.]", "").trim()));
                overallDurationAndTimeAfterWayPoint.add(Integer.parseInt(routeOptionTravelTimes.get(i).getText()
                        .replaceAll("[^\\d.]", "").trim()));
            }
        }
return overallDurationAndTimeAfterWayPoint;
    }

    public List<Integer> Method B() {
        List<Integer> overallDurationAndTimeAfterWayPoint = new ArrayList<>();

        if (routeOptions.size() != 0) {
            for (int i = 0; i < routeOptionDescriptions.size(); i  ) {
                overallDurationAndTimeAfterWayPoint.add(Integer.parseInt(routeOptionDescriptions.get(i).getText()
                        .replaceAll("[^\\d.]", "").trim()));
                overallDurationAndTimeAfterWayPoint.add(Integer.parseInt(routeOptionTravelTimes.get(i).getText()
                        .replaceAll("[^\\d.]", "").trim()));
            }
        }
        System.out.println("After "   overallDurationAndTimeAfterWayPoint);
        return overallDurationAndTimeAfterWayPoint;
    }


 Assert.assertTrue(navigate.MethodA().equals(navigate.MethodB()));

CodePudding user response:

You should do

Assert.assertFalse(navigate.MethodA().equals(navigate.MethodB()));

or

Assert.assertTrue(!navigate.MethodA().equals(navigate.MethodB()));

The assertions will throw an exception when the condition is not respected, so you need to assert that the two values are not equal. In this case, if they're not equal the assertion will pass

CodePudding user response:

Does order play role or not? That is something very important if you want to state if they are "same" or not.

Also I miss information what library and what version of this library are you trying to use....

Nevetheless - you still can compare those to lists as Strings.

String listAsString = list.stream()
  .map(n -> String.valueOf(n))
  .collect(Collectors.joining(",", "[", "]"));

that will result into [x,y,z,...] format. If order matter, you will not sort it before (also list is not best structure for something where order matters), if order does not matter - sort it first, and then compare String representations.

CodePudding user response:

If you know the exact expected result, you can

  • compare for the expected list length
  • check relevant elements from the list

If order does not matter, apply some sorting first as suggested by @Smeki.

  •  Tags:  
  • Related