JUNIT HAMCREST 找不到符号断言(java.lang.Long, org.hamcrest.Matcher<java.lang.Long>)



所以我有一个简单的实体:

//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;
    }
    ....
}

测试:

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

但是当我尝试编译时,我收到此编译错误:

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

我检查了一下,is(T value)存在,assertThat(T actual, org.hamcrest.Matcher<T> matcher)存在并被导入......这到底是怎么回事呢?如果将 is 和断言 for Long 组合生成编译错误,如何测试长整型是否具有我期望的值?

解释为什么我要测试 get id---它是我

保存在 setup() 中的嵌套对象,它的 getId() 值在测试中显示为空,即使我知道我保存了它(休眠生成一个 id)。

让我觉得自己像个白痴。

假设您使用的是最新版本的Hamcrest(1.3),则Matchers类没有任何assertThat方法。

您需要静态导入 MatcherAssert 类:

import static org.hamcrest.MatcherAssert.*;
对我来说

我必须导入

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

最新更新