TestSmells / TestSmellDetector

A tool to detect test smells in Java projects that utilize JUnit as the testing framework
https://testsmells.github.io/
GNU General Public License v3.0
73 stars 41 forks source link

magic number smell: Inappropriate default threshold #27

Open testmigrator opened 3 months ago

testmigrator commented 3 months ago

The default threshold is set to 0. Assuming a refactoring has already eliminated the magic number, re-testing should not detect this smell. However, because magicCount = 0 >= 0 (default threshold), the following code will still detect a magic number:

  override val magicNumberTest: Int
        get() = 0
 testMethod.setSmell(false); //default value is false (i.e. no smell)
 super.visit(n, arg);
 testMethod.setSmell(magicCount >= thresholds.getMagicNumberTest());
jose commented 3 days ago

I've just found the exact same issue. IMO, the fix is:

diff --git a/src/main/java/testsmell/smell/MagicNumberTest.java b/src/main/java/testsmell/smell/MagicNumberTest.java
index 6b756ed..c29c18f 100644
--- a/src/main/java/testsmell/smell/MagicNumberTest.java
+++ b/src/main/java/testsmell/smell/MagicNumberTest.java
@@ -52,7 +52,7 @@ public class MagicNumberTest extends AbstractSmell {
                 testMethod.setSmell(false); //default value is false (i.e. no smell)
                 super.visit(n, arg);

-                testMethod.setSmell(magicCount >= thresholds.getMagicNumberTest());
+                testMethod.setSmell(magicCount > thresholds.getMagicNumberTest());
                 testMethod.addDataItem("MagicNumberCount", String.valueOf(magicCount));
                 smellyElementsSet.add(testMethod);