diff --git a/service-mybatis/src/main/java/fi/nls/oskari/mybatis/MyBatisHelper.java b/service-mybatis/src/main/java/fi/nls/oskari/mybatis/MyBatisHelper.java index 223e8821f4..78c2d4e4aa 100755 --- a/service-mybatis/src/main/java/fi/nls/oskari/mybatis/MyBatisHelper.java +++ b/service-mybatis/src/main/java/fi/nls/oskari/mybatis/MyBatisHelper.java @@ -7,6 +7,9 @@ import org.apache.ibatis.transaction.TransactionFactory; import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; import javax.sql.DataSource; +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.List; public class MyBatisHelper { @@ -15,6 +18,10 @@ public static SqlSessionFactory initMyBatis(DataSource ds, Class... mappers) } public static Configuration getConfig(DataSource ds, Class... mappers) { + if (ds == null) { + // TODO: anything better to detect that we are in test env? + ds = getTestDS(); + } if (ds == null) { throw new NullPointerException("Tried initializing MyBatis without a datasource"); } @@ -26,6 +33,18 @@ public static Configuration getConfig(DataSource ds, Class... mappers) { return configuration; } + private static DataSource getTestDS() { + try { + // try to dig up TestHelper that is only available while testing to get a mem-based datasource + Class helper = Class.forName("fi.nls.test.util.TestHelper"); + Method m = helper.getMethod("createMemDBforUnitTest", List.class); + // TODO: call with DB initializing scripts to actually use this + return (DataSource) m.invoke(null, (Object) Collections.emptyList()); + } catch (Exception e) { + throw new RuntimeException("Not testing"); + } + } + public static void addAliases(Configuration config, Class... aliases) { for (Class alias : aliases) { config.getTypeAliasRegistry().registerAlias(alias);