Written Custom Java Rule: test Errors

Template for a good bug report, formatted with Markdown:

  • Versions used (SonarQube, Scanner, Plugin, and any relevant extension)
  • Error observed (wrap logs/code around triple quote ``` for proper formatting)
  • Steps to reproduce
  • Potential workaround
  • Scanner command used when applicable (private details masked)
  • In case of SonarCloud:
    • ALM used (GitHub, Bitbucket Cloud, Azure DevOps)
    • CI system used (Bitbucket Cloud, Azure DevOps, Travis CI, Circle CI, Jenkins, other)

Working on the initiative of writing Custom Java rules for sonarQube; Link to “How to Create Custom Java Rules 101”

https://docs.sonarqube.org/display/PLUG/Writing+Custom+Java+Rules+101#WritingCustomJavaRules101-Threefilestoforgearule

I am currently at this step:

Below are the error I am receiving upon testing:

java.lang.NoClassDefFoundError: org/sonar/api/config/internal/MapSettings
	at org.sonar.samples.java.checks.MyFirstCustomCheckTest.test(MyFirstCustomCheckTest.java:10)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.ClassNotFoundException: org.sonar.api.config.internal.MapSettings
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	... 24 more

Can some one please assist me in this regards:

Thanks:

Weakon

Hello,

I suspect the problem coming from the IDE, but I did not managed to reproduce your problem when I tried it this morning.
Do you have the same error if you try to build the project with the command line?

mvn clean install

I hope this can help to lead your research.

Best,
Quentin

Thanks @Quentin I sort out the error upon using mvn clean install

Hello,

actually, I encounter the same problem, but I don’t think it is a problem of the IDE.

We upgraded recently to the following versions:

SonarQube Server: 8.1.0.31237

sonar-java-plugin: 6.1.0.20866

I wrote some custom java rules for our own project, which I’m trying to upgrade for this version. That’s why I use the following dependencies:

<dependency>
    <groupId>org.sonarsource.java</groupId>
    <artifactId>sonar-java-plugin</artifactId>
    <type>sonar-plugin</type>
    <version>6.1.0.20866</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.sonarsource.sonarqube</groupId>
    <artifactId>sonar-plugin-api</artifactId>
    <version>8.1.0.31237</version>
    <scope>provided</scope>
</dependency>

And for Testing:

    <dependency>
        <groupId>org.sonarsource.java</groupId>
        <artifactId>java-checks-testkit</artifactId>
        <version>6.1.0.20866</version>
        <scope>test</scope>
    </dependency>

With this dependecies, the eroror mentioned above occurs, when I try to run the tests.

What I figured out is, that in version 6.1.0.2066 (and of cource also the previous ones) of the java-checks-testkit, the class org.sonar.java.checks.verifier.CheckVerifier , references the class org.sonar.api.config.internal.MapSettings. But org.sonar.api.config.internal.MapSettings was removed from the sonar plugin api in version 8.0, which causes the ClassNotFoundException when I try to run the tests.

Is this an error in the sonar java plugin or do I have do wait until I get a version of the plugin which supports the new sonar plugin api? When I compile against the version 7.9.2 of the sonar plugin api, everything works fine.

Actually I could fix the problem. I didn’t realize that I have to add

    <dependency>
        <groupId>org.sonarsource.sonarqube</groupId>
        <artifactId>sonar-plugin-api-impl</artifactId>
        <version>${sonar.plugin.api.version}</version>
        <scope>provided</scope>
    </dependency>

to my project dependencies.

1 Like