gpt4 book ai didi

java - AssertJ:从提取列表中检索第一项返回列表

转载 作者:行者123 更新时间:2023-12-02 11:13:17 25 4
gpt4 key购买 nike

我正在尝试验证提取列表中的第一项。然而 AssertJ 返回 List而不是 .first() 中的预期对象.

给出以下示例

public class AssertJTests
{
@Test
@DisplayName("Region Asia should only have one Country, Malaysia")
public void assertRegion()
{
Region asia = new Region()
.setName("Asia")
.setCountries(Lists.newArrayList("Malaysia"));

Consumer<Region> regionRequirement = region ->
assertThat(region)
.extracting(Region::getName)
.containsOnly("Asia");

Consumer<String> countryRequirement = country ->
assertThat(country)
.isEqualToIgnoringCase("Malaysia");

assertThat(asia)
.isInstanceOfSatisfying(Region.class, regionRequirement)
.extracting(Region::getCountries)
.hasSize(1)
.first()
.isInstanceOfSatisfying(String.class, countryRequirement);
}

protected static class Region
{
private String name;

private List<String> countries;

List<String> getCountries()
{
return countries;
}

Region setCountries(final List<String> pCountries)
{
countries = pCountries;
return this;
}

String getName()
{
return name;
}

Region setName(final String pName)
{
name = pName;
return this;
}
}
}

我在期待.first()返回String但它返回 List<String> 。出现以下错误:

java.lang.AssertionError: [List check first element] Expecting: <["Malaysia"]>to be an instance of: <java.lang.String>but was instance of: <java.util.ArrayList>

最佳答案

extracting 对于 Object断言提取被测对象(此处 asia )属性,从而产生 List提取的属性值。您的代码导致仅提取一个属性,因此结果是一个值的列表,事实证明该值也是一个列表,位于 extracting 之后你最终得到的是 List一个元素是 asia.countries .

关于java - AssertJ:从提取列表中检索第一项返回列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50461109/

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