jp.terasoluna.fw.ex.unit.testcase
クラス DaoTestCase
java.lang.Object
junit.framework.Assert
junit.framework.TestCase
org.springframework.test.ConditionalTestCase
org.springframework.test.AbstractSpringContextTests
org.springframework.test.AbstractSingleSpringContextTests
org.springframework.test.AbstractDependencyInjectionSpringContextTests
org.springframework.test.AbstractTransactionalSpringContextTests
org.springframework.test.AbstractTransactionalDataSourceSpringContextTests
jp.terasoluna.fw.ex.unit.testcase.DaoTestCase
- すべての実装されたインタフェース:
- junit.framework.Test
- 直系の既知のサブクラス:
- JndiActivatedDaoTestCase
public abstract class DaoTestCase
- extends org.springframework.test.AbstractTransactionalDataSourceSpringContextTests
DAO実行試験支援テストケース。
本試験では前処理中にSpringのApplicationContext
を作成します。
作成時には次の設定ファイルを読み込みます。
設定ファイル |
デフォルト値 |
変更ポイント |
備考 |
共通Bean定義ファイル |
WebContext/WEB-INF/applicationContext.xml |
terasoluna-unit-override.propertiesの以下にキーに対する値を設定することで、変更可能です。
WebContext → webapp.pathキー
WEB-INF → webinf.dirキー
applicationContext.xml → applicationcontext.fileキー
|
ファイルが存在しない場合、無視します。コンストラクタにてsetLoadDefaultConfig(boolean) でfalseを設定した場合またはisLoadDefaultConfig() がfalseを返すようにオーバライドした場合、読み込みません。 |
モジュールBean定義ファイル |
WebContext/WEB-INF/moduleContext.xml |
terasoluna-unit-override.propertiesの以下にキーに対する値を設定することで、変更可能です。
WebContext → webapp.pathキー
WEB-INF → webinf.dirキー
moduleContext.xml → modulecontext.fileキー
|
ファイルが存在しない場合、無視します。コンストラクタにてsetLoadDefaultConfig(boolean) でfalseを設定した場合またはisLoadDefaultConfig() がfalseを返すようにオーバライドした場合、読み込みません。 |
試験毎Bean定義ファイル |
なし |
addConfigLocations(List) メソッドをオーバーライドして、引数のBean定義ファイルパスリストに追加したいファイルのパスをaddしてください。 |
ファイルが存在しない場合、 org.springframework.beans.factory.BeanDefinitionStoreException をスローします。 |
Bean定義ファイル中にはDataSource
の定義が必要であり、DBを接続した試験を行うことができます。
(ただし、DB環境を用意する必要があります。)
読み込んだBean定義ファイルに定義したBeanを試験コード中で使用する場合、以下の二種類のBean取得方法があります。
1. BeanFactory.getBean(String)
によって取得する方法
2. setterを定義してautowire(by type)する方法
以下のような定義がある場合を例にとって説明します。
WEB-INF/foo/fooContext.xml
<!-- 前後略 -->
<bean id="foo" class="com.example.Foo" />
<bean name="/foo" class="com.example.FooBLogic" />
1. BeanFactory.getBean(String)
によって取得する方法
public FooTest extends DaoTestCase
{
public void testDoSomething() throws Exception {
Foo foo = getBean("foo");
// あるいは Foo foo = (Foo) applicationContext.getBean("foo");
int result = foo.doSomething();
assertEquals(1, result);
}
}
public FooBLogicTest extends DaoTestCase
{
public void testFoo() throws Exception {
FooBLogic foo = getBean("/foo");
// あるいは FooBLogic foo = (FooBLogic) applicationContext.getBean("/foo");
BLogicResult result = foo.execute(null);
AssertUtils
.assertBLogicSuccess(result);
}
}
取得するBeanのインターフェイスにAOP処理を設定している場合は、変数の型をAOP対象のインターフェイスにする必要があります。
2. setterを定義してautowire(by type)する方法
取得したいBeanのセッターを定義してください。
public FooTest extends DaoTestCase
{
private Foo foo;
public void setFoo(Foo foo) {
this.foo = foo;
}
public void testDoSomething() throws Exception {
int result = foo.doSomething();
assertEquals(1, result);
}
}
public FooBLogicTest extends DaoTestCase
{
private FooBLogic foo;
public void setFoo(FooBLogic foo) {
this.foo = foo;
}
public void testFoo() throws Exception {
BLogicResult result = foo.execute(null);
AssertUtils
.assertBLogicSuccess(result);
}
}
<strong>この方法は以下の場合には利用できません。</strong>
・Bean定義ファイル中に同じ型のBeanが複数定義されている場合
・AOP処理によりApplicationContext
からProxy
実装クラスとして取得される場合
本クラスを継承したテストは以下の様に処理が行われる
- 前処理
-- WEBAPディレクトリ(コンテキストルート)のクラスパスへの追加(TestCaseUtils.getConfigLocationsForDaoTestCase(String[])
)
-- Bean定義ファイル読み込み前処理(beforeGetConfig()
で実装可)
-- 読み込むBean定義ファイル追加(addConfigLocations(List)
で実装可)
-- ApplicationContext
作成
-- トランザクション開始前前処理(AbstractTransactionalSpringContextTests.onSetUpBeforeTransaction()
で実装可)
-- トランザクション開始後前処理(AbstractTransactionalSpringContextTests.onSetUpInTransaction()
で実装可)
- 試験実行
- 後処理
-- トランザクション終了前後処理(AbstractTransactionalSpringContextTests.onTearDownInTransaction()
)で実装可
-- トランザクション終了後後処理(AbstractTransactionalSpringContextTests.onTearDownAfterTransaction()
で実装可)
クラス org.springframework.test.AbstractTransactionalDataSourceSpringContextTests から継承されたフィールド |
jdbcTemplate |
クラス org.springframework.test.AbstractTransactionalSpringContextTests から継承されたフィールド |
transactionDefinition, transactionManager, transactionStatus |
クラス org.springframework.test.AbstractDependencyInjectionSpringContextTests から継承されたフィールド |
AUTOWIRE_BY_NAME, AUTOWIRE_BY_TYPE, AUTOWIRE_NO |
クラス org.springframework.test.AbstractSingleSpringContextTests から継承されたフィールド |
applicationContext |
クラス org.springframework.test.ConditionalTestCase から継承されたフィールド |
logger |
メソッドの概要 |
protected void |
addConfigLocations(List<String> configLocations)
読み込むBean定義ファイルパスを追加します。 |
int[] |
batchUpdate(String... sqls)
|
protected void |
beforeGetConfig()
Bean定義ファイルパス取得前処理。 |
String |
createInsertSql(Class<?> clazz)
|
String |
createInsertSql(String tableName,
Class<?> clazz)
|
String |
createInsertSql(String tableName,
String[] fieldNames)
|
String |
createSelectSql(Class<?> clazz)
|
String |
createSelectSql(String tableName,
Class<?> clazz)
|
String |
createSelectSql(String tableName,
String[] fieldNames)
|
protected void |
deleteFromTable(String name)
|
void |
execute(String sql)
|
|
getBean(String name)
Beanを取得します。 |
protected String[] |
getConfigLocations()
|
protected String |
getConfigPath()
|
boolean |
isLoadDefaultConfig()
デフォルトのBean定義ファイルを読み込むかどうかを取得します |
protected void |
onTearDown()
|
int |
queryForInt(String sql)
|
int |
queryForInt(String sql,
Object[] args)
|
long |
queryForLong(String sql)
|
long |
queryForLong(String sql,
Object[] args)
|
|
queryForObject(String sql,
Class<T> requiredType)
|
|
queryForObject(String sql,
Object[] args,
Class<T> requiredType)
|
Map<String,?> |
queryForRowMap(String sql)
|
Map<String,?> |
queryForRowMap(String sql,
Object[] args)
|
List<Map<String,?>> |
queryForRowMapList(Class<?> clazz)
|
List<Map<String,?>> |
queryForRowMapList(String sql)
|
List<Map<String,?>> |
queryForRowMapList(String sql,
Object[] args)
|
|
queryForRowObject(String sql,
Class<T> clazz)
|
|
queryForRowObject(String sql,
Object[] args,
Class<T> clazz)
|
|
queryForRowObjectList(Class<T> clazz)
|
|
queryForRowObjectList(String sql,
Class<T> clazz)
|
|
queryForRowObjectList(String sql,
Object[] args,
Class<T> clazz)
|
|
queryForSingleColumnList(String sql,
Class<T> elementType)
|
|
queryForSingleColumnList(String sql,
Object[] args,
Class<T> elementType)
|
String |
queryForString(String sql)
|
String |
queryForString(String sql,
Object[] args)
|
void |
setLoadDefaultConfig(boolean loadDefaultConfig)
デフォルトのBean定義ファイルを読み込むかどうかを設定します |
int |
update(String sql)
|
int |
update(String sql,
Object[] args)
|
クラス org.springframework.test.AbstractTransactionalDataSourceSpringContextTests から継承されたメソッド |
countRowsInTable, deleteFromTables, executeSqlScript, getJdbcTemplate, setComplete, setDataSource, setSqlScriptEncoding |
クラス org.springframework.test.AbstractTransactionalSpringContextTests から継承されたメソッド |
endTransaction, isDefaultRollback, isRollback, onSetUp, onSetUpBeforeTransaction, onSetUpInTransaction, onTearDownAfterTransaction, onTearDownInTransaction, preventTransaction, setDefaultRollback, setTransactionDefinition, setTransactionManager, startNewTransaction |
クラス org.springframework.test.AbstractDependencyInjectionSpringContextTests から継承されたメソッド |
getAutowireMode, injectDependencies, isDependencyCheck, isPopulateProtectedVariables, prepareTestInstance, setAutowireMode, setDependencyCheck, setPopulateProtectedVariables |
クラス org.springframework.test.AbstractSingleSpringContextTests から継承されたメソッド |
contextKey, createApplicationContext, createBeanDefinitionReader, customizeBeanFactory, getApplicationContext, getConfigPaths, getLoadCount, loadContext, loadContextLocations, prepareApplicationContext, setDirty, setUp, tearDown |
クラス org.springframework.test.AbstractSpringContextTests から継承されたメソッド |
addContext, contextKeyString, getContext, hasCachedContext, isContextKeyEmpty, setDirty |
クラス org.springframework.test.ConditionalTestCase から継承されたメソッド |
getDisabledTestCount, isDisabledInThisEnvironment, recordDisabled, runBare |
クラス junit.framework.TestCase から継承されたメソッド |
countTestCases, createResult, getName, run, run, runTest, setName, toString |
クラス junit.framework.Assert から継承されたメソッド |
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail |
DaoTestCase
public DaoTestCase()
DaoTestCase
public DaoTestCase(String name)
getConfigPath
protected final String getConfigPath()
- オーバーライド:
- クラス
org.springframework.test.AbstractSingleSpringContextTests
内の getConfigPath
getBean
public <T> T getBean(String name)
- Beanを取得します。
- 型パラメータ:
T
- 取得するBeanの型- パラメータ:
name
- beanのidまたはname
- 戻り値:
- 取得したBean
beforeGetConfig
protected void beforeGetConfig()
- Bean定義ファイルパス取得前処理。
DIコンテナを作成する前に実行したい処理はこのメソッドをオーバライドして実行してください。
例:JNDIをアクティベートする等
addConfigLocations
protected void addConfigLocations(List<String> configLocations)
- 読み込むBean定義ファイルパスを追加します。
引数のBean定義ファイルパスリストに追加したいファイルのパスをaddするようオーバーライドしてください。
- パラメータ:
configLocations
- 読み込むBean定義ファイルリスト
onTearDown
protected void onTearDown()
throws Exception
- オーバーライド:
- クラス
org.springframework.test.AbstractTransactionalSpringContextTests
内の onTearDown
- 例外:
Exception
getConfigLocations
protected final String[] getConfigLocations()
- オーバーライド:
- クラス
org.springframework.test.AbstractSingleSpringContextTests
内の getConfigLocations
setLoadDefaultConfig
public void setLoadDefaultConfig(boolean loadDefaultConfig)
- デフォルトのBean定義ファイルを読み込むかどうかを設定します
- パラメータ:
loadDefaultConfig
- デフォルトのBean定義ファイルを読み込むかどうか
isLoadDefaultConfig
public boolean isLoadDefaultConfig()
- デフォルトのBean定義ファイルを読み込むかどうかを取得します
- 戻り値:
- デフォルトのBean定義ファイルを読み込むかどうか
deleteFromTable
protected void deleteFromTable(String name)
execute
public void execute(String sql)
throws org.springframework.dao.DataAccessException
- パラメータ:
sql
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.execute(org.springframework.jdbc.core.JdbcTemplate, java.lang.String)
update
public int update(String sql)
throws org.springframework.dao.DataAccessException
- パラメータ:
sql
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.update(org.springframework.jdbc.core.JdbcTemplate, java.lang.String)
update
public int update(String sql,
Object[] args)
throws org.springframework.dao.DataAccessException
- パラメータ:
sql
- args
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.update(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.Object[])
batchUpdate
public int[] batchUpdate(String... sqls)
throws org.springframework.dao.DataAccessException
- パラメータ:
sqls
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.batchUpdate(org.springframework.jdbc.core.JdbcTemplate, java.lang.String[])
queryForLong
public long queryForLong(String sql)
throws org.springframework.dao.DataAccessException
- パラメータ:
sql
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForLong(org.springframework.jdbc.core.JdbcTemplate, java.lang.String)
queryForLong
public long queryForLong(String sql,
Object[] args)
throws org.springframework.dao.DataAccessException
- パラメータ:
sql
- args
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForLong(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.Object[])
queryForInt
public int queryForInt(String sql)
throws org.springframework.dao.DataAccessException
- パラメータ:
sql
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForInt(org.springframework.jdbc.core.JdbcTemplate, java.lang.String)
queryForInt
public int queryForInt(String sql,
Object[] args)
throws org.springframework.dao.DataAccessException
- パラメータ:
sql
- args
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForInt(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.Object[])
queryForString
public String queryForString(String sql)
throws org.springframework.dao.DataAccessException
- パラメータ:
sql
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForString(org.springframework.jdbc.core.JdbcTemplate, java.lang.String)
queryForString
public String queryForString(String sql,
Object[] args)
throws org.springframework.dao.DataAccessException
- パラメータ:
sql
- args
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForString(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.Object[])
queryForObject
public <T> T queryForObject(String sql,
Class<T> requiredType)
throws org.springframework.dao.DataAccessException
- 型パラメータ:
T
- - パラメータ:
sql
- requiredType
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForObject(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.Class)
queryForObject
public <T> T queryForObject(String sql,
Object[] args,
Class<T> requiredType)
throws org.springframework.dao.DataAccessException
- 型パラメータ:
T
- - パラメータ:
sql
- args
- requiredType
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForObject(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.Object[], java.lang.Class)
queryForRowMap
public Map<String,?> queryForRowMap(String sql)
throws org.springframework.dao.DataAccessException
- パラメータ:
sql
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForRowMap(org.springframework.jdbc.core.JdbcTemplate, java.lang.String)
queryForRowMap
public Map<String,?> queryForRowMap(String sql,
Object[] args)
throws org.springframework.dao.DataAccessException
- パラメータ:
sql
- args
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForRowMap(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.Object[])
queryForRowObject
public <T> T queryForRowObject(String sql,
Class<T> clazz)
throws org.springframework.dao.DataAccessException
- 型パラメータ:
T
- - パラメータ:
sql
- clazz
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForRowObject(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.Class)
queryForRowObject
public <T> T queryForRowObject(String sql,
Object[] args,
Class<T> clazz)
throws org.springframework.dao.DataAccessException
- 型パラメータ:
T
- - パラメータ:
sql
- args
- clazz
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForRowObject(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.Object[], java.lang.Class)
queryForSingleColumnList
public <T> List<T> queryForSingleColumnList(String sql,
Class<T> elementType)
throws org.springframework.dao.DataAccessException
- 型パラメータ:
T
- - パラメータ:
sql
- elementType
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForSingleColumnList(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.Class)
queryForSingleColumnList
public <T> List<T> queryForSingleColumnList(String sql,
Object[] args,
Class<T> elementType)
throws org.springframework.dao.DataAccessException
- 型パラメータ:
T
- - パラメータ:
sql
- args
- elementType
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForSingleColumnList(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.Object[], java.lang.Class)
queryForRowMapList
public List<Map<String,?>> queryForRowMapList(String sql)
throws org.springframework.dao.DataAccessException
- パラメータ:
sql
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForRowMapList(org.springframework.jdbc.core.JdbcTemplate, java.lang.String)
queryForRowMapList
public List<Map<String,?>> queryForRowMapList(String sql,
Object[] args)
throws org.springframework.dao.DataAccessException
- パラメータ:
sql
- args
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForRowMapList(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.Object[])
queryForRowMapList
public List<Map<String,?>> queryForRowMapList(Class<?> clazz)
throws org.springframework.dao.DataAccessException
- パラメータ:
clazz
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForRowMapList(org.springframework.jdbc.core.JdbcTemplate, java.lang.Class)
queryForRowObjectList
public <T> List<T> queryForRowObjectList(String sql,
Class<T> clazz)
throws org.springframework.dao.DataAccessException
- 型パラメータ:
T
- - パラメータ:
sql
- clazz
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForRowObjectList(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.Class)
queryForRowObjectList
public <T> List<T> queryForRowObjectList(String sql,
Object[] args,
Class<T> clazz)
throws org.springframework.dao.DataAccessException
- 型パラメータ:
T
- - パラメータ:
sql
- args
- clazz
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForRowObjectList(org.springframework.jdbc.core.JdbcTemplate, java.lang.String, java.lang.Object[], java.lang.Class)
queryForRowObjectList
public <T> List<T> queryForRowObjectList(Class<T> clazz)
throws org.springframework.dao.DataAccessException
- 型パラメータ:
T
- - パラメータ:
clazz
-
- 戻り値:
-
- 例外:
org.springframework.dao.DataAccessException
- 関連項目:
JdbcTemplateUtils.queryForRowObjectList(org.springframework.jdbc.core.JdbcTemplate, java.lang.Class)
createSelectSql
public String createSelectSql(String tableName,
String[] fieldNames)
- パラメータ:
tableName
- fieldNames
-
- 戻り値:
- 関連項目:
JdbcTemplateUtils.createSelectSql(java.lang.String, java.lang.String[])
createSelectSql
public String createSelectSql(String tableName,
Class<?> clazz)
- パラメータ:
tableName
- clazz
-
- 戻り値:
- 関連項目:
JdbcTemplateUtils.createSelectSql(java.lang.String, java.lang.Class)
createSelectSql
public String createSelectSql(Class<?> clazz)
- パラメータ:
clazz
-
- 戻り値:
- 関連項目:
JdbcTemplateUtils.createSelectSql(java.lang.Class)
createInsertSql
public String createInsertSql(String tableName,
String[] fieldNames)
- パラメータ:
tableName
- fieldNames
-
- 戻り値:
- 関連項目:
JdbcTemplateUtils.createInsertSql(java.lang.String, java.lang.String[])
createInsertSql
public String createInsertSql(String tableName,
Class<?> clazz)
- パラメータ:
tableName
- clazz
-
- 戻り値:
- 関連項目:
JdbcTemplateUtils.createInsertSql(java.lang.String, java.lang.Class)
createInsertSql
public String createInsertSql(Class<?> clazz)
- パラメータ:
clazz
-
- 戻り値:
- 関連項目:
JdbcTemplateUtils.createInsertSql(java.lang.Class)
Copyright © 2011. All Rights Reserved.