如何在没有 spring 引导的情况下为 junit 在 J2EE 中模拟数据库
How to mock the dtabase in J2EE for junit without spring boot
我正在处理一个完全基于 Java/J2EE servlet 构建的遗留项目。没有 spring 和其他框架。它建立在蚂蚁之上。
现在我想将 junit 与我的项目集成,我能够集成但我想模拟数据库和 运行 模拟数据库连接中的测试用例。
我认为在 spring 启动时我们可以使用 H2 数据库轻松地做到这一点。但我面临的唯一挑战是我们没有使用 spring 引导。
我能够模拟函数和 运行 测试用例,但我需要的是模拟完整的数据库和 运行 模拟数据库上的测试 类。
任何线索我如何才能实现这个或者它是否可行?
你可以 运行 h2 database
纯 java
因为它是写在里面的。见 H2 Documentation.
在纯 java 和 运行 sql
脚本中启动 h2 database
服务器
要启动 tcp-server,您可以使用 Server
工具:
Server server = Server.createTcpServer();
server.start();
要开始也 web-server(h2-console
)你可以添加:
Server server = Server.createWebServer();
server.start();
打开JdbcConnection
and RunScript
可以这样写:
private void createTables(String url, String username,
String password, File script)
throws SQLException, IOException {
Properties info = new Properties();
info.put("user", username);
info.put("password", password);
JdbcConnection connection = new JdbcConnection(url, info);
InputStreamReader reader =
new InputStreamReader(new FileInputStream(script));
RunScript.execute(connection, reader);
reader.close();
connection.close();
}
另请参阅:
- How to enable h2-console in spring-webmvc without spring-boot?
- Executing script file in h2 database
我正在处理一个完全基于 Java/J2EE servlet 构建的遗留项目。没有 spring 和其他框架。它建立在蚂蚁之上。 现在我想将 junit 与我的项目集成,我能够集成但我想模拟数据库和 运行 模拟数据库连接中的测试用例。 我认为在 spring 启动时我们可以使用 H2 数据库轻松地做到这一点。但我面临的唯一挑战是我们没有使用 spring 引导。 我能够模拟函数和 运行 测试用例,但我需要的是模拟完整的数据库和 运行 模拟数据库上的测试 类。 任何线索我如何才能实现这个或者它是否可行?
你可以 运行 h2 database
纯 java
因为它是写在里面的。见 H2 Documentation.
在纯 java 和 运行 sql
脚本中启动 h2 database
服务器
要启动 tcp-server,您可以使用
Server
工具:Server server = Server.createTcpServer(); server.start();
要开始也 web-server(
h2-console
)你可以添加:Server server = Server.createWebServer(); server.start();
打开
JdbcConnection
andRunScript
可以这样写:private void createTables(String url, String username, String password, File script) throws SQLException, IOException { Properties info = new Properties(); info.put("user", username); info.put("password", password); JdbcConnection connection = new JdbcConnection(url, info); InputStreamReader reader = new InputStreamReader(new FileInputStream(script)); RunScript.execute(connection, reader); reader.close(); connection.close(); }
另请参阅:
- How to enable h2-console in spring-webmvc without spring-boot?
- Executing script file in h2 database