-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rule S7177: @DirtiesContext should be properly configured
- Loading branch information
1 parent
0aa2a9d
commit 830a23e
Showing
2 changed files
with
27 additions
and
27 deletions.
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
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 |
---|---|---|
@@ -1,44 +1,46 @@ | ||
FIXME: add a description | ||
|
||
// If you want to factorize the description uncomment the following line and create the file. | ||
//include::../description.adoc[] | ||
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. | ||
|
||
== Why is this an issue? | ||
|
||
FIXME: remove the unused optional headers (that are commented out) | ||
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 | ||
multiple times unnecessarily. This redundancy wastes resources, increases test execution time, and could lead to unexpected test failures. | ||
|
||
//=== What is the potential impact? | ||
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. | ||
|
||
== How to fix it | ||
//== How to fix it in FRAMEWORK NAME | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,java,diff-id=1,diff-type=noncompliant] | ||
---- | ||
FIXME | ||
@ContextConfiguration | ||
@DirtiesContext(methodMode = MethodMode.AFTER_METHOD) // Noncompliant, for class-level control, use classMode instead. | ||
public class TestClass { | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) // Noncompliant, for method-level control use methodMode instead | ||
public void test() {...} | ||
} | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,java,diff-id=1,diff-type=compliant] | ||
---- | ||
FIXME | ||
@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 | ||
public void test() {...} | ||
} | ||
---- | ||
|
||
//=== How does this work? | ||
|
||
//=== Pitfalls | ||
|
||
//=== Going the extra mile | ||
== Resources | ||
|
||
=== Documentation | ||
|
||
//== Resources | ||
//=== Documentation | ||
//=== Articles & blog posts | ||
//=== Conference presentations | ||
//=== Standards | ||
//=== External coding guidelines | ||
//=== Benchmarks | ||
* Spring documentation - https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/annotation/DirtiesContext.html[@DirtiesContext] |