Skip to content
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 9 commits into from
Jan 27, 2025

Conversation

github-actions[bot]
Copy link
Contributor

You can preview this rule here (updated a few minutes after each push).

Review

A dedicated reviewer checked the rule description successfully for:

  • logical errors and incorrect information
  • information gaps and missing content
  • text style and tone
  • PR summary and labels follow the guidelines

@leonardo-pilastri-sonarsource leonardo-pilastri-sonarsource changed the title Create rule S7177 SONARJAVA-5291 Create rule S7177 Jan 24, 2025
@leonardo-pilastri-sonarsource leonardo-pilastri-sonarsource marked this pull request as ready for review January 24, 2025 10:02
[source,java,diff-id=1,diff-type=compliant]
----
@ContextConfiguration
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) // Noncompliant, for class-level control, use classMode instead.
Copy link
Contributor

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

@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
Copy link
Contributor

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

== 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

@leonardo-pilastri-sonarsource leonardo-pilastri-sonarsource changed the title SONARJAVA-5291 Create rule S7177 SONARJAVA-5291 Create rule S7177: @DirtiesContext should be properly configured Jan 24, 2025
@@ -0,0 +1,23 @@
{
"title": "@DirtiesContext should be properly configured",
Copy link
Contributor

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

@@ -0,0 +1,45 @@
The `@DirtiesContext` annotation configuration should make sense with the appropriate mode for the target scope.
Copy link
Contributor

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

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.
Copy link
Contributor

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.

Copy link

Quality Gate passed Quality Gate passed for 'rspec-tools'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

Copy link

Quality Gate passed Quality Gate passed for 'rspec-frontend'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

@leonardo-pilastri-sonarsource leonardo-pilastri-sonarsource merged commit 8aadee1 into master Jan 27, 2025
9 of 10 checks passed
@leonardo-pilastri-sonarsource leonardo-pilastri-sonarsource deleted the rule/add-RSPEC-S7177 branch January 27, 2025 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants