AssertJ:从提取列表 Returns 列表中检索第一项
AssertJ: Retrieving First Items From Extracting List Returns List Instead
我正在尝试验证提取列表中的第一项。但是 AssertJ return 是 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()
到 return String
,但 return 变成了 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
.
您可以使用 API 的 contains
和 extracting
方法断言。
例如:
@Test
public void oneDateShouldNotAfterAnotherDate() {
Set<String> errors = ...;
Assertions
.assertThat(errors)
.isNotEmpty()
.contains("OneDate_ShouldNot_After_AnotherDate");
/*
If you need assert specific field of object. Then:
*/
Set<Error> errors = ...;
Assertions
.assertThat(errors)
.isNotEmpty()
.extracting("message")
.contains("OneDate_ShouldNot_After_AnotherDate");
}
聚会晚了几年但是
.flatExtracting("countries")
应该对你有用
我正在尝试验证提取列表中的第一项。但是 AssertJ return 是 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()
到 return String
,但 return 变成了 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
.
您可以使用 API 的 contains
和 extracting
方法断言。
例如:
@Test
public void oneDateShouldNotAfterAnotherDate() {
Set<String> errors = ...;
Assertions
.assertThat(errors)
.isNotEmpty()
.contains("OneDate_ShouldNot_After_AnotherDate");
/*
If you need assert specific field of object. Then:
*/
Set<Error> errors = ...;
Assertions
.assertThat(errors)
.isNotEmpty()
.extracting("message")
.contains("OneDate_ShouldNot_After_AnotherDate");
}
聚会晚了几年但是
.flatExtracting("countries")
应该对你有用