# Testing Example Plugin - 2024

**URL:** <https://community.sonarsource.com/t/testing-example-plugin-2024/111737>\
**Category:** Plugin Development\
**Tags:** custom\_plugin, custom\_rules, sonarqube\
**Created:** [March 20, 2024, 6:55pm UTC](https://community.sonarsource.com/t/testing-example-plugin-2024/111737 "2024-03-20T18:55:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![balbi\_andre](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/balbi_andre/32/50559_2.png) [@balbi\_andre](https://community.sonarsource.com/u/balbi_andre)\
**Post date:** [March 20, 2024, 6:55pm UTC](https://community.sonarsource.com/t/testing-example-plugin-2024/111737/1 "2024-03-20T18:55:15Z")

</div>

I’m developing rules for a non-supported language (Gherkin) using [sonar-custom-plugin-example](https://github.com/SonarSource/sonar-custom-plugin-example) shown in this [doc](https://docs.sonarsource.com/sonarqube/latest/extension-guide/developing-a-plugin/plugin-basics/#api-basics)

**I need help to develop an unit test layer to assert that my rules are working correctly**. However, there is no template test layer for new language rules (like Foo), I tried creating one manually, but I don’t seem to find a way to run this program without fully packaging and installing it within a SonarQube instance.

**Problem**  
I don’t seem to find a way to instantiate a class that implements SensorContext (_org\sonar\api\batch\sensor\SensorContext.class_) to use in my test layer

**If possible, I’d like to see an template Unit Test using the Foo Language in this Example Plugin**

I’ve been searching around and found that [sonar-java](https://github.com/SonarSource/sonar-java/blob/master/docs/CUSTOM_RULES_101.md) has an implementation regarding testing, **is it possible to replicate this behaviour in my plugin**?

Cheers,  
André

---

<div class="post-metadata">

**Author:** ![Margarita\_Nedzelska](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/margarita_nedzelska/32/47493_2.png) [@Margarita\_Nedzelska](https://community.sonarsource.com/u/Margarita_Nedzelska)\
**Post date:** [March 22, 2024, 11:59am UTC](https://community.sonarsource.com/t/testing-example-plugin-2024/111737/4 "2024-03-22T11:59:51Z")

</div>

Hello @balbi_andre,

Thanks for raising your hand.

It’s not completely clear to me. Do you want to implement custom rules for Java language or do you want to implement a completely new plugin to analyze a new language?

If you want to write custom Java rules then you indeed need to use [this](https://github.com/SonarSource/sonar-java/blob/master/docs/CUSTOM_RULES_101.md) guide.

However, if you want to implement a completely new language plugin then you will need to use [this](https://github.com/SonarSource/sonar-analyzer-commons/tree/master/test-commons) dependency.

Let me know if that answers your question.

Best,  
Margarita

---

<div class="post-metadata">

**Author:** ![balbi\_andre](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/balbi_andre/32/50559_2.png) [@balbi\_andre](https://community.sonarsource.com/u/balbi_andre)\
**Post date:** [March 22, 2024, 2:27pm UTC](https://community.sonarsource.com/t/testing-example-plugin-2024/111737/6 "2024-03-22T14:27:30Z")

</div>

Hello @Margarita_Nedzelska, first of all, thanks for responding.

> [@Margarita\_Nedzelska](#):
>
> However, if you want to implement a completely new language plugin then you will need to use [this](https://github.com/SonarSource/sonar-analyzer-commons/tree/master/test-commons) dependency.

I believe the dependency you sent would not satisfy my needs.

**My objective** :  
On a Higher level: I want to use the example plugin as basis to integrate a non-supported Language and develop new rules.  
On a Lower level: I will use a Gherkin parser to analyse Karate DSL code in .feature files. Issues found will be reported and shown in SonarQube.

**My plan**  
I want to fork this [Example Plugin](https://github.com/SonarSource/sonar-custom-plugin-example) into a customized plugin where I can write my implementation.

**Problem**  
I can’t find a way to test this same plugin, there is no way to instantiate SensorContext to extract data from the file.

Is there any way to test rule implementation in this specific plugin, or should I explore another way? TDD is a priority for me.

Thanks!

---

<div class="post-metadata">

**Author:** ![Margarita\_Nedzelska](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/margarita_nedzelska/32/47493_2.png) [@Margarita\_Nedzelska](https://community.sonarsource.com/u/Margarita_Nedzelska)\
**Post date:** [March 22, 2024, 2:51pm UTC](https://community.sonarsource.com/t/testing-example-plugin-2024/111737/7 "2024-03-22T14:51:50Z")

</div>

@balbi_andre since now this is a bit more clear what you’re trying to achieve. So the dependency I shared with you is more to write unit tests and check the rules without the context. So You will need to create your verifier and create a mocked test context.

If you want to actually check the integration there’s a testkit for SQ called orchestrator: [GitHub - SonarSource/orchestrator: Java library for running SonarQube in tests](https://github.com/SonarSource/orchestrator) It is used for heavy integration tests.

For unit tests of the plugin or sensors, you can check out this dependency: [GitHub - SonarSource/sonar-plugin-api: API to develop plugins for SonarQube, SonarCloud and SonarLint](https://github.com/SonarSource/sonar-plugin-api). For example for having test sensor context you can use `SensorContextTester` as mentioned [here](https://github.com/SonarSource/sonar-plugin-api/blob/22fb5c1914b181fabf51089cd29281110ed79b69/plugin-api/src/main/java/org/sonar/api/batch/sensor/Sensor.java#L35).

Best,  
Margarita

---

<div class="post-metadata">

**Author:** ![balbi\_andre](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/balbi_andre/32/50559_2.png) [@balbi\_andre](https://community.sonarsource.com/u/balbi_andre)\
**Post date:** [June 17, 2024, 3:29pm UTC](https://community.sonarsource.com/t/testing-example-plugin-2024/111737/8 "2024-06-17T15:29:26Z")

</div>

**Thank you!**

SensorContextTester helped a LOT! However, I only got an actual test layer after learning how [sonar-slang](https://github.com/SonarSource/slang/blob/b3bb437b7c8d713e82fe23c26d99c0af8519fc48/slang-testing/src/main/java/org/sonarsource/slang/testing/AbstractSensorTest.java#L78) implemented SensorContextTester.

**TIPS for plugin development**

And, I recommend using [sonar-xml’s](https://github.com/SonarSource/sonar-xml/blob/master/sonar-xml-plugin/src/main/java/org/sonar/plugins/xml/XmlRulesDefinition.java) for metadata implementation using html and json files.

**These are the dependencies I used** (pom.xml) - `sonar-plugin-api-impl` helped a lot!:

```auto
<dependencies>
    <!-- https://mvnrepository.com/artifact/org.sonarsource.analyzer-commons/sonar-analyzer-commons -->
    <dependency>
      <groupId>org.sonarsource.analyzer-commons</groupId>
      <artifactId>sonar-analyzer-commons</artifactId>
      <version>2.11.0.2861</version>
    </dependency>

    <dependency>
      <groupId>org.sonarsource.api.plugin</groupId>
      <artifactId>sonar-plugin-api</artifactId>
      <version>10.7.0.2191</version>
      <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.sonarsource.api.plugin/sonar-plugin-api-test-fixtures -->
    <dependency>
      <groupId>org.sonarsource.api.plugin</groupId>
      <artifactId>sonar-plugin-api-test-fixtures</artifactId>
      <version>10.7.0.2191</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.30</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.17.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.google.code.findbugs/jsr305 -->
    <dependency>
      <groupId>com.google.code.findbugs</groupId>
      <artifactId>jsr305</artifactId>
      <version>3.0.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.sonarsource.analyzer-commons/sonar-xml-parsing -->
    <dependency>
      <groupId>org.sonarsource.analyzer-commons</groupId>
      <artifactId>sonar-xml-parsing</artifactId>
      <version>2.11.0.2861</version>
    </dependency>

    <!-- Test dependencies -->
    <!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
    <dependency>
      <groupId>org.assertj</groupId>
      <artifactId>assertj-core</artifactId>
      <version>3.26.0</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.16.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>5.10.2</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-migrationsupport -->
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-migrationsupport</artifactId>
      <version>5.10.2</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>5.12.0</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.sonarsource.analyzer-commons/sonar-analyzer-test-commons -->
    <dependency>
      <groupId>org.sonarsource.analyzer-commons</groupId>
      <artifactId>sonar-analyzer-test-commons</artifactId>
      <version>2.11.0.2861</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.sonarsource.sonarqube</groupId>
      <artifactId>sonar-plugin-api-impl</artifactId>
      <version>10.3.0.82913</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [June 24, 2024, 3:29pm UTC](https://community.sonarsource.com/t/testing-example-plugin-2024/111737/9 "2024-06-24T15:29:31Z")

</div>

Closed on June 24, 2024, 3:29pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
