You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在IntelliJ中使用SonarLint分析src/test/java下的测试用例

How to Analyze 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.

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's sonar-project.properties file 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/java files 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/java folder 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/java directory 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:38:41