2017-12-13 17 views
1
내 시험 AssertJ을 사용하고

내림차순으로 정렬되어있는 경우 어설와 나는 List<T> 정렬되어있는 경우 확인하는 방법이있다주의 :AssertJ는 : 목록 역 또는 주문

public static <T> void sorted(final List<T> actual) { 
    try { 
     assertThat(actual).isSorted(); 
    } catch (AssertionError e) { 
     LOGGER.error(e.getMessage(), e); 
     throw e; 
    } 
} 

확인하는 방법이 있나요 목록이 내림차순으로 정렬 된 경우?

guava가 Ordering.natural().reverse().isOrdered(values)을 제공하지만 실제로 디버깅에 많은 도움이되는 AssertJ의 주장 메시지를 활용하고 싶습니다.

group is not sorted because element 5: 
<"4000366190001391"> 
is not less or equal than element 6: 
<"4000206280001394"> 
group was: 
<["4000206280001363", 
    "4000206280001364", 
    "4000206280001365", 
    "4000206280001373", 
    "4000206280001388", 
    "4000366190001391", 
    "4000206280001394", 
    "4000366190001401", 
    "4000206280001403", 
    "4000206280001405", 
    ....]> 

답변

2

예. Comparator을 사용하는 isSortedAccordingTo 메소드도 있습니다.

일반 유형 매개 변수를 <T extends Comparable<T>>으로 변경해야합니다. 그렇지 않으면 주문을 처리 할 방법이 없습니다.

public static <T extends Comparable<T>> void sorted(final List<T> actual) { 
    try { 
     assertThat(actual).isSortedAccordingTo(Comparator.reverseOrder()); 
    } catch (AssertionError e) { 
     LOGGER.error(e.getMessage(), e); 
     throw e; 
    } 
}