Difference between Hamcrest Matcher compiler between Eclipse and javac

I am trying to use a custom match from hamcrest inside a hasItem socket

  @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)));
  }

Compatible as follows

  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 works very well due to the eclipse, but when building with maven from the 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-erase problem, and due to some difference between the eclipse compiler and the command line, but I'm not sure if you can handle it.

+5
source share
3 answers

the problem occurs when the TypeSafeMatcher implementation is an inner class.

Moving the helper to a single .java file should solve your problem.

+5

JUnit Hamcrest, Eclipse, Maven. Eclipse JUnit Hamcrest , , Maven pom.xml

+1

Maxence is correct - a problem with using TypeSafeMatcher. However, if you use CustomTypeSafeMatcher , this should allow you to complete the Maven build.

0
source

All Articles