[英]How to assert position of element on the list (Confirm it is first element on the list)
I am fairly new at this. 我对此很陌生。 I am trying assert that elements are positioned in descending order and first one is latest dated one(2019,2018,2017,2016 etc)
我正在尝试断言元素以降序排列并且第一个是最新的日期(2019,2018,2017,2016等)
here are the elements with DATES: 以下是DATES的元素:
}-->2017/2018 }-> 2017/2018
}-->2016/2017 }-> 2016/2017
}-->2015/2016 }-> 2015/2016
}-->2014/2015 }-> 2014/2015
Solution using com.google.common.collect
is here : 使用
com.google.common.collect
解决方案在这里 :
boolean isSorted = Ordering.natural().reverse().isOrdered(actualDates);
Assert.assertTrue(isSorted);
You can use sort
and assert using TestNG: 您可以使用TestNG使用
sort
和assert:
// Assume it's your actual dates to check
List<String> actualDates = new ArrayList<>();
actualDates.add("2014/2015");
actualDates.add("2016/2017");
actualDates.add("2017/2018");
actualDates.add("2015/2016");
// Create new list, add all from actual list and order it.
List<String> sortedDates = new ArrayList<>(actualDates);
sortedDates.sort(Comparator.reverseOrder());
Assert.assertEquals(actualDates, sortedDates);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.