S2AbstractServiceにない処理を作成して、テストを実行する

次にS2AbstractServiceでは用意されていなく、独自に作成したテストケースを書きます。


まずはInsertBatchです。要は一括処理等に使用する事が想定されます。

ServiceClass

public int insertAll(List entities) {
return this.jdbcManager.insertBatch(entities).execute();
}

テストクラス

public void testInsertAllTx() throws Exception {
List entities = new ArrayList();
OrderVoucher entity = new OrderVoucher();
entity.id = Long.parseLong("10");
entity.accountId = "11";
entity.accountName = "222";
entities.add(entity);

entity = new OrderVoucher();
entity.id = Long.parseLong("10");
entity.accountId = "22";
entity.accountName = "333";
entities.add(entity);

int expecteds = orderVoucherService.insertAll(entities);
assertEquals(expecteds.length, 2);
}

これでいけます。戻り値の所がInt型の配列になっていますね。
本家サイトを確認した所、Update・Deleteも同じように記述できます。


また、S2JDBC-Genではサービスクラスにメソッドを1つ自動で作成してくれます。
それはプライマリーキーを昇順に設定した全件のEntityの取得です。

ServiceClass

public List findAllOrderById() {
return select().orderBy(asc(id())).getResultList();
}

上記はS2JDBC-Genが自動で作成しました。

テストクラス

public void testServiceFindAll() throws Exception {
List lists = orderVoucherService.findAllOrderById();
assertEquals(lists.size(), 1);
}

これでいけます。


直接SQLを書かざるを得ない場合もあるので、直接SQLを書いて見ます。

ServiceClass

public OrderVoucher findBySql(String sql, Long params) {
return this.jdbcManager.selectBySql(OrderVoucher.class, sql,
params).getSingleResult();
}

テストクラス

public void testSelectSql() throws Exception {
String sql = "SELECT ID, ACCOUNT_ID, ACCOUNT_NAME FROM
ORDER_VOUCHER WHERE ID = ?";
OrderVoucher expected = orderVoucherService.findBySql(sql, new
Long("6"));
assertEquals(expected.accountName, "8888");
}

大丈夫ですね。


Seasar2ですから、外だしSQLの可能性もありますから。

ServiceClass

public OrderVoucher findBySqlFile(Long params) {
return this.jdbcManager.selectBySqlFile(OrderVoucher.class,
"jp/globalsystems/businessflow/service/findBySqlFile.sql",
params).getSingleResult();
}

テストクラス

public void testSelectSqlFile() throws Exception {
OrderVoucher expected = orderVoucherService.findBySqlFile(new Long("6"));
assertEquals(expected.accountId, "666");
}

大丈夫です。このあたりは本家サイトをしっかり読むと、より詳細が記述されています。

さくさくできますね。。。