'Pmd rule: Too many static imports may lead to messy code in unitTest is not violated
I have a doubt, I have a class in which I'm using different static import for import constants, my issue is that I'm getting the error message: Too many static imports may lead to messy code. But in the unit test it looks like it is not a bad practice. For example in a unit test class, I'm using this import with any problem:
import static com.rccl.middleware.kidsclub.engine.web.controller.KidController.KID_FIND_PATH;
import static com.rccl.middleware.kidsclub.engine.web.controller.KidController.KID_LIST_PATH;
import static com.rccl.middleware.kidsclub.engine.web.controller.KidController.KID_PATH;
import static com.rccl.middleware.kidsclub.engine.web.controller.KidController.KID_REGISTER_ALL_PATH;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.times;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;```
Conversely in my class:
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY;
These imports makes me problems and I get the error message: Too many static imports may lead to messy code. I'm not pretty sure why.
I am not able to access the pmd file to figured out the real cause of this issue. Any clue?
Thanks!
Solution 1:[1]
If you are using maven-pmd-plugin, it ignores tests by default. You could configure it to includeTests. See Docs
<plugin>
<artifactId>maven-pmd-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<rulesets>
<ruleset>/my-custom-rules.xml</ruleset> // Your own rules here
</rulesets>
<includeTests>true</includeTests> // Default value is false
</configuration>
</plugin>
Regarding the TooManyStaticImports rule, just like any other PMD rule, it can be subjective, and it won't necessarily fit all use cases or code styles. That is why it says "may lead to messy code". There are arguments for and against its usage.
In general, if you want to mute a rule for one class, you could use:
@SuppressWarnings("PMD.TooManyStaticImports")
Alternatively, if you want to fine-tune the rule, you could provide your own custom config e.g.:
<rule ref="category/java/codestyle.xml/TooManyStaticImports">
<properties>
<property name="maximumStaticImports" value="6" /> // Default is 4
</properties>
</rule>
Solution 2:[2]
This seems to be an objectionable PMD configuration or rule; especially in testing, this sort of static import is to be expected. The PMD setup should be changed either to disable this inspection entirely or not to apply it to test code.
Solution 3:[3]
While this isn't an answer to the question, it does make me wonder if PMD could have an annotation that allows users to override rule configurations in one or more source files or even a package? The only issue is that this would introduce a compile-time dependency. Would it be worth it if introduced at the package-level? Are there any rules with that kind of irritability quotient? Law of Demeter comes to mind.
As for the question itself, use star imports for static imports when you're practically importing the whole class. It's less verbose and less ugly.
Solution 4:[4]
To me, it's still better to import all static explicitly and concretely because it loads faster than importing all (*), and one reason is we prefer explicit things in Java.
So, I recommend to disable this PMD rules check.
Cheers.
Solution 5:[5]
You can try importing one level up.
For example, Use Constant.MY_VAR instead of directly using MY_VAR. So you don't have to import each variable separately in Constant, you can just import Constant and access the variable in code with Constant.MY_VAR.
Solution 6:[6]
Basically this is bad practice, as you refere to static fields from another Class, in a way you would address a static property from the current file. To prevent this PMD error you have to make a qualified call.
Instead of:
BAD_REQUEST;
INTERNAL_SERVER_ERROR;
NOT_FOUND;
UNPROCESSABLE_ENTITY;
call:
HttpStatus.BAD_REQUEST;
HttpStatus.INTERNAL_SERVER_ERROR;
HttpStatus.NOT_FOUND;
HttpStatus.UNPROCESSABLE_ENTITY;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | chrylis -cautiouslyoptimistic- |
| Solution 3 | |
| Solution 4 | turong |
| Solution 5 | Vishal Sharma |
| Solution 6 | JackLanger |
