Spring Boot 1.3.x の Web アプリを 1.4.x へバージョンアップする ( その6 )( 「Run 'All Tests' with Coverage」実行時のエラーを解消する+build タスク実行時の警告を解消する )
概要
記事一覧はこちらです。
- 今回の手順で確認できるのは以下の内容です。
- 「Run ‘All Tests’ with Coverage」実行時のエラーの解消
- 上の対応にそんなに時間がかからなかったので、build タスク実行時に出る警告も解消します
参照したサイト・書籍
How to load freemarker templates from external file system folder with Spring Boot
http://www.sandc.software/blog/how-to-load-freemarker-templates-from-external-file-system-folder-with-spring-boot/Compilation error while upgrading from Guava 19 to 20
http://stackoverflow.com/questions/40364635/compilation-error-while-upgrading-from-guava-19-to-20google/error-prone
https://github.com/google/error-prone- このページを見ると Guava 20 から依存している error_prone_annotations ライブラリは Google の静的解析ツール関連のライブラリのようです。
目次
- 何のエラーが出ているのか?
java.lang.RuntimeException: freemarker.template.TemplateNotFoundException: Template not found for name ...
- 「Run ‘All Tests’ with Coverage」実行時のエラーが解消したので build タスクも試してみる
警告: タイプ'CompatibleWith'内に注釈メソッド'value()'が見つかりません: com.google.errorprone.annotations.CompatibleWithのクラス・ファイルが見つかりません
- 次回は。。。
手順
何のエラーが出ているのか?
失敗しているテストは 7 個、種類は以下の3種類でした。
- Mail001HelperTest, Mail002HelperTest, Mail003HelperTest, InquiringStatusOfBookQueueListenerTest
java.lang.RuntimeException: freemarker.template.TemplateNotFoundException: Template not found for name "mail/mail001-body.ftl".
のようにテンプレートファイルが見つからないというエラーが出ています。
- LendingappControllerTest$貸出申請画面の正常処理時のテスト
- java.lang.AssertionError: View name
- Expected :lendingapp/lendingapp
- Actual :error
- LendingapprovalControllerTest$貸出承認画面の正常処理時のテスト
- java.lang.AssertionError: View name
- Expected :lendingapproval/lendingapproval
- Actual :error
java.lang.RuntimeException: freemarker.template.TemplateNotFoundException: Template not found for name ...
上のエラーと一緒に
The name was interpreted by this TemplateLoader: MultiTemplateLoader(loader1 = FileTemplateLoader(baseDir=“C:\project-springboot\ksbysample-webapp-lending\build\resources\test\templates”, canonicalBasePath=“C:\project-springboot\ksbysample-webapp-lending\build\resources\test\templates\”), loader2 = ClassTemplateLoader(resourceLoaderClass=org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer, basePackagePath=“” / relatively to resourceLoaderClass pkg /)).
というエラーメッセージも出ています。テストクラスからテストを実行した時に src/main/resources/templates の下に配置したテンプレートファイルを参照できていないように見えます。
Web で何か情報がないかいろいろ検索するも見つからず。。。
どうしてよいのか困ってしまいましたが、設定で何とかなるように作られているはずと思い Spring Boot Reference Guide の Appendix A. Common application properties に記載されている spring.freemarker.~ の設定項目をいろいろ試してみました。
結論として以下の設定を application.properties に追加するとテストが成功するようになりました。
spring.freemarker.prefer-file-system-access=false
しかも他に出ていた java.lang.AssertionError: View name
のエラーも消えました。上の設定を入れた後に「Run ‘All Tests’ with Coverage」を実行した結果が以下の画像です。failed のテストが1つもありません。
Velocity → FreeMarker への移行方法は前回の記事にまとめておきたいので、Spring Boot 1.3.x の Web アプリを 1.4.x へバージョンアップする ( その5 )( メールのテンプレートに使用していた Velocity を FreeMarker に変更する ) にもこの内容を反映しました。
デフォルト値は false にした方が良さそうな気がしますが、なぜ true なのでしょうか? 自分の実装に今まで気付いていない問題があるのかもしれませんが、現時点では分かりませんでした。。。
「Run ‘All Tests’ with Coverage」実行時のエラーが解消したので build タスクも試してみる
「Run ‘All Tests’ with Coverage」でテストが全て成功するようになり、build タスクも通るのでは?と思ったので試してみます。
無事 “BUILD SUCCESSFUL” のメッセージが表示されました! ただし guava で警告が 10 個出力されているので、その原因を調査します。
警告: タイプ'CompatibleWith'内に注釈メソッド'value()'が見つかりません: com.google.errorprone.annotations.CompatibleWithのクラス・ファイルが見つかりません
出力されている警告は以下のものでした。
C:\Users\root.gradle\caches\modules-2\files-2.1\com.google.guava\guava\21.0\3a3d111be1be1b745edfa7d91678a12d7ed38709\guava-21.0.jar(com/google/common/collect/Multiset.class): 警告: タイプ'CompatibleWith'内に注釈メソッド'value()‘が見つかりません: com.google.errorprone.annotations.CompatibleWithのクラス・ファイルが見つかりません
調べると stackoverflow の QA で Compilation error while upgrading from Guava 19 to 20 が見つかりました。Guava の 20 から error_prone_annotations ライブラリに依存していると書かれているので、build.gradle に追加してみます。
build.gradle を リンク先の内容 に変更します。変更後、Gradle Tool Window の左上にある「Refresh all Gradle projects」ボタンをクリックして更新します。
clean タスク → Rebuild Project → build タスク の順に実行すると今度は警告が出ませんでした。
次回は。。。
今回初めて知った Google の Java 静的解析ツール Error Prone に興味が湧いたので試してみたいと思います。
ソースコード
build.gradle
dependencies { .......... // dependency-management-plugin によりバージョン番号が自動で設定されないもの、あるいは最新バージョンを指定したいもの runtime("${jdbcDriver}") compile("org.seasar.doma:doma:2.15.0") compile("org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16") compile("org.apache.commons:commons-lang3:3.5") compile("com.google.guava:guava:21.0") compileOnly("com.google.errorprone:error_prone_annotations:2.0.15") compile("org.simpleframework:simple-xml:2.7.1")
compileOnly("com.google.errorprone:error_prone_annotations:2.0.15")
を追加します。
履歴
2017/02/12
初版発行。