简体   繁体   中英

Sonar complains about assertThat with nullValue

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM