S2Junit4详解

http://hondou.homedns.org/pukiwiki/index.php?Seasar%20S2JUnit4

 

サンプルプロジェクトはこちら file S2JUnit4Exam.zip
(ビルドや実行には Eclipse + m2plugin が必要です)

はじめに

  • S2Junit4 は、Junit4 を拡張して作られた Seasar2 のためのテストフレームワーク
  • S2 のトランザクションに介入して、テストメソッドの終了後に Rollback する
    • 通常は
      BEGIN
      テスト対象の実行()
      COMMIT
    • テスト時は
      BEGIN
      テストデータの投入
      テスト対象の実行()
      照合
      ROLLBACK
  • dbunitと同じようなことができる
    • Excel ファイルに定義したテストデータを Database に投入できる
    • 実行後の Database の状態を Excel ファイルと照合する機能はついていないが、SQL 文の実行結果と Excel ファイルの照合を簡単にできるような機構が準備されている
  • Bean と Excel ファイルの照合が簡単にできる API が準備されている
  • テストデータを簡単に作れるように、Bean や Table の内容を Excel ファイルに出力する API が準備されている

S2Junit4 を使うために S2/S2DAO の他に必要なもの

pom.xml (依存ライブラリの追加)

  • S2DAO Tiger で必要とされているライブラリの他に、Apache Geronimo の JPA、JTA、EJB3.0 が必要
    • geronimo-jpa_3.0_spec
    • geronimo-jta_1.1_spec
    • geronimo-ejb_3.0_spec
  • また、S2JUnit4 では複数の DB Connection を使うので、組み込み運用の Derby では動かない。別途 Derby サーバーを起動して、ネットワーク経由で接続するようにしなければならない。
    • derbyclient
  • pom.xml <script src="http://hondou.homedns.org/pukiwiki/skin/code.js" type="text/javascript"></script>
    Everything is expanded. Everything is shortened.
      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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
     













































































































    <?xml version="1.0"
     encoding="UTF-8"
    ?><project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>S2DAOExam</groupId>
    <artifactId>S2DAOExam</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <description></description>
    <build>
    <finalName>S2DAOExam</finalName>
    <plugins>
    <plugin >
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
    <source>1.5</source>
    <target>1.5</target>
    <encoding>UTF-8</encoding>
    </configuration>
    </plugin >
    </plugins>
    </build>
    <repositories>
    <repository>
    <id >maven.seasar.org</id >
    <name >The Seasar Foundation Maven2 Repository</name >
    <url>http://maven.seasar.org/maven2</url>
    </repository>
    <repository>
    <id >maven2-repository.dev.java.net</id >
    <name >Java.net Maven 2 Repository</name >
    <url>http://download.java.net/maven/2</url>
    </repository>
    <repository>
    <id >maven-repository.dev.java.net</id >
    <name >Java.net Maven 1 Repository (legacy)</name >
    <url>http://download.java.net/maven/1</url>
    <layout>legacy</layout>
    </repository>
    </repositories>
    <dependencies>
    <dependency>
    <groupId>org.seasar.container</groupId>
    <artifactId>s2-tiger</artifactId>
    <version>2.4.32</version>
    </dependency>
    <dependency>
    <groupId>org.seasar.dao</groupId>
    <artifactId>s2-dao-tiger</artifactId>
    <version>1.0.49</version>
    </dependency>
    <dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <version>10.3.1.4</version>
    </dependency>
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.3</version>
    <scope >provided</scope >
    </dependency>
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.15</version>
    <exclusions>
    <exclusion>
    <artifactId>jmxtools</artifactId>
    <groupId>com.sun.jdmk</groupId>
    </exclusion>
    <exclusion>
    <artifactId>jmxri</artifactId>
    <groupId>com.sun.jmx</groupId>
    </exclusion>
    <exclusion>
    <artifactId>jms</artifactId>
    <groupId>javax.jms</groupId>
    </exclusion>
    </exclusions>
    </dependency>
    <dependency>
    <groupId>javax.transaction</groupId>
    <artifactId>transaction-api</artifactId>
    <version>1.1</version>
    </dependency>
    <dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.7.0</version>
    </dependency>
    <dependency>
    <groupId>org.apache.geronimo.specs</groupId>
    <artifactId>geronimo-jpa_3.0_spec</artifactId>
    <version>1.1</version>
    </dependency>
    <dependency>
    <groupId>org.apache.geronimo.specs</groupId>
    <artifactId>geronimo-jta_1.1_spec</artifactId>
    <version>1.1</version>
    </dependency>
    <dependency>
    <groupId>org.apache.geronimo.specs</groupId>
    <artifactId>geronimo-ejb_3.0_spec</artifactId>
    <version>1.0</version>
    </dependency>
    <dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derbyclient</artifactId>
    <version>10.3.1.4</version>
    </dependency>
    </dependencies>
    </project>

jdbc.dicon (DB接続の変更)

  • S2JUnit4では、同時に1接続しかできない 組み込み版 Derby は使えないので、Network Client にする
    Everything is expanded. Everything is shortened.
      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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
     






























































    <?xml version="1.0"
     encoding="UTF-8"
    ?>
    <!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN"
    "http://www.seasar.org/dtd/components24.dtd " >
    <components namespace="jdbc" >
    <include path="jta.dicon" />
    <include path="jdbc-extension.dicon" />

    <component class ="org.seasar.extension.jdbc.impl.BasicResultSetFactory" />
    <component class ="org.seasar.extension.jdbc.impl.ConfigurableStatementFactory" >
    <arg>
    <component class ="org.seasar.extension.jdbc.impl.BasicStatementFactory" />
    </arg>
    <property name ="fetchSize" >100</property >
    <!--
    <property name ="maxRows" >100</property >
    -->
    </component>
    <!-- ********** Derby Embedded **********
    <component name ="xaDataSource"
    class ="org.seasar.extension.dbcp.impl.XADataSourceImpl"
    instance="singleton"
    autoBinding="auto" >

    <property name ="driverClassName" >
    "org.apache.derby.jdbc.EmbeddedDriver"
    </property >
    <property name ="URL" >
    "jdbc:derby:/Users/atsushi/Documents/mydb/data;create=false"
    </property >
    </component>
    -->
    <!-- Derby Network Client -->
    <component name ="xaDataSource"
    class ="org.seasar.extension.dbcp.impl.XADataSourceImpl"
    instance="singleton"
    autoBinding="auto" >

    <property name ="driverClassName" >
    "org.apache.derby.jdbc.ClientDriver"
    </property >
    <property name ="URL" >
    "jdbc:derby://localhost:1527//Users/atsushi/Documents/mydb/data;create=false"
    </property >
    </component>

    <component name ="connectionPool"
    class ="org.seasar.extension.dbcp.impl.ConnectionPoolImpl"
    instance="singleton"
    autoBinding="auto" >
    <property name ="timeout" >600</property >
    <!-- ********** for Embedded Derby **********
    <property name ="maxPoolSize" >0</property >
    -->
    <property name ="maxPoolSize" >10</property >
    <property name ="allowLocalTx" >true</property >
    <destroyMethod name ="close" />
    </component>

    <component name ="DataSource"
    class ="org.seasar.extension.dbcp.impl.DataSourceImpl"
    instance="singleton"
    autoBinding="auto" />
    </components>

テストのための資産とその内容

  • S2Junit4の設定ファイル
    • s2junit4.dicon
  • テストクラス
    • com.snail.exam.s2dao.test.CustomerDaoTest?
  • テスト固有の設定ファイル
    • テストクラス名 + ".dicon"
      • 命名規約は s2jinut4.dicon で定義されている
      • 変更できるけど、その必要はないかも
    • com.snail.exam.s2dao.test.CustomerDaoTest? .dicon
    • (src/test/resources に作る。src/test/java に作ると変更が直ちに反映されない)
  • テストデータ
    • Excelファイル
      • 命名規約は s2jinut4.dicon で定義されている
      • 変更できるけど、その必要はないかも
      • 事前データ   : テストクラス名_テストメソッド名.xls
      • 実行結果データ : テストクラス名_テストメソッド名_Expected.xls
    • com.snail.exam.s2dao.test.CustomerDaoTest_testFindByAddress? .xls
    • com.snail.exam.s2dao.test.CustomerDaoTest_testFindByAddress_Expected.xls
    • (src/test/resources に作る。src/test/java に作ると変更が直ちに反映されない)
  • テスト後のDBの状態検証
    • S2JUnit4では、特にサポートされていない
    • Table と Excel ファイルを手軽に比較する API が準備されている
    • ここでは、com.snail.exam.s2dao.test.CustomerDaoTest_testFindByAddress_SIDE_EFFECTS.xls を準備した

テスト対象

CustomerDao? #findByAddress? () をテストする。(実際には Service や Model をテストするが、話がややこしくなるので今回は Dao のメソッドをテストする)

テストデータ

  • MS Office が買えない貧乏人でも、Open Office で、「Excel 97/2000/XP 形式」で保存すれば OK (iWork の Numbers ではダメだった)
  • 事前データ(テスト実行前のDBの状態)
    • com.snail.exam.s2dao.test.CustomerDaoTest_testFindByAddress? .xls
      dbInit1.png dbInit2.png
    • Sheet名に Table名を設定する
    • 1行目には、列名を設定する(テストに必要なければ、全ての列を定義する必要はない)
    • 後ろにあるシートから読み込まれるようだ(FKを設定してるばあいには、親Tableから上書きするようにする)
    • 余分な Sheet は残さない(←これ重要! )
  • 実行結果データ(テスト対象の実行結果)
    • com.snail.exam.s2dao.test.CustomerDaoTest_testFindByAddress_Expected.xls
    • Sheet名は任意
    • 1行目には、列名を設定する
    • Excel に記述された列のみ照合されて、一致すれば合格になる
  • 事後データ(テスト実行後のDBの状態)
    • com.snail.exam.s2dao.test.CustomerDaoTest_testFindByAddress_SIDE_EFFECTS.xls
    • Sheet名は任意
    • 1行目には、列名を設定する
    • Excel に記述された列のみ照合されて、一致すれば合格になる

テストクラス

テストメソッドの命名規約や起動順序の詳細については、下のサンプルに目を通してから、Seasar Projectのサイト( http://s2container.seasar.org/2.4/ja/S2JUnit4.html )を読むとわかりやすい

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值