Gwt-junit-integration
GWT-JUnit統合
GWTは、JUnitテストフレームワークを使用したクライアント側コードの自動テストの優れたサポートを提供します。 この記事では、GWTとJUNITの統合について説明します。
Junitアーカイブをダウンロードする
JUnit公式サイト-https://www.junit.org/[https://www.junit.org]
ダウンロード Junit-4.10.jar
OS | Archive name |
---|---|
Windows | junit4.10.jar |
Linux | junit4.10.jar |
Mac | junit4.10.jar |
ダウンロードしたjarファイルをコンピューター上のある場所に保管します。 C:/> JUNIT に保存しました
GWTインストールフォルダーを見つける
OS | GWT installation folder |
---|---|
Windows | C:\GWT\gwt-2.1.0 |
Linux | /usr/local/GWT/gwt-2.1.0 |
Mac | /Library/GWT/gwt-2.1.0 |
GWTTestCaseクラス
GWTは、JUnit統合を提供する GWTTestCase 基本クラスを提供します。 JUnitでGWTTestCaseを拡張するコンパイル済みクラスを実行すると、テスト実行中のアプリケーションの動作をエミュレートするHtmlUnitブラウザーが起動します。
GWTTestCaseはJUnitのTestCaseから派生したクラスであり、JUnit TestRunnerを使用して実行できます。
webAppCreatorを使用する
GWTは、スターターテストケースを生成できる特別なコマンドラインツール webAppCreator に加えて、開発モードと本番モードの両方でテストするためのantターゲットとEclipse起動構成を提供します。
コマンドプロンプトを開き、 C:\> GWT_WORKSPACE> に移動して、テストサポート付きの新しいプロジェクトを作成します。次のコマンドを実行します。
注目すべきポイント
- webAppCreatorコマンドラインユーティリティを実行しています。
- HelloWorldは、作成するプロジェクトの名前です
- -junitオプションはwebAppCreatorにjunitサポートをプロジェクトに追加するよう指示します
- com.finddevguides.HelloWorldはモジュールの名前です
出力を確認します。
テストクラスの理解:HelloWorldTest.java
注目すべきポイント
Sr.No. | Note |
---|---|
1 | HelloWorldTest class was generated in the com.finddevguides.client package under the HelloWorld/test directory. |
2 | HelloWorldTest class will contain unit test cases for HelloWorld. |
3 | HelloWorldTest class extends the GWTTestCase class in the com.google.gwt.junit.client package. |
4 | HelloWorldTest class has an abstract method (getModuleName) that must return the name of the GWT module. For HelloWorld, this is com.finddevguides.HelloWorldJUnit. |
5 | HelloWorldTest class is generated with two sample test cases testFieldVerifier, testSimple. We’ve added testGreetingService. |
6 | These methods use one of the many assert* functions that it inherits from the JUnit Assert class, which is an ancestor of GWTTestCase. |
7 | The assertTrue(boolean) function asserts that the boolean argument passed in evaluates to true. If not, the test will fail when run in JUnit. |
GWT-JUnit統合の完全な例
この例では、簡単な手順でGWTでのJUnit統合の例を示します。
上記で作成したGWTアプリケーションを更新するには、次の手順に従います-
Step | Description |
---|---|
1 | Import the project with a name HelloWorld in eclipse using import existing project wizard (File → Import → General → Existing Projects into workspace). |
2 | Modify HelloWorld.gwt.xml, HelloWorld.css, HelloWorldl and HelloWorld.java as explained below. Keep rest of the files unchanged. |
3 | Compile and run the application to verify the result of the implemented logic. |
以下は、Eclipseのプロジェクト構造です。
以下は、変更されたモジュール記述子 src/com.finddevguides/HelloWorld.gwt.xml の内容です。
以下は、変更されたスタイルシートファイル war/HelloWorld.css の内容です。
以下は、変更されたHTMLホストファイル war/HelloWorldl の内容です。
生成された起動構成を使用してEclipseでテストケースを実行する
開発モードと本番モードの両方でwebAppCreatorによって生成された起動構成を使用して、Eclipseで単体テストを実行します。
開発モードでJUnitテストを実行する
- Eclipseメニューバーから、[実行]→[実行構成…]を選択します。
- JUnitセクションで、HelloWorldTest-devを選択します
- 引数への変更を保存するには、適用を押します
- テストを実行するには、実行を押します
すべてがあなたのアプリケーションでうまくいけば、これは次の結果を生成します-
実稼働モードでJUnitテストを実行する
- Eclipseメニューバーから、[実行]→[実行構成…]を選択します。
- JUnitセクションで、HelloWorldTest-prodを選択します
- 引数への変更を保存するには、適用を押します
- テストを実行するには、実行を押します
すべてがあなたのアプリケーションでうまくいけば、これは次の結果を生成します-