-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SONARJAVA-5291 Create rule S7177: @DirtiesContext should be properly configured #4610
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0aa2a9d
Create rule S7177
leonardo-pilastri-sonarsource 830a23e
Create rule S7177: @DirtiesContext should be properly configured
leonardo-pilastri-sonarsource 3d907ed
Fix typos
leonardo-pilastri-sonarsource c457a5e
Review changes
leonardo-pilastri-sonarsource 646e86c
Reduce scope
leonardo-pilastri-sonarsource 0dc9df2
Review changes
leonardo-pilastri-sonarsource c130f4d
Merge branch 'master' into rule/add-RSPEC-S7177
leonardo-pilastri-sonarsource c657562
Merge branch 'master' into rule/add-RSPEC-S7177
leonardo-pilastri-sonarsource 7dc80a8
Merge branch 'master' into rule/add-RSPEC-S7177
leonardo-pilastri-sonarsource File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"title": "@DirtiesContext should be properly configured", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
erwan-serandour marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-7177", | ||
"sqKey": "S7177", | ||
"scope": "Tests", | ||
"defaultQualityProfiles": ["Sonar way"], | ||
"quickfix": "unknown", | ||
"code": { | ||
"impacts": { | ||
"RELIABILITY": "MEDIUM" | ||
}, | ||
"attribute": "LOGICAL" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
The `@DirtiesContext` annotation configuration should make sense with the appropriate mode for the target scope. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, we can remove this part |
||
|
||
== Why is this an issue? | ||
|
||
The `@DirtiesContext` annotation marks the ApplicationContext as dirty and indicates that it should be cleared and recreated. | ||
This is important in tests that modify the context, such as altering the state of singleton beans or databases. | ||
However, improper use of the test phase configurations (such as `classMode` or `methodMode`) can lead to redundant or unnecessary context reloads, | ||
potentially impacting performance or leading to incorrect test behavior. | ||
erwan-serandour marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Misusing `@DirtiesContext` by selecting incorrect `classMode` and `methodMode` values can result in unnecessary context reloads. | ||
For example, using `BEFORE_EACH_TEST_METHOD` at the class level along with `AFTER_METHOD` at the method level triggers unnecessary context reloads increasing test execution time. | ||
This redundancy wastes resources, increases test execution time, and could lead to unexpected test failures. | ||
|
||
Also, configuring the `methodMode` at the class level or the `classMode` at the method level does not have meaning and will not have the expected behavior. | ||
|
||
erwan-serandour marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This rule will raise an issue when the incorrect mode is configured on a @DirtiesContext annotation targeting a different scope. | ||
erwan-serandour marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
== How to fix it | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,java,diff-id=1,diff-type=noncompliant] | ||
---- | ||
@ContextConfiguration | ||
@DirtiesContext(methodMode = MethodMode.AFTER_METHOD) // Noncompliant, for class-level control, use classMode instead. | ||
public class TestClass { | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) // Non compliant, for method-level control use methodMode instead | ||
public void test() {...} | ||
} | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,java,diff-id=1,diff-type=compliant] | ||
---- | ||
@ContextConfiguration | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) | ||
public class TestClass { | ||
@DirtiesContext(methodMode = MethodMode.AFTER_METHOD) | ||
public void test() {...} | ||
} | ||
---- | ||
|
||
== Resources | ||
|
||
=== Documentation | ||
|
||
* Spring documentation - https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/annotation/DirtiesContext.html[@DirtiesContext] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be clearer to use: Use appropriate @DirtiesContext mode or something similar