Files
VV-ISTIC-TP3/exercises/pmd-test-smells.md
2025-12-05 12:46:46 +01:00

58 lines
2.9 KiB
Markdown

# Detecting test smells with PMD
In folder [`pmd-documentation`](../pmd-documentation) you will find the documentation of a selection of PMD rules designed to catch test smells.
Identify which of the test smells discussed in classes are implemented by these rules.
Use one of the rules to detect a test smell in one of the following projects:
- [Apache Commons Collections](https://github.com/apache/commons-collections)
- [Apache Commons CLI](https://github.com/apache/commons-cli)
- [Apache Commons Math](https://github.com/apache/commons-math)
- [Apache Commons Lang](https://github.com/apache/commons-lang)
Discuss the test smell you found with the help of PMD and propose here an improvement.
Include the improved test code in this file.
## Answer
On lance la commande
`pmd check -d ./test_folder/commons-math/ -R category/java/errorprone.xml/DetachedTestCase -r results_q2_tp3.txt -f text`
---
`
./test_folder/commons-math/commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/MathArraysTest.java:730: DetachedTestCase: Probable detached JUnit test case.
./test_folder/commons-math/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/SingularValueDecompositionTest.java:169: DetachedTestCase: Probable detached JUnit test case.
./test_folder/commons-math/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/SingularValueDecompositionTest.java:202: DetachedTestCase: Probable detached JUnit test case.
./test_folder/commons-math/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/regression/MillerUpdatingRegressionTest.java:559: DetachedTestCase: Probable detached JUnit test case.
`
---
On a pris le 1ere erreur:
```
public void testConcatenateEmptyArguments() {
final double[] x = new double[] {0, 1, 2};
final double[] y = new double[] {3};
final double[] z = new double[] {};
final double[] u = new double[] {0, 1, 2, 3};
Assert.assertArrayEquals(u, MathArrays.concatenate(x, z, y), 0);
Assert.assertArrayEquals(u, MathArrays.concatenate(x, y, z), 0);
Assert.assertArrayEquals(u, MathArrays.concatenate(z, x, y), 0);
Assert.assertEquals(0, MathArrays.concatenate(z, z, z).length);
}
```
On peut voir quand il n'a pas l'annotation `@Test` au début de class test donc normalement le test
qu'il a fait mais JUnit n'execute pas.
Voici la correction:
```
@Test
public void testConcatenateEmptyArguments() {
final double[] x = new double[] {0, 1, 2};
final double[] y = new double[] {3};
final double[] z = new double[] {};
final double[] u = new double[] {0, 1, 2, 3};
Assert.assertArrayEquals(u, MathArrays.concatenate(x, z, y), 0);
Assert.assertArrayEquals(u, MathArrays.concatenate(x, y, z), 0);
Assert.assertArrayEquals(u, MathArrays.concatenate(z, x, y), 0);
Assert.assertEquals(0, MathArrays.concatenate(z, z, z).length);
}
```