我正在尝试在hasItem匹配器中使用来自Hamcrest的自定义匹配器。
匹配器看起来像这样:
我知道这是一个类型擦除问题,由于Eclipse编译器和命令行之间的某些差异导致的,但我不确定处理它的最佳方法。
@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编译器和命令行之间的某些差异导致的,但我不确定处理它的最佳方法。
hasItem()
来自哪里? - axtavt