Hamcrest Matcher compilation difference between Eclipse and javac
Asked Answered
S

3

5

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.

Sgraffito answered 30/11, 2011 at 13:35 Comment(2)
Cannot reproduce. Which version of JDK do you use? Also, where does hasItem() come from?Erma
org.hamcrest.Matchers.hasItem() (sorry forgot it was statically imported. Using JDK 1.6.29 I believe.Sgraffito
V
6

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

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

Vulgus answered 8/8, 2012 at 15:1 Comment(3)
this answer saved my day.Curet
any idea why that is?Curet
do anyone know why the hamcrest TypeSafeMatcher subclass cannot be an inner class for javac to work when compiled from mvn 'mvn test'?Delbert
M
1

I would compare the JUnit and Hamcrest jars used in Eclipse as well as Maven. Many times Eclipse bundles its own JUnit and Hamcrest jars that are different than you might have defined in Maven pom.xml

Meehan answered 21/1, 2012 at 0:6 Comment(0)
D
0

Maxence is correct - your use of TypeSafeMatcher is the problem. However, if you use CustomTypeSafeMatcher instead it should allow the Maven build to complete successfully.

Dieldrin answered 22/1, 2016 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.