sample codes for HttpUnit

to instantiate a request by indicating a URL;
-new GetMethodWebRequest(String)

to set local infomation;
-WebRequest#setHeaderField("accept-language","ja")

to get the response(WebResponse) for the WebRequest;
(when the req is redirect, the res gonna be the result of redirect)
-WebConversation#getResponse(WebRequest)

to get the HTML pert of the WebResponse;
-WebResponse#getText()

to get the title of the HTML;
-WebResponse#getTitle(String)

to get the form(WebForm);
-WebResponse#getForms()[1] // for getting the 2nd form.

to get the next screen(WebResponse) by submitting;
-WebFrom#submit()

to get the result screen via a webConversation,
such as WebConversation#getResponse(WebRequest) or WebForm#submit();
-WebConversation#getCurrentPage()

to get or set input values of textBox or other requestParameters;
-WebForm#getParameter(String, String)
-WebForm#setParameter(String, String)

to get or set status of checkBox;
-WebForm#getParameterValue(String) // if there were no check, it returns null.
-WebForm#setCheckbox(String, boolean)

cactusStrutsTestCase

□overview
cactusStrutsTestCase
↓↑
HTTP conversation
↓↑
J2EE
↓↑
proxyRedirector
↓↑
cactusStrutsTestCase
↓↑
struts
↓↑
any classes like hogeAction

□to use cactusStrutsTestCase
1.
first, create a testcase extending CactusStrutsTestCase.
2.
delete "setContextDirectory()" if there it is.
the "setContextDirecty()" if for mockStrutsTestCase.
and,
it is no longer cuz contextDirectry gonna be set in server (tomcat/conf/server.xml) in incontainerTest.
3.
delete CONTEXT_ROOT definition as well cuz just no longer.
4.
make sure there is the jndi.datasource difinition in webapp/web-inf/src/system.properties.
ex.
jndi.datasource=java:comp/env/jdbc/seminardb
it is needed for tomcat to get the datasource.
5.
delete the code setting a MockDataSource into ds, the field of ConnectionUtil.
when to use mockStrutsTestCase, tom was not working. thus, the code above was needed to get a datasource.
and,
when to use cactusStrutsTestCase, tom gonna working. so the code is no longer. and by that, the condition, incontainerTest, gonna be satisfied.

sample codes of MockStrutsTestCase

to set a contextRoot (only when to use MockStrutsTestCase);
-setCotectDirectory(File)
-use cons.CONTEXT_ROOT for contextRoot path.

to indicate locale of struts-stg file;
-setConfigFile("WEB-INF/struts-config.xml")

to indicate a actionPath;
-setRequestPathInfo("/hoge")

to get a sessionObject;
-getSession()

to execute;
-actionPerform()

to verify forward or forwardPath;
-verifyForward(String)
-verifyForwardPath(String)

UT for jsp

when to test JSP,
assume the target JSP as a class of serverSide.
and create a testcase class extending org.apache.cactus.JspTestCase.
the test method gonna be following.
pageContext.include([JSPnoコンテキスト相対パス])
then,
set a redirector as following.
<'servlet>
<'servlet-name>JspRedirector<'/servlet-name>
<'jsp-file>/jspRedirector.jsp<'/jsp-file>
<'/servlet>
<'servlet-mapping>
<'servlet-name>JspRedirector<'/servlet-name>
<'url-pattern>/JspRedirector<'/url-pattern>
<'/servlet-mapping>

UT procedure for filter class

when to test filter class,
you need to make a class extending org.apache.cactus.FilterTestCase.
then,
complete web.xml as following.
<'filter>
<'filter-name>FilterRedirector<'/filter-name>
<'filter-class>org.apache.cactus.server.FilterTestRedirector<'/filter-class>
<'/filter>
<'filter-mapping>
<'filter-name>FilterRedirector<'/filter-name>
<'url-pattern>/FilterRedirector<'/url-pattern>
<'/filter-mapping>

UT procedure for filter class

when to test filter class,
you need to make a class extending org.apache.cactus.FilterTestCase.
then,
complete web.xml as following.
<'filter>
<'filter-name>FilterRedirector<'/filter-name>
<'filter-class>org.apache.cactus.server.FilterTestRedirector<'/filter-class>
<'/filter>
<'filter-mapping>
<'filter-name>FilterRedirector<'/filter-name>
<'url-pattern>/FilterRedirector<'/url-pattern>
<'/filter-mapping>

cactus#servletRedirector

□basic cactus operation flow
1. testcase class of clientSide
2. proxyRedirector
3. testcase class of serverSide
4. any serverSide class

□cactus#servletRedirector
as a internal step to use cacus,
you need to map cactus#servletRedirector as a servlet.
sample @web.xml
<'servlet>
<'servlet-name>ServletRedirector<'/servlet-name>
<'servlet-class>org.apache.cactus.server.ServletTestRedirector<'/servlet-class>
<'/servlet>
<'servlet-mapping>
<'servlet-name>ServletRedirector<'/servlet-name>
<'url-pattern>/ServletRedirector<'/url-pattern>
<'/servlet-mapping>

jars for cactus

prepare following for using cactus.
aspectjrt-1.1.1.jar
cactus-1.6.1.jar
junit.jar
(commons-logging.jar)

UT

□three types for testing
・whiteBox
・grayBox
・blackBox

□whiteBox
プログラムの内部構造を参照し、そのロジックや制御の流れを調べるテスト手法
few types for whiteBox-test
・制御パステスト

□制御パステスト
プログラムの処理経路(開始から終了に至る命令の列)をテストする手法
depends on テストする処理経路の網羅度合い
・C0網羅(命令網羅)
・C1網羅(分岐網羅)
・C2網羅(条件網羅)

□C0網羅
処理経路を構成する全ての命令を最低1回実行するテスト方法

□C1網羅
条件分岐の真と偽の組み合わせをテストする方法

□C2網羅
条件分岐の真と偽の全ての組み合わせをテストする方法

□網羅率(カバレッジ)
全処理経路数のうち、実際にテストした処理経路数の割合
ex
全命令数が100で、このうち50をテストした場合、C0網羅率は50%となる。

□blackBox
テスト対象の仕様や使い方に着眼して、入力値と出力結果の対応関係から、関数やメソッドの振る舞いを調べるテスト
結合テスト以降のテストで用いられる手法
few types for blackBox-test
・同値分割
・境界値分析
・原因結果グラフ・テスト

□同値分割
入力値を有効値の集合と無効値の集合に分け、各集合から代表値を選んでテストする手法

□境界値分析
有効値と無効値の境界地とのその前後の値を選んでテストする手法

□grayBox
メモリへの適当なアクセスやメモリ領域の確保・開放、
プログラムの命令が実行されたかをテスト
テスト対象プログラム内にチェック用のコード(プローブ)を
埋め込んで命令の実行を確保したり、
テスト実施中のメモリ状況を計測してテストする手法

□結合テストの手法
two types
・増加テスト(インクメンタル・テスト)
・一斉テスト(ビックバン・テスト)

□増加テスト
最初にテストしたモジュールに徐々に他のモジュールを結合しながらテストを進める方法。
depends on 結合方法
・トップダウンテスト
 上位モジュールからスタート using stub.
・ボトムアップテスト
 下位モジュールからスタート using driver.
・サンドイッチテスト
 中間のモジュールからスタート

□一斉テスト
全てのモジュールを結合してからテストを実施する方法

ref:
http://itpro.nikkeibp.co.jp/article/COLUMN/20051102/223934/?ST=itarchitect

reflectionAPI

□for private field
ClassA obj = new ClassA();
Field f = obj.getClass().getDeclaredField("privateFieldName");
f.setAccessible(true);
f.set(obj, "testValueForPrivateField");
[Type result = (Type)f.get(obj);]

□for private method
Field→Method
getDeclaredField→getDeclaredMethod
set/get→invoke

□attention
if the field is 静的field, use "null" for argument of set/get-method instead of "obj".
consider性能, in usually programming, avoid to use reflection gonna be better.

Top|Next »

HOME

09 | 2008/10 | 11
- - - 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 -

QR

name:
mail:
title:
body:

N/A