1

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.

1
  • Could you please add the static import for assertThat to your Test code. Commented Nov 18, 2014 at 6:23

2 Answers 2

3

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.*; 
Sign up to request clarification or add additional context in comments.

Comments

0

for me I have to import

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.