SONARJAVA-5291 Create rule S7177: @DirtiesContext should be properly configured (#4610)
This commit is contained in:
parent
46eb087a63
commit
8aadee1de1
24
rules/S7177/java/metadata.json
Normal file
24
rules/S7177/java/metadata.json
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"title": "Use appropriate @DirtiesContext modes",
|
||||||
|
"type": "CODE_SMELL",
|
||||||
|
"status": "ready",
|
||||||
|
"remediation": {
|
||||||
|
"func": "Constant\/Issue",
|
||||||
|
"constantCost": "5min"
|
||||||
|
},
|
||||||
|
"tags": [
|
||||||
|
"spring"
|
||||||
|
],
|
||||||
|
"defaultSeverity": "Major",
|
||||||
|
"ruleSpecification": "RSPEC-7177",
|
||||||
|
"sqKey": "S7177",
|
||||||
|
"scope": "Tests",
|
||||||
|
"defaultQualityProfiles": ["Sonar way"],
|
||||||
|
"quickfix": "unknown",
|
||||||
|
"code": {
|
||||||
|
"impacts": {
|
||||||
|
"RELIABILITY": "MEDIUM"
|
||||||
|
},
|
||||||
|
"attribute": "LOGICAL"
|
||||||
|
}
|
||||||
|
}
|
42
rules/S7177/java/rule.adoc
Normal file
42
rules/S7177/java/rule.adoc
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
== 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.
|
||||||
|
|
||||||
|
Misconfiguring `@DirtiesContext` by setting the `methodMode` at the class level or the `classMode` at the method level will make the annotation have no effect.
|
||||||
|
|
||||||
|
This rule will raise an issue when the incorrect mode is configured on a @DirtiesContext annotation targeting a different scope.
|
||||||
|
|
||||||
|
== 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]
|
2
rules/S7177/metadata.json
Normal file
2
rules/S7177/metadata.json
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
{
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user