简体   繁体   English

声纳规则S2699:并非所有断言都被识别为有效断言

[英]Sonar rule S2699: Not all asserts are recognized as valid assertions

We are running Sonarqube 5.6.1 with the Java Plugin 4.1 and having some troubles using the Sonar rule S2699 (Test should include assertions). 我们使用Java插件4.1运行Sonarqube 5.6.1并且使用Sonar规则S2699遇到一些麻烦(测试应包括断言)。

Using this example test class 使用此示例测试类

import mypackage.Citit1543Dummy;
import mypackage.Citit1543OtherDummy;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;

import java.util.Arrays;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isIn;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.core.IsNot.not;
import static org.mockito.Matchers.notNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.junit.Assert.assertThat;

public class Citit1543Test {

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void test1() {
        assert true;
    }

    @Test
    public void test2() {
        Assert.assertTrue(1 > (2-3));
    }

    @Test
    public void test3() {
        Assert.assertFalse(1 > (100-1));
    }

    @Test
    public void test4() {
        Assert.assertThat("test", 1, is(1));
    }

    @Test
    public void test5() {
        Assert.assertArrayEquals(new String[0], new String[0]);
    }

    @Test
    public void test6() {
        Assert.assertEquals(1 > 0, true);
    }

    @Test
    public void test7() { // asserts in another method
        test7asserts(1, 1);
    }

    private void test7asserts(int a, int b) {
        Assert.assertTrue(a == b);
    }

    @Test
    public void test8() {
        test8asserts(1, 2);
    }

    private void test8asserts(int a, int b) {
        Assert.assertNotSame(a, b);
    }

    @Test
    public void test9() {
        Citit1543Dummy dummy = new Citit1543Dummy();
        dummy.otherDummy = mock(Citit1543OtherDummy.class);
        dummy.doSomething();
        verify(dummy.otherDummy, times(1)).doSomething();
    }

    @Test
    public void test10() {
        Citit1543Dummy dummy = new Citit1543Dummy();
        dummy.otherDummy = mock(Citit1543OtherDummy.class);
        dummy.doSomething();
        test10verifies(dummy.otherDummy);
    }

    private void test10verifies(Citit1543OtherDummy otherDummy) {
        verify(otherDummy, times(1)).doSomething();
    }

    @Test
    public void test11() {
        Assert.assertThat("test", "", not(1));
    }

    @Test
    public void test12() {
        Assert.assertThat("test", 1, lessThan(2));
    }

    @Test
    public void test13() {
        Long[] arr = new Long[] { 1L, 2L, 3L, 4L };
        assertThat("Just testing", arr, is(new Long[] {
            1L, 2L, 3L, 4L
        }));
    }
}

our Sonarqube instance flags the test cases test1 (assert statement not recognized), test7 (assert statements in another method), test8 (same) , test10 (Mockitos verify in another method), test11 and test13 as methods without assertions. 我们的Sonarqube实例标记测试用例test1 (断言语句未被识别), test7 (另一种方法中的断言语句), test8 (相同), test10 (Mockitos在另一种方法中verify ), test11test13作为没有断言的方法。 I'm pretty sure that there are a lot more methods which aren't recognized (yes, unfortunately we use a bunch of different mocking/testing framework across our projects). 我很确定有很多方法无法识别(是的,遗憾的是我们在我们的项目中使用了一堆不同的模拟/测试框架)。

For now, we started to //NOSONAR whenever one of the asserts/verifies aren't recognized. 现在,只要其中一个断言/验证无法识别,我们就开始//NOSONAR Is there an easy way to include these methods to be recognized as valid asserts? 是否有一种简单的方法可以将这些方法包含在有效的断言中?

Many of your stated issues are known and indeed (in some form of another) marked as FP: test1: The current flow analysis ignores assert statements. 许多已陈述的问题是已知的,并且确实(以某种形式的另一种)标记为FP:test1:当前流分析忽略断言语句。 See this post over at the groups. 在群组中查看此帖子

The cases test7, test8 and test10 are related to the lack of not having cross-procedural analysis: They are valid cases but the current flow doesn't know that (ex.) test7assert is a valid assert statement for another method. 案例test7,test8和test10与缺乏没有跨程序分析有关:它们是有效案例,但当前流程不知道(例如)test7assert是另一种方法的有效断言语句。 See this post over at the groups. 在群组中查看此帖子

Your other cases also produce false positives in the tests of S2699. 您的其他案例也会在S2699的测试中产生误报。 I'd expect that once a SonarSource dev reads this topic that they'll create a ticket to resolve the cases in test11/13. 我希望一旦SonarSource开发人员阅读这个主题,他们就会创建一张票来解决test11 / 13中的案例。 But as I'm not a dev of them I can't guarantee that of course. 但由于我不是他们的开发者,我当然不能保证。

As to : 至于:

Is there an easy way to include these methods to be recognized as valid asserts? 是否有一种简单的方法可以将这些方法包含在有效的断言中?

No, the valid assertions are defined within the code of S2699 and are not a parameter. 不,有效断言是在S2699的代码中定义的,不是参数。 Some of your cases will require a more complex flow analysis whilst the last couple just seem to boil down to some missing definitions or too strict definitions, but I didn't deep-dive into the reasons why they produce FPs. 你的一些案例需要更复杂的流程分析,而最后一对案例似乎归结为一些缺失的定义或过于严格的定义,但我并没有深入研究它们产生FP的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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