Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions rules/S8345/java/metadata.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
68 changes: 68 additions & 0 deletions rules/S8345/java/rule.adoc
Original file line number Diff line number Diff line change
@@ -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]

2 changes: 2 additions & 0 deletions rules/S8345/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}