2011年10月31日月曜日

[Java][Maven] 今さらかもだけどMaven入門してみた(4) - ユニットテスト

テストコードの作成
ユニットテストにはJUnitが利用されているので、テストコードの作成については、JUnitのリファレンスを参照する。

テストの実行
Run As > Maven test
で実行する。
そうすると、次のような出力が出る。

[INFO] Scanning for projects...
[INFO]                                                                      
[INFO] ------------------------------------------------------------------------
[INFO] Building M2ETest 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ M2ETest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Trials/M2ETest/M2ETest/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ M2ETest ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ M2ETest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Trials/M2ETest/M2ETest/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ M2ETest ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.7.1:test (default-test) @ M2ETest ---
[INFO] Surefire report directory: /Trials/M2ETest/M2ETest/target/surefire-reports
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running jp.test.m2e.foo.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.009 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.563s
[INFO] Finished at: Mon Oct 31 16:26:59 JST 2011
[INFO] Final Memory: 7M/81M
[INFO] ------------------------------------------------------------------------
加えて、target/surefire-reportsのなかに、テストクラスごとにレポートができる。こんな感じ。
-------------------------------------------------------------------------------
Test set: jp.test.m2e.foo.AppTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.009 sec



HTMLで結果を一覧して確認
でも、これだと全体を一度に見られないから不便…と検索したら、レポーティングの一部としてテストを実行することもできるらしい。(ていうかJUnitって結果どうやって見てるの?)
これだと全てのテストの結果をHTMLにまとめて見られる。他の人への状況のシェアも(必要なときは)できるし、とりあえずこれを覚えておけばいい気がする。


1) pom.xmlに次の通り記述
<reporting>
    <plugins>
...
        <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-surefire-report-plugin</artifactId>
        </plugin>
...
    </plugins>
</reporting>
2) プロジェクトディレクトリで次のコマンドを実行する。
mvn site(プロジェクトサイト作成コマンド。この場合、CSSや画像をダウンロードために実行)
mvn surefire-report:report
以上。良い感じのができてちょっと嬉しい。

0 件のコメント: