■ DbSetup
* DBの単体試験における以下の事ができる 1) DBのデータクリア(初め、後処理) 2) テストデータをDBに入れる
メモ
* 参照系の試験で使用するには、使いやすい * 更新系の確認するためのツールではない(?) => AssertJ-DBなどで行う? => AssertJ-DB について、以下の関連記事を参照のことhttp://blogs.yahoo.co.jp/dk521123/36157721.html
■ 設定
* いくつか方法がある。(以下の方法以外にもmavenとかもある)
その1: 直接、ダウンロード
* 以下のサイトからJAR(今回は「DbSetup-2.1.0.jar」)を 直接、ダウンロードし、インポートしておくhttp://mvnrepository.com/artifact/com.ninja-squad/DbSetup
その2: Gradleで取得する
dependencies { // ... 略 ... // Use JUnit test framework testImplementation 'junit:junit:4.12' // DbSetup testCompile 'com.ninja-squad:DbSetup:2.1.0' }http://dbsetup.ninja-squad.com/download.html
■ サンプル
* ユーザズガイドを見るとわかるhttp://dbsetup.ninja-squad.com/user-guide.html#getting-started
日本語訳
http://kagamihoge.hatenablog.com/entry/2015/08/14/205330
環境
* Windows10 * Java1.8 * Eclipse Mars.2 Release (4.5.2) * MySQL * DbSetup-2.1.0
サンプルデータ:MySQL
CREATE TABLE `section` ( `id` BIGINT(20) NOT NULL AUTO_INCREMENT, `name` VARCHAR(100) NOT NULL, PRIMARY KEY (`id`) ) COLLATE='utf8_general_ci' ENGINE=InnoDB;
Section.java
public class Section { public Long id; public String name; }
テスト対象クラス : TargetSample.java
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class TargetSample { public static void main(String[] args) throws Exception { for (Section section : getAllSections()) { System.out.println("***************************"); System.out.println("section.id : " + section.id); System.out.println("section.name : " + section.name); } } public static List<Section> getAllSections() throws Exception { Class.forName("com.mysql.jdbc.Driver"); List<Section> returnValues = new ArrayList<Section>(); try ( Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sampledb", "root", "password"); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM section");) { while (resultSet.next()) { Section section = new Section(); section.id = resultSet.getLong("id"); section.name = resultSet.getString("name"); returnValues.add(section); } } return returnValues; } }
TargetSampleTest.java
* テストコードimport static org.junit.Assert.*; import java.util.List; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import com.ninja_squad.dbsetup.DbSetup; import com.ninja_squad.dbsetup.destination.Destination; import com.ninja_squad.dbsetup.destination.DriverManagerDestination; import com.ninja_squad.dbsetup.operation.Operation; import static com.ninja_squad.dbsetup.Operations.*; /** * */ public class TargetSampleTest { private static Destination destination; public static final Operation DELETE_ALL = deleteAllFrom("section"); public static final Operation CLEAR_ID = sql("ALTER TABLE section AUTO_INCREMENT = 1;"); /** * @throws java.lang.Exception */ @BeforeClass public static void setUpBeforeClass() throws Exception { destination = new DriverManagerDestination("jdbc:mysql://localhost:3306/sampledb", "root", "password"); } /** * @throws java.lang.Exception */ @AfterClass public static void tearDownAfterClass() throws Exception { } /** * @throws java.lang.Exception */ @Before public void setUp() throws Exception { Operation operation = sequenceOf( DELETE_ALL, CLEAR_ID, insertInto("section") .columns("name") .values("General Affairs Department") .values("Human Resources Division") .values("Accounting Department") .values("Development Department") .build()); DbSetup dbSetup = new DbSetup(destination, operation); dbSetup.launch(); } /** * @throws java.lang.Exception */ @After public void tearDown() throws Exception { DbSetup dbSetup = new DbSetup(destination, DELETE_ALL); dbSetup.launch(); } @Test public void getAllPeopleTest() { try { List<Section> results = TargetSample.getAllSections(); assertNotNull(results); assertEquals(4, results.size()); Section result; results.sort((x1, x2) -> x1.id.compareTo(x2.id)); result = results.get(0); assertEquals(1L, result.id.longValue()); assertEquals("General Affairs Department", result.name); result = results.get(1); assertEquals(2L, result.id.longValue()); assertEquals("Human Resources Division", result.name); result = results.get(2); assertEquals(3L, result.id.longValue()); assertEquals("Accounting Department", result.name); result = results.get(3); assertEquals(4L, result.id.longValue()); assertEquals("Development Department", result.name); } catch (Exception e) { fail("Exception..."); e.printStackTrace(); } } }