-
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
Conversation
3cdb736
to
4f435f1
Compare
4f435f1
to
830a23e
Compare
rules/S7177/java/rule.adoc
Outdated
[source,java,diff-id=1,diff-type=compliant] | ||
---- | ||
@ContextConfiguration | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) // Noncompliant, for class-level control, use classMode instead. |
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 there is a typo, it should not be non compliant
rules/S7177/java/rule.adoc
Outdated
@ContextConfiguration | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) // Noncompliant, for class-level control, use classMode instead. | ||
public class TestClass { | ||
@DirtiesContext(methodMode = MethodMode.AFTER_METHOD) // Noncompliant, for method-level control use methodMode instead |
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 there is a typo, it should not be non compliant
rules/S7177/java/rule.adoc
Outdated
== Why is this an issue? | ||
|
||
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 can result in the context being reloaded |
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.
For example, using `BEFORE_EACH_TEST_METHOD` at the class level along with `AFTER_METHOD` at the method level can result in the context being reloaded | |
For example, using `BEFORE_EACH_TEST_METHOD` at the class level with `AFTER_METHOD` at the method level triggers unnecessary context reloads increasing test execution time. |
I think we can be more direct it is not hypothetical and it is better to keep 1 sentence on the same line. It makes better diff afterwards in git.
rules/S7177/java/metadata.json
Outdated
@@ -0,0 +1,23 @@ | |||
{ | |||
"title": "@DirtiesContext should be properly configured", |
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
rules/S7177/java/rule.adoc
Outdated
@@ -0,0 +1,45 @@ | |||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
I think, we can remove this part
rules/S7177/java/rule.adoc
Outdated
This is important in tests that modify the context, such as altering the state of singleton beans or databases. | ||
|
||
Misconfiguring `@DirtiesContext` by selecting incorrect `classMode` and `methodMode` values will make the annotation have no effect. | ||
Setting the `methodMode` at the class level or the `classMode` at the method level does not have meaning and will not trigger the expected behavior. |
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 we can merge the two sentences :
Misconfiguring @DirtiesContext
by setting the methodMode
at the class level or the classMode
at the method level will make the annotation have no effect.
Quality Gate passed for 'rspec-tools'Issues Measures |
Quality Gate passed for 'rspec-frontend'Issues Measures |
You can preview this rule here (updated a few minutes after each push).
Review
A dedicated reviewer checked the rule description successfully for: