Spring-MVC/ステップ・バイ・ステップ/プログラムのテスト
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
[[Spring-MVC/ステップ・バイ・ステップ]]
2008/03/23からのアクセス回数 &counter;
#contents
プログラムのデバッグできるようになりましたので、次はmaven...
できるようにしましょう。
Springの提供するテストには、
- 単体テスト
- 結合テスト
- トランザクションテスト
があります。
ここでは、JunitとMockHttpServletRequestの使い方について説...
** テストの準備 [#u12fe24a]
*** ディレクトリの追加 [#sdcbbc65]
mavenのWebアプリケーションにテストケースを組み込む場合に...
#pre{{
+ mvc-convention/
|- pom.xml
|-+ src/
|-+ main/
| |-+ resources/
| |-+ webapp/
| |-+ WEB-INF/
| |- web.xml
| |- index.jsp
|-+ test/
|-+java/ ← 追加
|-+resources/ ← 追加
}}
- src/test/java/ には、テストケースのjavaプログラムを配置
- src/test/resources/ には、テスト用のリソースファイルを...
を追加します。
*** ライブラリの追加 [#gd0b07de]
次に、テストに使用するSpringのライブラリをpom.xmlに追加し...
テストには、spring-testが必要です。[[MVNRepository>http:/...
#pre{{
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>2.5.2</version>
</dependency>
}}
をpom.xmlに追加します。
*** クラスパスの変更 [#iff2d857]
以下のコマンドを実行して.project, .classpathファイルを更...
#pre{{
$ rm .project .classpath
$ mvn eclipse:eclipse -DdownloadSources=true
}}
** テストケースの作成 [#ac895904]
準備ができましたので、テストケースを作成しましょう。
Springでは、
- AbstractDependencyInjectionSpringContextTests
- AbstractTransactionalSpringContextTests
- AbstractTransactionalDataSourceSpringContextTests
のテストケースを提供しています。今回はトランザクションの...
*** クラスの単体テスト [#z4c124fd]
最初にStubRecipeManagerのfindByIdの単体テスト作ってみまし...
src/test/java以下にSampleTestCase.java として以下のファイ...
#pre{{
import org.springframework.showcase.coverc.domain.Recipe;
import org.springframework.showcase.coverc.service.StubRe...
import org.springframework.test.AbstractDependencyInjecti...
public class SampleTestCase extends AbstractDependencyInj...
public void testFindById() {
StubRecipeManager manager = new StubRecipeManager();
Recipe recipe = manager.findById(1L);
assertNotNull(recipe);
assertEquals("Goats Cheese with beetroot sauce", recipe...
}
}
}}
JUnitでは、個々のテストは、''public void testXXXX()'のよ...
testFindByIdでの処理は以下の通りです。
- StubRecipeManagerのインスタンスmanagerを作成する
- managerにfindByIdメソッドでIdが1のrecipeを取り出す。
- recipeがNullでなく、nameが"Goats Cheese with beetroot s...
*** 単体テストの実行 [#h2de5ac9]
テストケースができたので、実際にJunitを使った単体テストを...
mavenでは、単体テストを簡単に実行するために、''test''とい...
コマンドラインから以下のコマンドを入力すると、自動的にSma...
#pre{{
$ mvn test
Listening for transport dt_socket at address: 8000
[INFO] Scanning for projects...
[INFO] --------------------------------------------------...
[INFO] Building mvc-convention Maven Webapp
[INFO] task-segment: [test]
[INFO] --------------------------------------------------...
-- 途中省略
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running SampleTestCase
2008/03/23 16:47:00 org.springframework.test.AbstractDepe...
情報: ApplicationContext has not been configured for test...
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time el...
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
}}
テストで1つの情報(Macでは???と文字化けします)がでます。
- SpringのBean定義が設定されていない旨の警告
テストの結果は、Results:以下の
#pre{{
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
}}
で確認します。
1個のテストを実行し、失敗、エラー、スキップがすべて無いこ...
もちろん、Eclipseを使って単体テストを実行することも可能で...
- Runメニューから"Run..."を選択する
- RunダイアログのJUnitを選択し、右クリックでNewを選択する
- RunダイアログのRunボタンを押す
とJUnitが実行します。
** Mockオブジェクトを使った結合テスト [#yb48f909]
次に、SpringのBean定義ファイルを使った結合テストの方法に...
*** リソースのコピー [#j8ca0af0]
最初にBean定義ファイルをテスト用リソースファイルにコピー...
- src/test/resources/にWEB-INFディレクトリを追加します
- src/main/webapp/WEB-INF以下のapplicationContext.xml, co...
coverc-servlet.xmlのSwitchBoardContrllerの定義に以下のよ...
#pre{{
<bean id="switchBoadController" class="org.springfram...
parent="baseRecipeController"/>
}}
*** テストケースでのBean定義ファイル設定方法 [#l3e8c69b]
テストケースで使用するSpringのBean定義ファイルは、getConf...
する約束になっています。
getConfigLocationsは以下のように定義します。
#pre{{
public String[] getConfigLocations() {
return new String[] {
"WEB-INF/applicationContext.xml",
"WEB-INF/coverc-servlet.xml"
};
}
}}
*** テストメソッドの追加 [#l054839d]
Mockオブジェクトを使った結合テストの例を以下に示します。
- MockHttpServletRequestを使ってMockのHTTPServletRequest...
- SpringのBean定義に従って生成されたApplicationContextか...
- switchBoadControllerにHTTPServletRequestを処理(handleR...
- hadleRequestで返されたModelAndViewのviewNameがnullであ...
- ModelAndViewのmodelからrecipeListを取り出します。
- recipeListのサイズが3であることを確認します
- 最初のRecipeのNameが”Goats Cheese with beetroot sauce”...
#pre{{
public void testIntegrated() throws Exception {
MockHttpServletRequest req = new MockHttpServletRequest...
SwitchBoardController controller =
(SwitchBoardController)getApplicationContext().getBea...
ModelAndView mv = controller.handleRequest(req,new Mock...
assertNull(mv.getViewName());
List list = (List)mv.getModel().get("recipeList");
assertEquals(3, list.size());
Recipe recipe = (Recipe)list.get(0);
assertEquals("Goats Cheese with beetroot sauce", recipe...
}
}}
*** 結合テストの実行 [#he6d6a2c]
Bean定義ファイルを追加し、Mockオブジェクトを使った結合テ...
出力されます。
#pre{{
$ mvn test
-- 途中省略
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running SampleTestCase
2008/03/23 18:28:31 org.springframework.test.AbstractSing...
-- 途中省略
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time el...
Results :
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
}}
2個のテストメソッドが正常に動作していることが確認できまし...
#ref(SampleTestCase.java);
** コメント [#o04e4088]
この記事は、
#vote(おもしろかった[7],そうでもない[3],わかりずらい[11])
皆様のご意見、ご希望をお待ちしております。
#comment_kcaptcha
終了行:
[[Spring-MVC/ステップ・バイ・ステップ]]
2008/03/23からのアクセス回数 &counter;
#contents
プログラムのデバッグできるようになりましたので、次はmaven...
できるようにしましょう。
Springの提供するテストには、
- 単体テスト
- 結合テスト
- トランザクションテスト
があります。
ここでは、JunitとMockHttpServletRequestの使い方について説...
** テストの準備 [#u12fe24a]
*** ディレクトリの追加 [#sdcbbc65]
mavenのWebアプリケーションにテストケースを組み込む場合に...
#pre{{
+ mvc-convention/
|- pom.xml
|-+ src/
|-+ main/
| |-+ resources/
| |-+ webapp/
| |-+ WEB-INF/
| |- web.xml
| |- index.jsp
|-+ test/
|-+java/ ← 追加
|-+resources/ ← 追加
}}
- src/test/java/ には、テストケースのjavaプログラムを配置
- src/test/resources/ には、テスト用のリソースファイルを...
を追加します。
*** ライブラリの追加 [#gd0b07de]
次に、テストに使用するSpringのライブラリをpom.xmlに追加し...
テストには、spring-testが必要です。[[MVNRepository>http:/...
#pre{{
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>2.5.2</version>
</dependency>
}}
をpom.xmlに追加します。
*** クラスパスの変更 [#iff2d857]
以下のコマンドを実行して.project, .classpathファイルを更...
#pre{{
$ rm .project .classpath
$ mvn eclipse:eclipse -DdownloadSources=true
}}
** テストケースの作成 [#ac895904]
準備ができましたので、テストケースを作成しましょう。
Springでは、
- AbstractDependencyInjectionSpringContextTests
- AbstractTransactionalSpringContextTests
- AbstractTransactionalDataSourceSpringContextTests
のテストケースを提供しています。今回はトランザクションの...
*** クラスの単体テスト [#z4c124fd]
最初にStubRecipeManagerのfindByIdの単体テスト作ってみまし...
src/test/java以下にSampleTestCase.java として以下のファイ...
#pre{{
import org.springframework.showcase.coverc.domain.Recipe;
import org.springframework.showcase.coverc.service.StubRe...
import org.springframework.test.AbstractDependencyInjecti...
public class SampleTestCase extends AbstractDependencyInj...
public void testFindById() {
StubRecipeManager manager = new StubRecipeManager();
Recipe recipe = manager.findById(1L);
assertNotNull(recipe);
assertEquals("Goats Cheese with beetroot sauce", recipe...
}
}
}}
JUnitでは、個々のテストは、''public void testXXXX()'のよ...
testFindByIdでの処理は以下の通りです。
- StubRecipeManagerのインスタンスmanagerを作成する
- managerにfindByIdメソッドでIdが1のrecipeを取り出す。
- recipeがNullでなく、nameが"Goats Cheese with beetroot s...
*** 単体テストの実行 [#h2de5ac9]
テストケースができたので、実際にJunitを使った単体テストを...
mavenでは、単体テストを簡単に実行するために、''test''とい...
コマンドラインから以下のコマンドを入力すると、自動的にSma...
#pre{{
$ mvn test
Listening for transport dt_socket at address: 8000
[INFO] Scanning for projects...
[INFO] --------------------------------------------------...
[INFO] Building mvc-convention Maven Webapp
[INFO] task-segment: [test]
[INFO] --------------------------------------------------...
-- 途中省略
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running SampleTestCase
2008/03/23 16:47:00 org.springframework.test.AbstractDepe...
情報: ApplicationContext has not been configured for test...
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time el...
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
}}
テストで1つの情報(Macでは???と文字化けします)がでます。
- SpringのBean定義が設定されていない旨の警告
テストの結果は、Results:以下の
#pre{{
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
}}
で確認します。
1個のテストを実行し、失敗、エラー、スキップがすべて無いこ...
もちろん、Eclipseを使って単体テストを実行することも可能で...
- Runメニューから"Run..."を選択する
- RunダイアログのJUnitを選択し、右クリックでNewを選択する
- RunダイアログのRunボタンを押す
とJUnitが実行します。
** Mockオブジェクトを使った結合テスト [#yb48f909]
次に、SpringのBean定義ファイルを使った結合テストの方法に...
*** リソースのコピー [#j8ca0af0]
最初にBean定義ファイルをテスト用リソースファイルにコピー...
- src/test/resources/にWEB-INFディレクトリを追加します
- src/main/webapp/WEB-INF以下のapplicationContext.xml, co...
coverc-servlet.xmlのSwitchBoardContrllerの定義に以下のよ...
#pre{{
<bean id="switchBoadController" class="org.springfram...
parent="baseRecipeController"/>
}}
*** テストケースでのBean定義ファイル設定方法 [#l3e8c69b]
テストケースで使用するSpringのBean定義ファイルは、getConf...
する約束になっています。
getConfigLocationsは以下のように定義します。
#pre{{
public String[] getConfigLocations() {
return new String[] {
"WEB-INF/applicationContext.xml",
"WEB-INF/coverc-servlet.xml"
};
}
}}
*** テストメソッドの追加 [#l054839d]
Mockオブジェクトを使った結合テストの例を以下に示します。
- MockHttpServletRequestを使ってMockのHTTPServletRequest...
- SpringのBean定義に従って生成されたApplicationContextか...
- switchBoadControllerにHTTPServletRequestを処理(handleR...
- hadleRequestで返されたModelAndViewのviewNameがnullであ...
- ModelAndViewのmodelからrecipeListを取り出します。
- recipeListのサイズが3であることを確認します
- 最初のRecipeのNameが”Goats Cheese with beetroot sauce”...
#pre{{
public void testIntegrated() throws Exception {
MockHttpServletRequest req = new MockHttpServletRequest...
SwitchBoardController controller =
(SwitchBoardController)getApplicationContext().getBea...
ModelAndView mv = controller.handleRequest(req,new Mock...
assertNull(mv.getViewName());
List list = (List)mv.getModel().get("recipeList");
assertEquals(3, list.size());
Recipe recipe = (Recipe)list.get(0);
assertEquals("Goats Cheese with beetroot sauce", recipe...
}
}}
*** 結合テストの実行 [#he6d6a2c]
Bean定義ファイルを追加し、Mockオブジェクトを使った結合テ...
出力されます。
#pre{{
$ mvn test
-- 途中省略
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running SampleTestCase
2008/03/23 18:28:31 org.springframework.test.AbstractSing...
-- 途中省略
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time el...
Results :
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
}}
2個のテストメソッドが正常に動作していることが確認できまし...
#ref(SampleTestCase.java);
** コメント [#o04e4088]
この記事は、
#vote(おもしろかった[7],そうでもない[3],わかりずらい[11])
皆様のご意見、ご希望をお待ちしております。
#comment_kcaptcha
ページ名:
SmartDoc