亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Pmd規則:過多的靜態導入可能導致unitTest中的代碼混亂不違反

Pmd規則:過多的靜態導入可能導致unitTest中的代碼混亂不違反

Cats萌萌 2023-09-27 10:04:46
我有一個疑問,我有一個類,在其中我對導入常量使用不同的靜態導入,我的問題是我收到錯誤消息:Too many static imports may lead to messy code。但在單元測試中看起來這并不是一個壞習慣。例如,在單元測試類中,我使用此導入來解決任何問題: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;```相反,在我的課堂上: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;這些導入給我帶來了問題,我收到錯誤消息:太多靜態導入可能會導致代碼混亂。我不太確定為什么。我無法訪問 pmd 文件來找出此問題的真正原因。有什么線索嗎?
查看完整描述

5 回答

?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

我已經使用 @SuppressWarnings("PMD.TooManyStaticImports") 來避免在我的班級中出現此問題。



查看完整回答
反對 回復 2023-09-27
?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

如果您使用maven-pmd-plugin,它默認會忽略測試。您可以將其配置為includeTests.?

? ? <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>

關于TooManyStaticImports規則,就像任何其他 PMD 規則一樣,它可能是主觀的,并且不一定適合所有用例或代碼風格。這就是為什么它說“可能導致混亂的代碼”。

一般來說,如果您想靜音一個類的規則,您可以使用:

@SuppressWarnings("PMD.TooManyStaticImports")

或者,如果您想微調規則,您可以提供自己的自定義配置,例如:

<rule ref="category/java/codestyle.xml/TooManyStaticImports">

? ? <properties>

? ? ? ? <property name="maximumStaticImports" value="6" /> // Default is 4

? ? </properties>

</rule>


查看完整回答
反對 回復 2023-09-27
?
蝴蝶刀刀

TA貢獻1801條經驗 獲得超8個贊

這似乎是令人反感的 PMD 配置或規則;特別是在測試中,這種靜態導入是可以預料的。應更改 PMD 設置以完全禁用此檢查或不將其應用于測試代碼。



查看完整回答
反對 回復 2023-09-27
?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

基本上這是不好的做法,因為您引用另一個類的靜態字段,在某種程度上您可以從當前文件中尋址靜態屬性。為了防止出現此 PMD 錯誤,您必須進行合格的調用。


代替:


BAD_REQUEST;

INTERNAL_SERVER_ERROR;

NOT_FOUND;

UNPROCESSABLE_ENTITY;

稱呼:


HttpStatus.BAD_REQUEST;

HttpStatus.INTERNAL_SERVER_ERROR;

HttpStatus.NOT_FOUND;

HttpStatus.UNPROCESSABLE_ENTITY;


查看完整回答
反對 回復 2023-09-27
?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

您可以嘗試導入上一級。
例如,使用Constant.MY_VAR而不是直接使用MY_VAR. 因此,您不必在 Constant 中單獨導入每個變量,只需導入 Constant 并使用Constant.MY_VAR.

查看完整回答
反對 回復 2023-09-27
  • 5 回答
  • 0 關注
  • 227 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號