我正在尝试在 hasItem 匹配器中使用来自 hamcrest 的自定义匹配器
@Test
public void populatesChildCompanies() {
final long firstChildId = 2;
final String firstChildName = "jim";
final long secondChildId = 3;
final String secondChildName = "adam";
final List<Company> childCompanies = asList(createCompanyForRelation(firstChildCid, firstChildName),
createCompanyForRelation(secondChildCid, secondChildName));
company.getChildCompanies().addAll(childCompanies);
final CompanyOverview companyOverview = new CompanyOverview(company);
assertThat(companyOverview.getChildCompanies(), hasItem(companyRelation(firstChildName, firstChildId)));
assertThat(companyOverview.getChildCompanies(), hasItem(companyRelation(secondChildName, secondChildId)));
}
匹配器看起来像这样
public static final Matcher<CompanyRelation> companyRelation(final String name, final long id) {
return new TypeSafeMatcher<CompanyRelation>() {
@Override
protected boolean matchesSafely(final CompanyRelation companyRelation) {
return name.equals(companyRelation.getName()) && id == companyRelation.getId();
}
@Override
public void describeTo(final Description description) {
description.appendText(format("a company relation with a name of %s and a CID of %s", name, id));
}
@Override
protected void describeMismatchSafely(final CompanyRelation companyRelation, final Description description) {
description.appendText(format("[%s, %s]", companyRelation.getName(), companyRelation.getId()));
}
};
}
这在 eclipse 中运行得很好,但是当从命令行使用 maven 构建时,它会引发异常:
[ERROR] CompanyOverviewTest.java:[96,4] cannot find symbol
[ERROR] symbol : method assertThat(java.util.List<CompanyRelation>,org.hamcrest.Matcher<java.lang.Iterable<? super java.lang.Object>>)
我知道这是一个类型擦除问题,这是由于 eclipse 编译器和命令行之间存在一些差异,但我不确定处理它的最佳方法。