0
JUnitテストを使用してデータベースがUCanAccessドライバで正常に動作することを確認する際に問題が発生しています。私はIntelliJの通過JUnitテストを実行したときにplay 2.6.3では、実行中に実行中にapplication.confからデータベースを検出しますが、テスト中ではありません。
[info] application - Creating Pool for datasource 'default'
[info] p.a.d.DefaultDBApi - Database [default] connected at jdbc:ucanaccess://c:/Users/me/Documents/testdb.accdb
はしかし、私は次のエラーを取得する:
Configuration error: Configuration error[Driver not found: [org.h2.Driver}]]
at play.api.Configuration$.configError(Configuration.scala:155)
at play.api.Configuration.reportError(Configuration.scala:984)
at play.api.db.DefaultDatabase$$anonfun$driver$1.apply(Databases.scala:114)
at play.api.db.DefaultDatabase$$anonfun$driver$1.apply(Databases.scala:108)
...
私はのIntelliJ IDEAに通常サーバーを実行すると、Iは、データベース・プールが正しく設定されていることを告げています
Playがデフォルトのデータベース構成、つまりh2データベースを使用するようにデフォルト設定されているために上記のエラーが発生していると仮定します。
JUnitテストでh2データベースをデフォルトに設定するのではなく、データベースドライバを認識できるようにするにはどうすればよいですか?
テスト:
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import play.db.Database;
import play.db.Databases;
import play.db.evolutions.Evolution;
import play.db.evolutions.Evolutions;
import java.sql.Connection;
import static org.junit.Assert.assertTrue;
public class ApplicationTest {
Database database;
@Before
public void setupDatabase() {
// Gets default database in this case
database = Databases.inMemory();
System.out.println(database.getConnection());
Evolutions.applyEvolutions(database, Evolutions.forDefault(new Evolution(
1,
"create table teste (id bigint not null, name varchar(255));",
"drop table test;"
)));
}
@After
public void shutdownDatabase() {
Evolutions.cleanupEvolutions(database);
database.shutdown();
}
@Test
public void databaseCRUD() throws Exception {
Connection conn = database.getConnection();
conn.prepareStatement("insert into test values (10, 'testing')").execute();
// Make sure that there is a tenth element in the table
assertTrue(
conn.prepareStatement("select * from test where id = 10 and name = 'testing'")
.executeQuery().next()
);
}
}