H2DB インメモリで DDL(sqlファイル)からテーブルを作成できません
Mavenのweb-appプロジェクトを作成し、H2DB インメモリモードでDB周りのテストコードを書いています。
org.h2.tools.RunScript.execute();
を使ってsqlファイルを読み込んでテーブル作成したいのですが、
作成できていないようで、'NoSuchTableException'
が出ます。
もし、原因になりそうな情報があれば、お教え願えますでしょうか。
よろしくお願いいたします。
自作のメソッドでエラーが出ている行番号です。↓
at testModel.LoginLogicTest.readDataSet(LoginLogicTest.java:33)
at testModel.LoginLogicTest.createSchema(LoginLogicTest.java:59)
LoginLogicTest.java
private static final String JDBC_DRIVER = org.h2.Driver.class.getName();
private static final String JDBC_URL = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;TRACE_LEVEL_FILE=4";
private static final String USER = "sa";
private static final String PASSWORD = "";
//(変更後)
public static IDataSet readDataSet(String dataPath)throws Exception{
//(LoginLogicTest.java:33)
IDataSet dataSet=new FlatXmlDataSetBuilder().build(new File(dataPath));
return dataSet;
}
public static void cleanlyInsert(IDataSet dataSet)throws Exception{
IDatabaseTester dbTester=new JdbcDatabaseTester(JDBC_DRIVER,JDBC_URL,USER,PASSWORD);
dbTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
dbTester.setDataSet(dataSet);
dbTester.onSetup();
}
public static DataSource dataSource() {
JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setURL(JDBC_URL);
dataSource.setUser(USER);
dataSource.setPassword(PASSWORD);
return dataSource;
}
@BeforeAll
public static void createSchema() throws Exception {
RunScript.execute(JDBC_URL, USER, PASSWORD, "src/test/resources/schema.sql", null , false);
//(LoginLogicTest.java:59)
IDataSet dataset=readDataSet("src/test/resources/setup_dataset.xml");
cleanlyInsert(dataset);
}
:
:
スタックトレース
org.dbunit.dataset.NoSuchTableException: Account
at org.dbunit.dataset.AbstractDataSet.getTable(AbstractDataSet.java:172)
at org.dbunit.dataset.xml.FlatDtdDataSet.getTable(FlatDtdDataSet.java:181)
at org.dbunit.dataset.AbstractDataSet.getTableMetaData(AbstractDataSet.java:156)
at org.dbunit.dataset.xml.FlatDtdDataSet.getTableMetaData(FlatDtdDataSet.java:168)
at org.dbunit.dataset.xml.FlatXmlProducer.createTableMetaData(FlatXmlProducer.java:189)
at org.dbunit.dataset.xml.FlatXmlProducer.startElement(FlatXmlProducer.java:445)
at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
at org.apache.xerces.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.emptyElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dbunit.dataset.xml.FlatXmlProducer.produce(FlatXmlProducer.java:365)
at org.dbunit.dataset.CachedDataSet.<init>(CachedDataSet.java:80)
at org.dbunit.dataset.xml.FlatXmlDataSet.<init>(FlatXmlDataSet.java:110)
at org.dbunit.dataset.xml.FlatXmlDataSetBuilder.buildInternal(FlatXmlDataSetBuilder.java:264)
at org.dbunit.dataset.xml.FlatXmlDataSetBuilder.build(FlatXmlDataSetBuilder.java:111)
at testModel.LoginLogicTest.readDataSet(LoginLogicTest.java:33)
at testModel.LoginLogicTest.createSchema(LoginLogicTest.java:59)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at .....
:
:
追記
pom.xml(省略版、h2db関連のみ)
※その他schema.sqlはh2コンソールで動作確認済み。
<project xmlns="http://maven.apache.org/POM/4.0.0"
:
:
<url>http://maven.apache.org</url>
<properties>
:
<dependencies>
:
:
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.199</version>
<scope>test</scope>
</dependency>
:
:
</dependencies>
<build>
<pluginManagement>
<plugins>
:
:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>1.5</version>
<configuration>
<driver>org.h2.Driver</driver>
<url>jdbc:h2:mem:test</url>
<username>sa</username>
<password> </password>
<srcFiles>
<srcFile>${basedir}/src/test/resources/schema.sql</srcFile>
</srcFiles>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<finalName>upload_image_db2</finalName>
</build>
</project>
/src/test/resources/setup_dataset.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<dataset>
<Account userId="Shima5" pass="135790" name="Shima.I" age="33"/>
</dataset>