我已经编写了一个以javax.sql.DataSource作为构造器arg的存储库类,并对其进行了一些基本的CRUD操作。现在我想为它编写junit测试。我想使用HSQL作为我的数据库来进行这些测试,但是我不确定如何按照我想要的方式设置数据库。使用Spring应用程序,我已经在我的应用程序上下文中放入了类似这样的东西来进行测试:
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:mySQLForDB.sql"/>
</jdbc:embedded-database>然而,我想测试的不是Spring应用程序。在代码中有没有设置数据源和从sql文件设置数据库的方法?基本上,在功能上等同于Spring对<jdbc:embedded-database>标记所做的事情。
发布于 2012-09-03 23:44:32
你会考虑试试h2吗?
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:mem:mytestdb",
"sa", "");
Statement st = conn.createStatement();
st.execute("create table foo (id integer)");
st.execute("insert into foo (id) values (1)");
ResultSet rs = st.executeQuery("select * from foo");
while (rs.next()) {
int result = rs.getInt("id");
System.out.println(result);
}
conn.close();
}HSQL也有同样的方法,但还没有尝试过。看看this link
我希望这能帮到你。
https://stackoverflow.com/questions/10916729
复制相似问题