Quick revision
The short answer
A SonarQube quality gate is a set of pass or fail conditions, such as test coverage, duplicated lines, new bugs and vulnerabilities, and whether security hotspots were reviewed, that a project's analysis must meet. Many teams apply it only to new or changed code because that lets a legacy project improve steadily without first fixing thousands of old issues. SonarQube does not run tests itself; it imports coverage from JaCoCo XML reports, and CI can use the gate's result to block a pull request from being merged.
What to remember
In this answer, we saw that a SonarQube quality gate is a set of conditions on measures such as new issues, reviewed hotspots, coverage and duplications, and that the built-in gate applies them to new code so that legacy projects can improve as they change instead of fixing everything first. SonarQube imports coverage from JaCoCo XML reports rather than running tests, and CI or branch protection uses the gate result to block pull requests that do not meet it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A SonarQube quality gate is a set of pass or fail conditions, such as test coverage, duplicated lines, new bugs and vulnerabilities, and whether security hotspots were reviewed, that a project's analysis must meet. Many teams apply it only to new or changed code because that lets a legacy project improve steadily without first fixing thousands of old issues. SonarQube does not run tests itself; it imports coverage from JaCoCo XML reports, and CI can use the gate's result to block a pull request from being merged.
Why this question is asked
Almost every Java team meets SonarQube at some point, usually as a red or green badge on a pull request. Interviewers ask about quality gates because using them well depends on understanding what is actually measured and why the new code approach works, and because misconfigured gates are a common source of frustration. In this answer, we will recap how SonarQube analyses a project, see what a quality gate contains, understand why gates focus on new code, see how coverage gets into SonarQube, and wire the gate into pull request merges.
1A Quick Recap: How SonarQube Analyses a Project
SonarQube is a server that stores analysis results and shows them over time. The analysis itself runs in our build: the SonarScanner for Maven or Gradle reads our source code and compiled classes, applies the rules of the project's quality profile (the set of rules that is switched on), and sends the results to the server. Each finding is an issue, classified by the software quality it affects, such as reliability (likely bugs), security (vulnerabilities) and maintainability (code smells). Security hotspots are a separate category: code that is security sensitive and needs a human to review it, such as a hard-coded password-like string or a weak random number generator.
The server also computes measures, such as coverage, duplicated lines and ratings, for the whole project and for the new code. The quality gate is then evaluated on those measures.
2What a Quality Gate Contains
A quality gate is a list of conditions, each comparing one measure with a threshold. If every condition passes, the gate passes; if any fails, the gate fails. SonarQube ships a built-in gate, Sonar way, whose conditions in recent versions apply to new code only. The exact conditions and names vary between versions, so check yours, but they look like this.
| Condition | Threshold | What it protects |
|---|---|---|
| New issues (or new bugs and vulnerabilities) | 0, or rating A | No new likely defects |
| Security hotspots reviewed | 100 percent | Sensitive code was looked at |
| Coverage on new code | at least 80 percent | New code has tests |
| Duplicated lines on new code | at most 3 percent | No copy and paste growth |
3Why Apply It Only to New Code
Imagine a ten-year-old service with 6,000 issues and 35 percent coverage. A gate on overall code, such as overall coverage at least 80 percent, fails today and will keep failing for months whatever the team does, so it gets ignored or switched off. A gate on new code asks a different, achievable question: is the code you changed in this period, or this pull request, clean and tested?
This approach is often called Clean as You Code. It works because code that changes is the code people are actively reading and touching, so that is where quality matters most and where fixing is cheapest. Old code that nobody touches stays as it is, and code that is touched gets cleaner. Over a year or two, the share of clean code grows naturally, without a big clean-up project that rarely gets funded.
SonarQube needs to know what counts as new. The new code definition can be the changes since the previous version, changes in the last number of days, or, for branches and pull requests, the difference from a reference branch such as main. For pull requests, new code is simply the lines changed in that pull request.
4Where Coverage Comes From
A common misunderstanding is that SonarQube runs our tests. It does not. Tests run in the build, a coverage tool records which lines they executed, and SonarQube imports that report. For Java, the tool is JaCoCo, which writes an XML report that the scanner picks up.
Notice what happens if the report is missing, for example because the path is wrong or tests were skipped: SonarQube sees 0 percent coverage and the gate fails on coverage, even though tests exist. When a coverage condition fails unexpectedly, the first thing to check is whether the JaCoCo XML report was produced and found. In Gradle, the jacoco plugin's jacocoTestReport task with XML output enabled does the same job.
5Blocking Merges With the Gate
The gate is most useful when it acts before code is merged. There are two common ways. In the first, the CI job waits for the gate result, with sonar.qualitygate.wait=true as above, and fails the build if the gate fails. In the second, SonarQube reports the gate status back to the pull request in GitHub, GitLab, Bitbucket or Azure DevOps (pull request decoration, available in editions that support pull request analysis; check yours), and the repository's branch protection requires that status to pass before merging.
7 messages in order. Play them, or step through them one at a time.
Either way, the developer sees the result on the pull request with links to the exact new issues and uncovered lines, which is what makes the gate actionable rather than just a number.
6Follow-up questions
Is 80 percent coverage on new code the right threshold?
It is a reasonable default, not a rule. Some code, such as configuration classes or generated code, is not worth testing line by line, and we should exclude it from coverage rather than lower the bar for everything. Teams sometimes pick 70 percent to start. What matters more is that the tests assert real behaviour; coverage only shows that lines ran.
What happens to old issues when we apply the gate only to new code?
They stay visible in the overall measures and in the issue list, but they do not fail the gate. When someone changes a file, issues on the changed lines count as new for that period or pull request, so touched code gets cleaned. Teams can still plan focused clean-ups for high-value old issues, such as vulnerabilities.
What is the difference between a quality profile and a quality gate?
The quality profile decides which rules run and therefore which issues are raised. The quality gate decides which measures must pass for the analysis to be accepted. A noisy profile makes a gate on new issues fail for unimportant reasons, so tuning the profile and choosing the gate conditions go together.
7Conclusion
In this answer, we saw that a SonarQube quality gate is a set of conditions on measures such as new issues, reviewed hotspots, coverage and duplications, and that the built-in gate applies them to new code so that legacy projects can improve as they change instead of fixing everything first. SonarQube imports coverage from JaCoCo XML reports rather than running tests, and CI or branch protection uses the gate result to block pull requests that do not meet it.
Open with
A quality gate in SonarQube is a set of conditions that the analysis of a project or pull request must meet, like no new bugs or vulnerabilities, all new security hotspots reviewed, at least 80 percent coverage on new code, and at most 3 percent duplicated lines.
Then, if they want more
If any condition fails, the gate fails. Many teams, and the built-in Sonar way gate, apply it only to new or changed code.
The reason is practical: a legacy project might have thousands of issues and low coverage, so a gate on overall code would fail forever and get ignored, while a gate on new code is achievable and makes every change clean. Over time the code people actually touch gets better, without a big clean-up project.
One thing people often get wrong is coverage: SonarQube does not run tests. The build runs them with JaCoCo, which writes an XML report, and the scanner imports it, so if the report path is wrong the gate shows zero coverage.
Finally, the gate is enforced in CI, either with sonar.qualitygate.wait so the build fails, or by reporting the status to the pull request and making it a required check before merging.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Maven guides, Gradle user manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is a SonarQube quality gate, and why do many teams apply it only to new code?": https://scrivio-interview-library.netlify.app/java/code-quality-tools/792db06168/