投稿‎ > ‎

MySQLでjpetstoreのトランザクション管理の単体テスト

posted Jul 29, 2013, 8:51 AM by Zhang Wenxu   [ updated Nov 18, 2013, 12:59 AM ]
1.datasourceを定義する
applicatiopnContext.xmlにあるdatasourceを修正する

datasoruce定義

<!--      

<jdbc:embedded-database id="dataSource">

        <jdbc:script location="classpath:database/jpetstore-hsqldb-schema.sql"/>

        <jdbc:script location="classpath:database/jpetstore-hsqldb-dataload.sql"/>

    </jdbc:embedded-database>

-->


<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name="driverClassName" value="com.mysql.jdbc.Driver" />

    <property name="url" value="jdbc:mysql://localhost:3306/jpetstore" />

    <property name="username" value="root" />

    <property name="password" value="admin" />

</bean>



2.データベースを構築する
MySQLのインストールは割愛する

データベース構築

mysql> create database jpetstore;

mysql> source /jpetstore/src/test/resources/db/mysql/initDB.sql 

mysql> source /jpetstore/src/test/resources/db/mysql/populateDB.sql 

initDB.sqlはjpetstore-hsqldb-schema.sqlと同じ内容
populateDB.sqlは、jpetstore-hsqldb-dataload.sqlと同じ内容

3.テストメソッドを作成

テストメソッド

@Test

@Transactional

public void testInsertAcount() throws Exception{

Account account = new Account();

account.setUsername("myid2");

account.setPassword("String");

account.setEmail("String");

account.setFirstName("String");

account.setLastName("String");

account.setStatus("OK");

account.setAddress1("String");

account.setAddress2("String");

account.setCity("String");

account.setState("String");

account.setZip("String");

account.setCountry("String");

account.setPhone("String");

account.setFavouriteCategoryId("String");

account.setLanguagePreference("String");

account.setListOption(true);

account.setBannerOption(true);

account.setBannerName("String");

accountMapper.insertAccount(account);

}

@Transactionalアノテーションより、メソッド内unchecked exceptions (that is, subclasses of java.lang.RuntimeException)が発生したときに、DBがローバークされる。

4.checked exceptionでもロールバークしたい場合は、@Transactional(rollbackFor=DBException.class)を宣言する必要、DBExceptionはchecked Exeptionである

Rollback for checked exception

/**

* Only unchecked exceptions (that is, subclasses of java.lang.RuntimeException) 

* are rollbacked by default in spring. If want to roll back for checked exception, 

* need to declare by this way @Transactional(rollbackFor=DBException.class)

* @throws Exception

*/

@Test

@Transactional(rollbackFor=Exception.class)

public void testInsertAcount() throws Exception{

Account account = new Account();

account.setUsername("myid7");

account.setPassword("String");

account.setEmail("String");

account.setFirstName("String");

account.setLastName("String");

account.setStatus("OK");

account.setAddress1("String");

account.setAddress2("String");

account.setCity("String");

account.setState("String");

account.setZip("String");

account.setCountry("String");

account.setPhone("String");

account.setFavouriteCategoryId("String");

account.setLanguagePreference("String");

account.setListOption(true);

account.setBannerOption(true);

account.setBannerName("String");

accountMapper.insertAccount(account);

throw new Exception();

}


このテストメソッドは、コミットせず、ロールバークされる。(本来は、Excpeionのチェックを入れるが、ここで割愛)
Google+
By Zhang Wenxu
Comments