gpt4 book ai didi

java - 使用自定义对象验证数组

转载 作者:行者123 更新时间:2023-11-29 08:48:12 25 4
gpt4 key购买 nike

我有一个类:

public class MyCustomObject {
private String field1;
private String field2;
}

我创建了 MyCustomObject 数组:

MyCustomObject[] array = new MyCustomObject[]{new MyCustomObject()};

我的目标是使用 hamcrest 匹配器验证此数组的元素。我尝试了以下方法:

assertThat(array, allOf(hasItemInArray(hasProperty("field1", equalTo("value1")))), hasItemInArray(hasProperty("field2", equalTo("value2")))));

但不幸的是,它不起作用。

可以通过哪些方式验证自定义对象数组?

最佳答案

为了测试目的,我会将您的数组更改为 ArrayList:

List<MyCustomObject> customObjects = Arrays.asList(array);

然后使用 Hamcrest hasItems 匹配器断言列表中是否存在预期的项目:

assertThat(customObjects, hasItems(myCustomObject1, myCustomObject2));

关于java - 使用自定义对象验证数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24032092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com