声纳抱怨用nullValue的assertThat [英] Sonar complains about assertThat with nullValue

查看:381
本文介绍了声纳抱怨用nullValue的assertThat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有使用JUnit 4.12的测试用例:

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()));
}

声纳说:

在此测试用例中至少添加一个断言.

Add at least one assertion to this test case.

Sonar为什么说没有断言,如何解决?

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

SonarQube v6.7

SonarQube v6.7

推荐答案

我刚刚找到了针对这种情况的解决方案:

I've just found a solution for this case:

此处应使用Hamcrest的MatcherAssert类中的assertThat方法,而不是JUnit的Assert类中的assertThat方法.因此,这里应该在下一次导入时使用

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;

代替:

import static org.junit.Assert.assertThat;

这篇关于声纳抱怨用nullValue的assertThat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆