-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[java] UnnecessaryImport: false positives with static imports #4602
Comments
I use com.codeborne:selenide:5.20.0 |
But to be honest, I don't think it's about Selenide. I haven't reached the code of the rule itself yet due to the large volume of work tasks, but the situation is similar with JUnit and Hamcrest package ru.myorg.tests.calls;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import ru.myorg.Base;
import ru.myorg.CallLayer;
import ru.myorg.test.automation.junit.annotations.annotations.Tag;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class RateCallTest extends Base {
@Before
public void setUp() {
createBaseLayer();
}
@Test
@Tag(groups = {"testExample"})
public void rateTest() {
LOGGER.info("msg");
CallLayer callLayer = new CallLayer();
callLayer.rateCallExcellent();
assertFalse("Where is layer?", callLayer.isCallLayerVisible());
LOGGER.info("That all");
}
}```
Report:
/ruletest/test.java:3: UnnecessaryImport: Unused import 'org.junit.After' // ok
/ruletest/test.java:10: UnnecessaryImport: Unused static import 'org.junit.Assert.assertFalse' // not ok
/ruletest/test.java:11: UnnecessaryImport: Unused static import 'org.junit.Assert.assertTrue' // ok |
Another example. There are triggers not only for static imports: package ru.myorg.test.session;
import com.forticom.api.base.ApiException;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import ru.myorg.test.Base;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static ru.myorg.CustomMatchers.throwsException;
public class TestTest extends Base {
public final static String ERROR_NO_SESSION = "SESSION_ERROR";
@Test
@Tag("example")
public void testWithoutSession() {
assertThat(
"Where is error?",
() -> client.getService().register("id", null),
throwsException(ApiException.class, containsString(ERROR_NO_SESSION))
);
}
}
/ruletest/test.java:3: UnnecessaryImport: Unused import 'com.forticom.api.base.ApiException' // not ok
/ruletest/test.java:8: UnnecessaryImport: Unused static import 'org.hamcrest.Matchers.containsString' // not ok
/ruletest/test.java:9: UnnecessaryImport: Unused static import 'org.junit.Assert.assertThat' // not ok
/ruletest/test.java:10: UnnecessaryImport: Unused static import 'ru.myorg.CustomMatchers.throwsException' // not ok |
Hi @BurovnikovEvgeniy, In order to have this rule work correctly, you need to make sure, to use the correct classpath and have your project compiled. This is ensured by using maven and executing pmd in the "verify" phase. Also note, that in order to use PMD 7.0.0-rc3, you'll need to use a snapshot version of maven-pmd-plugin (see also #4478 (comment) ) Looking at your additional examples, I suspect that you don't compile your test project and don't use maven to execute PMD, which makes it way harder to provide the correct auxclasspath. Why do I think you don't use maven/don't compile? Because your java source file is called Could you please double check, that you execute PMD correctly? You can take my reproducer project as a starting point: https://github.com/adangel/scratchpad/tree/master/issue-4602-unnecessaryimport |
Hi @adangel. |
This "another project" is the project you want to analyze, correct? Which kind of project is "another project"? Does it use maven to build? If yes, then you can simply use the maven-pmd-plugin to execute PMD with the correct list of files and the maven-pmd-plugin will automatically calculate the correct aux-classpath. Can you describe in more detail, how exactly you actually integrate PMD in order to analyze "another project"? |
This issue is still present in 7.0.0. |
I'm also still facing this issue, please re-open. I. I'm using java 21, with maven-pmd-plugin 3.22.0 (should be using the default PMD 7.0.0) I get over 500 PMD failures complaining about |
As indicated on this issue, milestone is 7.1.0. 7.1.0 should be released before the end of the month. |
Thank you, i am also getting false positives for UnusedPrivateMethod when a private method is being used as part of @ParameterizedTest @MethodSource argument. Will this also be addressed alongside the false positives for UnnecessaryImport? |
That's a separate issue, open new one. |
Hey, guys! It hasn't been long, but I'm waiting for this feature. I really hope for you |
@BurovnikovEvgeniy You can now update PMD to 7.1.0 as described here: https://maven.apache.org/plugins/maven-pmd-plugin/examples/upgrading-PMD-at-runtime.html |
Affects PMD Version:
7.0.0-rc3
Rule:
UnnecessaryImport
Please provide the rule name and a link to the rule documentation:
https://docs.pmd-code.org/latest/pmd_rules_java_codestyle.html#unnecessaryimport
Description:
I'm trying to analyze a project with UI tests and check the imports used. But the result of the check is incorrect.
Suppressing these exceptions is too time-consuming for me, because these imports are in the entire project. There was no such problem in PMD 6.37, but when upgrading to 7.0.0 it showed up.
In CM 7.0.0-rc2, you wrote that this problem was detected and fixed (if I'm not confusing anything), but it didn't give a result (
Code Sample demonstrating the issue:
Expected outcome:
Expected no violations
PMD reports a violation, but that's wrong. That's a false positive.
Example:
/myPath/test.java:8: UnnecessaryImport: Unused static import 'com.codeborne.selenide.Condition.visible'
/myPath/test.java:9: UnnecessaryImport: Unused static import 'com.codeborne.selenide.Selenide.$'
Running PMD through: Maven
The text was updated successfully, but these errors were encountered: