问题描述
我有一个类,它连接到 H2 数据库并运行多个 SQL 语句.
I have a class, which connects to an H2 database and runs several SQL statements.
我想编写一个单元测试,它验证在 open
方法中执行了某个 SQL 语句(DROP TABLE IF EXISTS PERSON
).
I want to write a unit test, which verifies, that in the open
method a certain SQL statement (DROP TABLE IF EXISTS PERSON
) is executed.
为了做到这一点,我编写了以下测试:
In order to do this, I wrote following test:
但它不起作用 - DriverManager
不是模拟连接,而是返回真实连接.
But it doesn't work - instead of the mock connection, DriverManager
returns real connection.
如何解决(让 DriverManager
在测试中返回连接模拟)?
How can I fix it (make DriverManager
return connection mock in the test) ?
这是我项目的 pom.xml
,可能有问题.
Here's the pom.xml
of my project, maybe something is wrong there.
推荐答案
这个可行(注意导入):
This one works (pay attention to the imports):
这篇关于如何模拟 DriverManager.getConnection(...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!