Files
VV-ISTIC-TP3/exercises/pmd-test-smells.md
2025-12-19 21:52:35 +01:00

2.9 KiB

Detecting test smells with PMD

In folder 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:

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 la première 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 que quand il n'y a pas l'annotation @Test au début de la class test JUnit ne l'exécute donc 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);
    }