如何在IntelliJ中使用SonarLint分析src/test/java下的测试用例
src/test/java with SonarLint in IntelliJ (No Folder Renaming Needed) Got it, let's break down the proper, non-hacky steps to get SonarLint analyzing your test cases in the src/test/java directory—no need to rename folders like you did with test → test1. The approach depends on whether you're using SonarLint in standalone mode or connected to your SonarQube instance.
Option 1: Connected Mode (Recommended, Syncs with SonarQube)
Since you already know about sonar.test.inclusions in SonarQube, this is the smoothest path because it inherits your existing server config:
- First, make sure your SonarQube project has
sonar.test.inclusions=src/test/java/**set up. You can add this either in your project'ssonar-project.propertiesfile or directly in the SonarQube web UI under Project Settings → General Settings → Analysis Scope. - In IntelliJ, open the SonarLint tool window (go to View > Tool Windows > SonarLint).
- Click the "Bind project to SonarQube/SonarCloud" button (looks like a cloud with a chain link). Follow the wizard to connect to your SonarQube server and select the matching project.
- Once bound, SonarLint will automatically pull down your SonarQube config—including the test inclusion rule. Now your
src/test/javafiles will be analyzed alongside your main code.
Option 2: Standalone Mode (Local, No SonarQube Connection)
If you're not using a SonarQube server, you can configure SonarLint directly in IntelliJ:
- Open IntelliJ's Settings (File > Settings on Windows/Linux, IntelliJ IDEA > Settings on Mac, or use the shortcut
Ctrl+Alt+S). - Navigate to Tools > SonarLint > Project Settings.
- Under the File Exclusions/Inclusions section, click the "+" button under Inclusions and add the pattern
src/test/java/**. - Next, ensure the directory is marked as a test source root: Right-click the
src/test/javafolder in your project pane, select Mark Directory as > Test Sources Root. This tells SonarLint to apply test-specific analysis rules (instead of main code rules) to these files.
Why Renaming test to test1 Worked
Quick side note: When you renamed the folder, IntelliJ probably didn't recognize test1 as a default test source root automatically, so SonarLint treated it like regular main code and analyzed it. But this is a workaround—marking the proper directory as a test source root and configuring inclusions is the correct approach.
Verify the Setup
To confirm everything's working:
- Right-click the
src/test/javadirectory in your project. - Select SonarLint > Analyze with SonarLint.
- Check the SonarLint tool window—you should see analysis results for your test cases now.
内容的提问来源于stack exchange,提问作者Diptman

