Java 插件 - SonarQube 添加自定义规则

如本Question所述,我已按照所有步骤操作并在/checks 文件夹中添加了一个 TestRule.java。

但是当我编译这个插件代码时,我得到以下错误:

17:02:52.614 [main] INFO  o.s.s.j.c.SecurityAnnotationMandatoryCheck - implements Interface : Second
Interface
17:02:52.614 [main] INFO  o.s.s.j.c.SecurityAnnotationMandatoryCheck - Method Name MySecurityAnnotat
ion
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.073 sec - in org.sonar.samples.jav
a.checks.SecurityAnnotationMandatoryCheckTest
Running org.sonar.samples.java.checks.TestRuleTests
17:02:52.617 [Report about progress of Java AST analyzer] INFO  org.sonar.squidbridge.ProgressReport
 - 1/1 source files have been analyzed
Executing *****************************TestRULE...........17:02:52.673 [main] INFO  org.sonar.squidb
ridge.ProgressReport - 1 source files to be analyzed
17:02:52.679 [main] DEBUG o.s.java.bytecode.ClassLoaderBuilder - ----- Classpath analyzed by Squid:
17:02:52.680 [main] DEBUG o.s.java.bytecode.ClassLoaderBuilder - C:\Users\dev\JavaPlugin\sonar-examples-master\sonar-examples-master\plugins\java-custom-rules\target\test-jars\commo
ns-collections4-4.0.jar
17:02:52.680 [main] DEBUG o.s.java.bytecode.ClassLoaderBuilder - C:\Users\dev\Jav
aPlugin\sonar-examples-master\sonar-examples-master\plugins\java-custom-rules\target\test-classes
17:02:52.680 [main] DEBUG o.s.java.bytecode.ClassLoaderBuilder - -----
17:02:52.688 [main] DEBUG o.s.java.resolve.BytecodeCompleter - Completing symbol : Object
17:02:52.691 [Report about progress of Java AST analyzer] INFO  org.sonar.squidbridge.ProgressReport
 - 1/1 source files have been analyzed
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.04 sec <<< FAILURE! - in org.sonar
.samples.java.checks.TestRuleTests
detected(org.sonar.samples.java.checks.TestRuleTests)  Time elapsed: 0.033 sec  <<< ERROR!
java.lang.IllegalStateException: At least one issue expected
        at org.sonar.samples.java.checks.TestRuleTests.detected(TestRuleTests.java:19)


Results :

Tests in error:
  TestRuleTests.detected:19 ╗ IllegalState At least one issue expected

Tests run: 8, Failures: 0, Errors: 1, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.750 s
[INFO] Finished at: 2015-12-04T17:02:53+05:30
[INFO] Final Memory: 28M/313M
[INFO] ------------------------------------------------------------------------

org.sonar.samples.java.checks.TestRuleTests.detected(TestRuleTests.java:19) 的代码如下:

import org.junit.Test;
import org.sonar.java.checks.verifier.JavaCheckVerifier;

import java.lang.System;

public class TestRuleTests {

    TestRule check = new TestRule();
    //check.max=100;

    @Test
    public void detected() {
        // Verifies that the check will raise the adequate issues with the expected message.
        // In the test file, lines which should raise an issue have been commented out
        // by using the following syntax: "// Noncompliant {{EXPECTED_MESSAGE}}"
        System.out.print("Executing *****************************TestRULE...........");
        JavaCheckVerifier.verify("src/test/files/TestRule.java", check); //Line number 19,this line is indicated in error
        System.out.print("After Executing *****************************TestRULE...........");
    }
}

如何添加问题?

最佳答案

如果您读到错误:您正在测试的文件中预计会出现问题。

因此,在“src/test/files/TestRule.java”中,您的规则应该提出一个问题,并使用注释检查://Noncompliant as specified in the comment in your test class.

如果您的检查绝对没有提出任何问题,那么您应该使用方法 verifyNoIssue 来避免错误。

https://stackoverflow.com/questions/34087690/

相关文章:

java - 在 Apache Spark SQL 中对多行进行操作

java - @Override,在接口(interface)中使用默认方法

android - 带有 Snackbar 和透明导航栏的 FloatingActionButton

android - 谷歌身份验证后,即将到来的网址不允许我在 Cordova 应用程序中走得更远

java - 为什么 Dagger 注入(inject)具体类?

python - 使用请求模块在 header 中获取 Set-Cookie

types - 如何声明 TypeScript 导入 * 类型?

sql - 带 Where 子句的 OFFSET SQL 查询

sql-server - 在 SQL Server 2012 的情况下,尝试在我的 select 语

python - 使用 python 3.5 用希伯来语和英语解析 json 文件