diff --git a/rules/S8345/java/metadata.json b/rules/S8345/java/metadata.json new file mode 100644 index 00000000000..d3a4e0bc6a5 --- /dev/null +++ b/rules/S8345/java/metadata.json @@ -0,0 +1,25 @@ +{ + "title": "FIXME", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-8345", + "sqKey": "S8345", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "MAINTAINABILITY": "HIGH", + "RELIABILITY": "MEDIUM", + "SECURITY": "LOW" + }, + "attribute": "CONVENTIONAL" + } +} diff --git a/rules/S8345/java/rule.adoc b/rules/S8345/java/rule.adoc new file mode 100644 index 00000000000..181fdf1f5e4 --- /dev/null +++ b/rules/S8345/java/rule.adoc @@ -0,0 +1,68 @@ +This rule raises an issue when a test annotated with the `@ParameterizedTest` +annotation does not take the parameterized argument(s) as a method parameter(s). + +== Why is this an issue? + +Methods annotated with JUnit's `@ParameterizedTest` annotation are designed to run +multiple times with different arguments provided with annotations, such as, +`@ValueSource`, `@CsvSource`, and `@MethodSource`. This rule raises an issue when +a method is marked with such annotation but the provided parameterized parameters are not used (either +when there is no corresponding method parameter, or when the method parameter is not used in the +test body) or if the number of method parameters doesn't match the parameterized parameters given. +This rule highlights this discrepancy, encouraging developers to either use the +provided parameters or remove the unnecessary annotations. + + +=== What is the potential impact? +This can lead to the developer believing the test is being run +with multiple values, when in fact it's executed multiple times with the same behavior. + +=== How to fix it +To fix the issue, use the parameterized parameters or remove them. + +=== Code examples + +==== Noncompliant code example + +[source,java,diff-id=1,diff-type=noncompliant] +---- +@ParameterizedTest +@ValueSource(strings = {"a", "b", "c"}) +void test() { // Noncompliant + // The values "a", "b", "c" are not used here. + // The test will run, but the parameterization is pointless. +} + +@ParameterizedTest +@ValueSource(strings = {"a", "b", "c"}) +void test(String value) { // Noncompliant + // value is not used +} + +@ParameterizedTest +@ValueSource(strings = {"a", "b", "c"}) +void test(String key, String value ) { // Noncompliant +} +---- +==== Compliant solution + +[source,java,diff-id=1,diff-type=compliant] +---- +@ParameterizedTest +@ValueSource(strings = {"a", "b", "c"}) +void test(String value) { // Compliant + // use `value` +} +---- + +=== Message +Method argument should match parametrized parameters + +=== Pitfalls +In the case the number of arguments is determined by the value of the annotations +(`@CsvSource`, `@CsvFileSource`, `@MethodSource`, `@FieldSource`, `@ArgumentSource`) and not the annotations itself, +the analysis matching the number of method arguments is not performed. + +=== Documentation +https://docs.junit.org/current/user-guide/#writing-tests-parameterized-tests[Parameterized Test Documentation] + diff --git a/rules/S8345/metadata.json b/rules/S8345/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S8345/metadata.json @@ -0,0 +1,2 @@ +{ +}