今週も雑な覚書。記憶に定着させたい……。
[PowerShellとMavenとエラー]
たくさんJUnitテストがあって、修正範囲が限られているような場合に、手元で全てのJUnitテストをいちいち実行したくないことがある。
そんなときはこのブログ記事のお世話になっている。
最近やっとmvn test -Dtest=(実行したいテストクラスの完全修飾名=パッケージ名+クラス名)
のコマンドが手に馴染んできて、上記記事を参照しなくてもよくなってきた。
特定のメソッドのみテストする場合はクラスの完全修飾名#メソッド名
のように書くのもぎりぎり身についた。
しかし、あるとき何かの気の迷いでPowerShellでコマンドを実行しようとしたときに、これまでコマンドプロンプト(cmd.exe)では上手くいっていたコマンドがエラーになってしまった。
❯ mvn test -Dtest=com.kdnakt.simple.AppTest [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building simple-project 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.133 s [INFO] Finished at: 2021-XX-XXTXX:XX:XX+09:00 [INFO] Final Memory: 13M/307M [ERROR] Unknown lifecycle phase ".kdnakt.simple.AppTest". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/LifecyclePhaseNotFoundException
[""で.をエスケープ]
適当に試した結果、以下のように""
で完全修飾名を囲ってやることで、テストを実行することができた。
❯ mvn test -Dtest="com.kdnakt.simple.AppTest" [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building simple-project 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ simple-project --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory C:\sandbox\java-sandbox\simpleproject\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ simple-project --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ simple-project --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory C:\sandbox\java-sandbox\simpleproject\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ simple-project --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ simple-project --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.kdnakt.simple.AppTest [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.056 s - in com.kdnakt.simple.AppTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.823 s [INFO] Finished at: 2021-XX-XXTXX:XX:XX+09:00 [INFO] Final Memory: 12M/387M [INFO] ------------------------------------------------------------------------
[思い出すシリーズ:`でエスケープ]
ここまで試したあたりでぼんやりと記憶が呼び起こされた。
過去のブログを漁ると1年半ほど前に同じ問題にぶつかっていた。まるで成長していない……。
こちらのやり方だと以下のようになる。こちらの方がタイプ数が1文字少なくて済むしシンプルかも。過去の自分、やるな……。
❯ mvn test `-Dtest=com.kdnakt.simple.AppTest (結果は同じなので省略)
[まとめ]
- MavenでJUnitをクラス指定で実行する場合は
mvn test -Dtest=(クラスの完全修飾名)
- クラスの完全修飾名にドット(.)が含まれる場合、PowerShellでは
""
や`
でエスケープが必要 - 関連するコードは以下のリポジトリにまとめてある