2015-12-23 19 views
1

greather考虑:assertj如何在列表中选中某个属性比的值

class Point { 
    int x; 
    int y; 
} 

List<Point> points; 

如何,我可以检查,在点列表属性x greather比的值?我的目标是类似于:后“”中提取

更新

assertThat(points).extracting("x").isGreatherThan(20) 

但是我无法找到“isGreatherThan”

我的目的不是编写自定义条件的这种价值检查,因为assertj已经有检查数字的方法。

感谢

回答

0

你可以试试这个...

.extracting("x", Integer.class).areAtLeast(1, greaterThan20); 

当然你必须write the condition yourself,像...

final Condition<Integer> greaterThan20 = new Condition<Integer>("greater than 20") { 

    @Override 
    public boolean matches(Integer value) { 
    return value.intValue() > 20; 
    } 
}; 
+0

我正在寻找没有定制条件的解决方案 – ejaenv

+0

祝你好运。 –

1

在Java 8,你可以做这样的事情:

assertThat(listOfPoints.stream().filter(p->p.x > 20).toArray()).hasSameSizeAs(listOfPoints); 

这对你想要的所有点有X> 20

要验证有至少一个(在弗洛里安Schaetz的答案)的情况下:

assertThat(listOfPoints.stream().filter(p->p.x > 20).toArray()).isNotEmpty(); 
+0

好主意,没有想到Java 8的流作为一种可能性。 –

+0

谢谢,但Lambdas是自定义条件。我的问题是,如果以某种方式,我可以使用assertj断言,如isGreaterThan检查列表中的数字。 – ejaenv

1

您可以使用filteredOn因为它支持java的8 Predicate,如:

assertThat(listOfPoints).filteredOn(p -> p.x > 20).isNotEmpty(); 

如果你想要做更复杂的东西,用Condition是要走的路,在AssertJ 3.X他们是简单的写,改写弗洛里安Schaetz例如:

Condition<Integer> greaterThan20 = new Condition<>(v -> v.intValue() > 20, "greater than 20"); 
+0

谢谢,但Lambdas是自定义条件。我的问题是,如果以某种方式我可以使用assertj断言,如isGreaterThan检查列表中的数字 – ejaenv

+0

不,你不能这样做。 –

相关问题