第6章 Struts 2のJunitテスト
Struts 2のJunitテスト
homepage
# **Struts 2のJunitテスト** *** ## **1.Junitテストケース** まず、「test」パッケージを作って、「LoginActionTest.java」を作ります。 <br> **(1)** ウィザードに「junit」を入力して、Junitを探します。 <!--graph6-1.png--> <a href="https://i.loli.net/2019/07/01/5d19b5993ec2754769.jpg" target="_blank"> <img src="https://i.loli.net/2019/07/01/5d19b5993ec2754769.jpg" width=40%></a> <br><br> **(2)** 「Junitテストケース」を選んで、次に行きます。 <!--graph6-2.png--> <a href="https://i.loli.net/2019/07/01/5d19b598e655023186.jpg" target="_blank"> <img src="https://i.loli.net/2019/07/01/5d19b598e655023186.jpg" width=40%></a> <br><br> **(3)** 「名前」に「LoginActionTest」を入力して、完了します。 <!--graph6-3.png--> <a href="https://i.loli.net/2019/07/01/5d19b59939c8258167.jpg" target="_blank"> <img src="https://i.loli.net/2019/07/01/5d19b59939c8258167.jpg" width=40%></a> <br><br> **(4)** 下のようなウインドウ出れば、「次のアクションを実行」を選んで、「OK」を押すと、完了しました。 <!--graph6-4.png--> <a href="https://i.loli.net/2019/07/01/5d19b59934cf488746.jpg" target="_blank"> <img src="https://i.loli.net/2019/07/01/5d19b59934cf488746.jpg" width=40%></a> <br> <br> ## **2.テストの内容** 下記のソースを「LoginActionTest.java」に書きます。 それは、「ユーザーID」と「パスワード」に何も入力しないの場合、出ていくのエラーメッセージはバリデーションから取得したエラーメッセージと同じかどうかをテストします。 <br> **・LoginActionTest.java** ``` package test; import java.util.Collection; import java.util.List; import org.apache.struts2.StrutsTestCase; import org.junit.Test; import com.opensymphony.xwork2.ActionProxy; import action.LoginAction; public class LoginActionTest extends StrutsTestCase { ActionProxy proxy; String result; LoginAction loginAction; @Test public void testErrorMessagePattern1() throws Exception{ //StrutsTestCaseを継承する //requestのParameterを設定する request.setParameter("accountId", ""); request.setParameter("password", ""); //"/login"のアクションを取得する proxy = getActionProxy("/login"); assertNotNull(proxy); loginAction = (LoginAction) proxy.getAction(); assertNotNull(loginAction); //LoginActionアクションのexecuteメソッドを実行する result = proxy.execute(); /** * テストデータから取得したエラーメッセージ */ List<String> msgGetByUserID =loginAction.getFieldErrors().get("accountId"); for(String s :msgGetByUserID){ System.out.println(s); } /** * validationから取得したエラーメッセージ */ String msgFromValidationUserID = loginAction.getText("msg_company_001",new String[]{loginAction.getText("userid")}); System.out.println(msgFromValidationUserID); /** * テストデータから取得したエラーメッセージ */ List<String> msgGetByPSW =loginAction.getFieldErrors().get("password"); for(String s :msgGetByPSW){ System.out.println(s); } /** * validationから取得したエラーメッセージ */ String msgFromValidationPSW = loginAction.getText("msg_company_001",new String[]{loginAction.getText("password")}); System.out.println(msgFromValidationPSW); assertTrue(msgGetByUserID.contains(msgFromValidationUserID) && msgGetByPSW.contains(msgFromValidationPSW)); assertEquals("input", result); } } ``` <br><br> ## **3.テストの結果** **(1)** コンソールに出力した内容 <!--graph6-5.png--> <a href="https://i.loli.net/2019/07/01/5d19ba5f3b3d363499.jpg" target="_blank"> <img src="https://i.loli.net/2019/07/01/5d19ba5f3b3d363499.jpg" width=40%></a> <br> **(2)** 出力したエラーメッセージはバリデーションから取得したエラーメッセージと同じですので、下記のようなウインドウであること。 <!--graph6-6.png--> <a href="https://i.loli.net/2019/07/01/5d19ba5f79e4e33043.jpg" target="_blank"> <img src="https://i.loli.net/2019/07/01/5d19ba5f79e4e33043.jpg" width=40%></a> <br> **(3)** 失敗した場合は、例えば出ていったのエラーメッセージはバリデーションから取得したエラーメッセージと異なる場合は、「エラー」を出ます。 <!--graph6-7.png--> <a href="https://i.loli.net/2019/07/01/5d19bbcd0a8fd76640.jpg" target="_blank"> <img src="https://i.loli.net/2019/07/01/5d19bbcd0a8fd76640.jpg" width=40%></a> <br><br> ## **4.その他のテスト** **(1)** パスワードを入力しない場合 ``` @Test public void testErrorMessagePattern2() throws Exception { request.setParameter("accountId", "111@softusing.com"); request.setParameter("password", ""); proxy=getActionProxy("/login"); assertNotNull(proxy); loginAction=(LoginAction)proxy.getAction(); assertNotNull(loginAction); result=proxy.execute(); List<String> msgGetByPSW =loginAction.getFieldErrors().get("password"); for(String s :msgGetByPSW){ System.out.println(s); } String msgFromValidationPSW = loginAction.getText("msg_company_001",new String[]{loginAction.getText("password")}); System.out.println(msgFromValidationPSW); assertTrue(msgGetByPSW.contains(msgFromValidationPSW)); assertEquals("input", result); } ``` **(2)** ユーザーIDフォーマットがメールアドレスではない場合 ``` @Test public void testErrorMessagePattern3() throws Exception { request.setParameter("accountId", "aaa@"); request.setParameter("password", "000001"); proxy=getActionProxy("/login"); assertNotNull(proxy); loginAction=(LoginAction)proxy.getAction(); assertNotNull(loginAction); result=proxy.execute(); List<String> msgGetByUserID=loginAction.getFieldErrors().get("accountId"); for(String s:msgGetByUserID) { System.out.println(s); } String msgFromValidationUserID=loginAction.getText("msg_company_006",new String[] {loginAction.getText("accountId")}); System.out.println(msgFromValidationUserID); assertTrue(msgGetByUserID.contains(msgFromValidationUserID)); assertEquals("input",result); } ``` **(3)** パスワードは6桁数字ではない場合 ``` @Test public void testErrorMessgaePattern4() throws Exception { request.setParameter("accountId", "111@softusing.com"); request.setParameter("password", "0000-"); proxy=getActionProxy("/login"); assertNotNull(proxy); loginAction=(LoginAction)proxy.getAction(); assertNotNull(loginAction); result=proxy.execute(); List<String> msgGetByPSW=loginAction.getFieldErrors().get("password"); for(String s:msgGetByPSW) { System.out.println(s); } String msgFromValidationPSW=loginAction.getText("msg_company_002",new String[] {loginAction.getText("minlen")}); System.out.println(msgFromValidationPSW); assertTrue(msgGetByPSW.contains(msgFromValidationPSW)); assertEquals("input",result); } ``` **(4)** パスワードは不正の場合 ``` @Test public void testErrorMessgaePattern5() throws Exception { request.setParameter("accountId", "111@softusing.com"); request.setParameter("password", "111111"); proxy=getActionProxy("/login"); assertNotNull(proxy); loginAction=(LoginAction)proxy.getAction(); assertNotNull(loginAction); result=proxy.execute(); Collection<String> msgGetByPSW=loginAction.getActionMessages(); for(String s:msgGetByPSW) { System.out.println(s); } String msgFromValidationPSW=loginAction.getText("msg_company_004"); System.out.println(msgFromValidationPSW); assertTrue(msgGetByPSW.contains(msgFromValidationPSW)); assertEquals("error",result); } ``` **(5)** ユーザーID存在しない場合 ``` @Test public void testErrorMessgaePattern6() throws Exception { request.setParameter("accountId", "000@softusing.com"); request.setParameter("password", "000001"); proxy=getActionProxy("/login"); assertNotNull(proxy); loginAction=(LoginAction)proxy.getAction(); assertNotNull(loginAction); result=proxy.execute(); Collection<String> msgGetByUserId=loginAction.getActionMessages(); for(String s:msgGetByUserId) { System.out.println(s); } String msgFromValidationUserId=loginAction.getText("msg_company_003"); System.out.println(msgFromValidationUserId); assertTrue(msgGetByUserId.contains(msgFromValidationUserId)); assertEquals("error",result); } ``` **(6)** ユーザーIDとパスワードも正しい場合 ``` @Test public void testErrorMessgaePattern7() throws Exception { request.setParameter("accountId", "111@softusing.com"); request.setParameter("password", "000001"); proxy=getActionProxy("/login"); assertNotNull(proxy); loginAction=(LoginAction)proxy.getAction(); assertNotNull(loginAction); result=proxy.execute(); assertEquals("success",result); } ```
content
戻る