Sonar complains about assertThat with nullValue

六月ゝ 毕业季﹏ 提交于 2019-12-11 16:47:49

问题


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:


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;


来源:https://stackoverflow.com/questions/49070436/sonar-complains-about-assertthat-with-nullvalue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!