1. 程式人生 > 其它 >通過Toggle 'Skip Tests' Mode跳過測試解決資料庫插入兩條資料的問題

通過Toggle 'Skip Tests' Mode跳過測試解決資料庫插入兩條資料的問題

通過Toggle 'Skip Tests' Mode(切換‘跳過測試’模式)解決執行一次新增記錄操作,資料庫卻插入兩條資料的問題

前段時間在做一個Maven專案的小功能時,需要將資料儲存進資料庫,但是卻遇到了插入一條記錄,執行成功後,資料庫卻存入兩條資料的情況。並且每次執行都會輸出如下的資訊:

[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------------< org.example:crud >--------------------------
[INFO] Building crud Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ crud ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 5 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ crud ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ crud ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ crud ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to G:\IdeaProjects\crud\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ crud ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-war-plugin:3.2.2:war (default-war) @ crud ---
[INFO] Packaging webapp
[INFO] Assembling webapp [crud] in [G:\IdeaProjects\crud\target\crud]
[INFO] Processing war project
[INFO] Copying webapp resources [G:\IdeaProjects\crud\src\main\webapp]
[INFO] Webapp assembled in [307 msecs]
[INFO] Building war: G:\IdeaProjects\crud\target\crud.war
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ crud ---
[INFO] Installing G:\IdeaProjects\crud\target\crud.war to E:\Programming\Maven\maven-repository\org\example\crud\1.0-SNAPSHOT\crud-1.0-SNAPSHOT.war
[INFO] Installing G:\IdeaProjects\crud\pom.xml to E:\Programming\Maven\maven-repository\org\example\crud\1.0-SNAPSHOT\crud-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.750 s
[INFO] Finished at: 2021-04-10T04:28:03+08:00
[INFO] ------------------------------------------------------------------------

經過多次測試檢查後,發現通過IntelliJ IDEA找到Maven Projects面板,頂部有一個Toggle 'Skip Tests' Mode 圖示(命令列方式:mvn clean package -Dmaven.test.skip=true),選中它後再執行package或者install打包就會跳過測試。
最後再去執行自己的業務程式碼,插入成功且只有一條記錄!後續對該功能問題有更好的理解再回來補充吧。