简体   繁体   English

如何使用 AssertThat 检查列表中对象的属性值?

[英]How to use AssertThat to check the property value of objects in a list?

I am to using asserThat to verify properties of objects in an ArrayList.我将使用 asserThat 来验证 ArrayList 中对象的属性。

I am able to use assertThat if none of the objects contains data to be tested but I am not able to figure how to use assertThat if only one of the objects contain data to be tested.如果没有任何对象包含要测试的数据,我可以使用 assertThat 但如果只有一个对象包含要测试的数据,我无法弄清楚如何使用 assertThat。

Sample Code:示例代码:

public class dataNeeded {
    String data;
    String modifiedDate;

    public String getData(){
         return data;
    }
    ......
}

public ArrayList<dataNeeded> returnsNeededData(){
 ...
}

List<dataNeeded> dataToBeTested = returnsNeededData();
dataToBeTested.forEach(data->assertThat(data.getData(), not(equalTo("No"))));

List<dataNeeded> dataToBeTested2 = returnsNeededData();
// I need to verify if one of the objects in this list has it's data as "No" 
dataToBeTested.forEach(data->assertThat(data.getData(), ???);

You can map to List<String> via the following function (uses Java 8 Streams) to extract all data strings您可以通过以下函数(使用 Java 8 Streams)映射到List<String>以提取所有数据字符串

private List<String> mapToDataStrings(List<dataNeeded> neededData) {
    return neededData.stream()
            .map(dataNeeded -> dataNeeded.getData())
            .collect(toList());
}

and then use the everyItem / hasItem function in Matchers to make assertions on the resulting list of strings:然后使用everyItem / hasItem功能Matchers ,使生成的字符串列表上断言:

List<String> dataToBeTested = mapToDataStrings(returnsNeededData());
assertThat(dataToBeTested, everyItem(not("No")));

List<String> dataToBeTested2 = mapToDataStrings(returnsNeededData());
assertThat(dataToBeTested2, hasItem("No"));

With everyItem you can assert that every element in the list is not "No" and hasItem asserts that there is at least one element in the list with value "No".使用everyItem您可以断言列表中的每个元素都不是“No”,而hasItem断言列表中至少有一个值为“No”的元素。


Alternatively you could implement a feature matcher to avoid the mapping function.或者,您可以实现一个特征匹配器来避免映射函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 assertThat - hamcrest - 检查列表是否已排序 - assertThat - hamcrest - check if list is sorted 如何断言对象列表具有一组具有特定值的属性 - How to assertThat a list of objects has a set of properties with certain values 如何使用AssertJ检查一个属性值计数的对象列表? - How to check list of objects one property value count using AssertJ? 检查自定义对象列表对于 Java 8 中的属性是否具有相同的值 - Check whether list of custom objects have same value for a property in Java 8 如何在 assertThat 中使用带有类型推断的 hamcrest nullValue - How to use hamcrest nullValue with type inference in assertThat 如何将Hamcrest的AssertThat用于String [] - How to use Hamcrest's AssertThat for String[] 如何在 Junit (java) 中使用 assertThat 包含用于列表比较的任何顺序方法 - How to use assertThat Contains any order method for list comparison in Junit (java) 如何检查对象列表中是否存在枚举值 - How to check if enum value exists in a list of objects Java-如何为每个循环使用a检查对象列表中值的不同出现 - Java - How to use a for each loop to check the different occurrences of a value in a list of objects 如何根据 java 中的属性值将对象列表转换为字符串列表? - How transform a list of objects to a list of strings based on the value of a property in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM