Webworkでのアクションのユニットテスト
ユニットテストフレームワークJUnitを使用してアクションをユニットテストする
example:
この方法について説明します.
1、対象paramsは要求パラメータのMapを表し、登録ユーザの情報が設定されている.paramCtxはもちろんActionContextコンテキストのコンテナであり、リクエストパラメータを配置するオブジェクトparamsが保存されています.
2、私たちのActionProxyを作成して、それが伝わるパラメータはあります:“example”-このActionのネーミングスペース、“register”-Actionの対応する名前、paramCtx-Actinコンテキストの中のオブジェクトを保存して、実行してそれを返した値を“success”と比較して、Actionが正しく実行できるかどうかをテストします.注意:proxy.setExecuteResult(false);,我々はユニットテストなので、Action実行が完了すればよいので、結果応答の操作を呼び出す必要がないので、実行結果を「false」に設定します.
3、Actionが正しく実行された後、現在のActionのフィールドのデータが私たちの予想通りに正しく設定されているかどうかをテストすることもできます.ActionProxyオブジェクトから実行するAction,すなわちRegisterActionオブジェクトを取得し,そのUserモデルを取得し,そのデータを前に設定したパラメータのデータと比較し,我々が想定した値に等しいか否かを判断する.
example:
public void testAction() throws Exception{
Map params = new HashMap();
params.put("user.username","tangyi");
params.put("user.password","123123");
Map paramCtx= new HashMap();
paramCtx.put(ActionContext.PARAMETERS,params);
//"example" Action namespace;"register" Action name;
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy("example", "register", paramCtx);
proxy.setExecuteResult(false);
assertEquals(proxy.execute(),"success");
RegisterAction action = (RegisterAction) proxy.getAction();
assertEquals(action.getUser().getUsername(),"tangyi");
assertEquals(action.getUser().getPassword(),123123);
}
この方法について説明します.
1、対象paramsは要求パラメータのMapを表し、登録ユーザの情報が設定されている.paramCtxはもちろんActionContextコンテキストのコンテナであり、リクエストパラメータを配置するオブジェクトparamsが保存されています.
2、私たちのActionProxyを作成して、それが伝わるパラメータはあります:“example”-このActionのネーミングスペース、“register”-Actionの対応する名前、paramCtx-Actinコンテキストの中のオブジェクトを保存して、実行してそれを返した値を“success”と比較して、Actionが正しく実行できるかどうかをテストします.注意:proxy.setExecuteResult(false);,我々はユニットテストなので、Action実行が完了すればよいので、結果応答の操作を呼び出す必要がないので、実行結果を「false」に設定します.
3、Actionが正しく実行された後、現在のActionのフィールドのデータが私たちの予想通りに正しく設定されているかどうかをテストすることもできます.ActionProxyオブジェクトから実行するAction,すなわちRegisterActionオブジェクトを取得し,そのUserモデルを取得し,そのデータを前に設定したパラメータのデータと比較し,我々が想定した値に等しいか否かを判断する.