I am trying to make use of a custom matcher from hamcrest within the hasItem matcher
@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)));
}
The matcher looks like this
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()));
}
};
}
This operates just fine from within eclipse but when built with maven from command line it throws an exception:
[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>>)
I know this is a type erasure problem, and that it's due to some difference between the eclipse compiler and the command line, but I am unsure of the best way to handle it.
hasItem()
come from? – Erma