Sonar complains about assertThat with nullValue

2019-08-26 09:15发布

Having the test case using JUnit 4.12:

import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;

//...

@Test
public void testShouldReturnNull() {
    final Long result = getIdFunction.apply(null);
    assertThat(result, is(nullValue()));
}

Sonar says:

Add at least one assertion to this test case.

Why does Sonar says that there are no assertions and how it can be fixed?

SonarQube v6.7

1条回答
smile是对你的礼貌
2楼-- · 2019-08-26 09:42

I've just found a solution for this case:

Here should be used assertThat method from Hamcrest's MatcherAssert class instead of JUnit's Assert class. So here should be used next import:

import static org.hamcrest.MatcherAssert.assertThat;

instead of:

import static org.junit.Assert.assertThat;
查看更多
登录 后发表回答