我们有一个使用Spring和MAVEN的Java项目.在这个项目中,我们在内存中使用H2数据库在我们的DAO/Repository层上执行几个测试.
经过几次测试后,但并不总是,我们得到以下错误:
org.h2.jdbc.JdbcSQLException: Table "WEATHER" not found; SQL statement:
Run Code Online (Sandbox Code Playgroud)
如果单独执行JUnit测试,它将永远不会失败.错误出现时没有模式.
我怀疑下面关于URL连接的RUNSCRIPT语句没有完成,并且单元测试开始时,即执行是异步执行的.
这是连接声明:
String jdbcUrl = "jdbc:h2:mem:WeatherAPI;MODE=MySQL;DB_CLOSE_ON_EXIT=TRUE;TRACE_LEVEL_SYSTEM_OUT=1;INIT=runscript from 'src/test/resources/sql/weatherapi.sql'"
Run Code Online (Sandbox Code Playgroud)
这个想法是每次测试都会重置数据库.
以下是获取DataSource对象的代码片段:
private static java.sql.DataSource ds = null;
public static DataSource getDs() {
if(this.ds==null) {
try {
this.ds = manualCreateDataSource();
} catch (Exception e) {
logger.error("Could not initialize Datasource", e);
throw new RuntimeException("Could not initialize Datasource");
}
}
return this.ds;
}
public static DataSource manualCreateDataSource() {
String driverClass = "org.h2.jdbcx.JdbcDataSource";
String jdbcUrl = "jdbc:h2:mem:WeatherAPI;MODE=MySQL;DB_CLOSE_ON_EXIT=TRUE;TRACE_LEVEL_SYSTEM_OUT=1;INIT=runscript from 'src/test/resources/sql/weatherapi.sql'";
int maxPoolSize = 20;
int minPoolSize …Run Code Online (Sandbox Code Playgroud)