Java模拟数据库连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8279547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-15 00:36:53  来源:igfitidea点击:

Java mock database connection

javaunit-testingjakarta-eetddmocking

提问by nyanev

I want to test class with make db connection. Class that I want to test accept as param in constructor Connectionclass. I want to pass mock object to the constructor. Can you tell me good framework with example how to mock db connection?

我想用 make db connection 测试类。我要测试的Connection类在构造函数类中接受为参数。我想将模拟对象传递给构造函数。你能用例子告诉我如何模拟数据库连接的好框架吗?

采纳答案by Tomasz Nurkiewicz

You can use MockRunner, which has support for JDBC. General mocking frameworks like Mockitowill also work, but JDBC is a set of interfaces returning each other so hand-mocking will be hard. See for yourself: How to stub/mock JDBC ResultSet to work both with Java 5 and 6?

您可以使用MockRunner,它支持JDBC。像Mockito这样的通用模拟框架也可以使用,但 JDBC 是一组相互返回的接口,因此手动模拟会很困难。亲自看看:如何存根/模拟 JDBC ResultSet 以同时使用 Java 5 和 6?

However mocking JDBC is so brittle and verbose (no matter which tools you use) that I would either suggest abstracting JDBC access within some thin DAO layer (see @duffymoanswer) or go for in-memory database like H2.

然而,模拟 JDBC 是如此脆弱和冗长(无论您使用哪种工具),我建议要么在一些薄的 DAO 层中抽象 JDBC 访问(请参阅@duffymo答案),要么使用像H2这样的内存数据库。

See also:

也可以看看:

回答by JB Nizet

Connection is an interface. Any mocking framework will be able to mock it: EasyMock, Mockito, ...

连接是一个接口。任何模拟框架都可以模拟它:EasyMock, Mockito, ...

Mocking it isn't different from mocking any other Java interface.

模拟它与模拟任何其他 Java 接口没有什么不同。

回答by erimerturk

you can try easymock. it is easy to use i think. you can find a tutorial for reference. easymock

你可以试试easymock。我认为它很容易使用。你可以找到一个教程供参考。易模

回答by duffymo

I wouldn't create a mock connection - it proves nothing, in my opinion.

我不会创建模拟连接 - 在我看来,它证明不了任何事情。

I can see why you'd mock the repository/DAO itself after you've tested it fully with a live connection. You'd give the mock repository/DAO to a service or other client because you've already tested it - no need to prove that it works until you do an integration test.

我可以理解为什么在使用实时连接对其进行了全面测试后,您还要模拟存储库/DAO 本身。您将模拟存储库/DAO 提供给服务或其他客户端,因为您已经对其进行了测试 - 在您进行集成测试之前无需证明它有效。

回答by Sebastien Lorber

If you are going to reuse that mock on many test cases you can also consider implementing your own implementation of connection and to reuse that implementation everywhere.

如果您打算在许多测试用例上重用该模拟,您还可以考虑实现自己的连接实现并在任何地方重用该实现。

回答by GETah

You can either use a mocking framework such as the ones mentioned in the above answer (I personally use EasyMock) ORCreate you own mock object:

您可以使用模拟框架,例如上述答案中提到的框架(我个人使用 EasyMock) 创建您自己的模拟对象:

class FakeConnection extends Connection{
       // Overrive all method behavious you want to fake.
}

回答by cchantep

My Acolyte framework is useful for such purposes -> https://github.com/cchantep/acolyte.

我的 Acolyte 框架可用于此类目的 -> https://github.com/ccantep/acolyte

With this lib you can create a connection instance for which you provide handler. Implementing handler, you are able to 'dispatch' query or update : producing resultsets or update count (or warning).

使用这个库,你可以创建一个你提供处理程序的连接实例。实施处理程序,您可以“调度”查询或更新:生成结果集或更新计数(或警告)。