JUNIT HAMCREST cannot find symbol assertThat(java.lang.Long, org.hamcrest.Matcher<java.lang.Long>)

6.8k views Asked by At

So I have a simple entity:

//imports
....
@Entity
@Table(name="ratings")
public class Rating {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    public Long getId() {
            return id;
    }

    public void setId(Long id) {
            this.id = id;
    }

    ....
}

Test:

import static org.hamcrest.Matchers.*;

....
@Test
public void shouldCreateARating() throws Exception {
    Rating expected = createdRating;
    assertThat(existingRating.getId(), is(1L));
}
...

But when I try to compile, I get this compilation error:

[ERROR] /c:/limits/src/test/java/hello/RatingsControllerTest.java:[170,33] 
c:\limits\src\test\java\hello\RatingsControllerTest.java:170: cannot find symbol
symbol  : method assertThat(java.lang.Long,org.hamcrest.Matcher<java.lang.Long>)
location: class hello.RatingsControllerTest

I checked and is(T value) exists, and assertThat(T actual, org.hamcrest.Matcher<T> matcher) exist and are imported... so what is going on here? How can I test that a Long has the value I expect if combining the is and assertThat for Long generates a compilation error?

An explanation of why I am testing the get id --- it is a nested object that I save in setup() and its getId() value comes up as null in the test, even though I know that I save it (which hibernate generates an id for).

Making me feel like an idiot.

2

There are 2 answers

0
Foxsly On BEST ANSWER

Assuming you are using the most recent version of Hamcrest (1.3), the Matchers class doesn't have any assertThat methods on it.

You need to static import the MatcherAssert class:

import static org.hamcrest.MatcherAssert.*;
0
Alexandre Gabrielli On

for me I have to import

import static org.assertj.core.api.Assertions.assertThat;