[英]JUNIT How to check the custom object retrieved is sorted in assertthat
嗨,我正在使用 Hibernate 以排序順序 FindByOrderByValueAsc 檢索元素。
如何檢查它是否在 Junit 中排序?
大概您在List<SomeType>
中有檢索到的元素。
編寫一個方法,該方法將使用Comparator<T>
迭代List<T>
> 以比較連續元素,如果一對無序則返回 false。
編寫斷言以使用適當的Comparator<SomeType>
對檢索到的元素調用方法。 如果調用返回false
,則斷言失敗。
為您感興趣的字段創建一個Comparator
器。
Comparator<YourObject> cmp =
Comparator.comparing(YourObject::getOrder)
.thenComparing(YourObject::getValue);
for(int i = 0; i < yourResult.size() - 1; ++i) {
YourObject left = yourResult.get(i);
YourObject right = yourResult.get(i + 1);
if(cmp.compare(left, right) > 0) {
Assert.fail("not sorted")
}
}
顯然,還沒有編譯任何代碼。
有些斷言庫可以與 JUnit 一起使用,它們具有比 JUnit 本身更復雜的斷言。 例如,您可以使用AssertJ編寫
assertThat(theList).containsExactly(firstValue, secondValue)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.