Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hamcrest Matcher compilation difference between Eclipse and javac

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.

like image 697
Matt Avatar asked Nov 30 '11 13:11

Matt


People also ask

What is Hamcrest Matcher in Java?

Hamcrest is a popular framework that help us to create the matcher objects. It is used for writing software tests and also performs unit testing in Java programming language. Hamcrest is mainly used with other unit testing frameworks like JUnit, jMockit, Mockito, etc.

How does Hamcrest matcher work?

Hamcrest is a framework for writing matcher objects allowing 'match' rules to be defined declaratively. There are a number of situations where matchers are invaluable, such as UI validation or data filtering, but it is in the area of writing flexible tests that matchers are most commonly used.

Why use Hamcrest matchers?

Purpose of the Hamcrest matcher framework. Hamcrest is a widely used framework for unit testing in the Java world. Hamcrest target is to make your tests easier to write and read. For this, it provides additional matcher classes which can be used in test for example written with JUnit.

Is Hamcrest part of JUnit?

Overview. Hamcrest is the well-known framework used for unit testing in the Java ecosystem. It's bundled in JUnit and simply put, it uses existing predicates – called matcher classes – for making assertions.


1 Answers

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

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

like image 109
Maxence Avatar answered Nov 14 '22 23:11

Maxence