How to test DAO methods using Mockito?(如何使用 Mockito 测试 DAO 方法?)
问题描述
我已经开始发现 Mockito 库,但有一个问题我没有找到正确的答案.
I've started to discovered Mockito library and there is a question for which I didn't find the proper answer.
如果我的 UserDAO 类中有这样的方法,可以将用户保存在数据库中:
If I have for example such method in my UserDAO class that saves user in database:
我应该如何测试它?
如果我想测试一个 DAO 类,那么我需要创建一个 DataSource
模拟、Connection
模拟、ResultSet
模拟等吗?所以不测试数据库本身?
If I want to test for example a DAO class then I need to create a DataSource
mock, Connection
mock, ResultSet
mock etc ? And so not to test the database itself ?
但是如果我还想测试 dao 和 database 的行为呢?
But what if I want to also test the behavior of dao and database ?
您能否提供一些可能有用的代码示例、链接并展示最佳方法?
Would you please produce some code samples, links that could be helpful and show best approaches of doing it ?
推荐答案
这是使用 Mockito 测试您的 UserDAO 的良好开端.此代码使用了大量的 Mockito 功能,因此您可以了解如何使用它们.如果您有任何问题,请告诉我.
Here is a good start using Mockito to test your UserDAO. This code uses a good amount of the Mockito features, so you can see how to use them. Let me know if you have questions.
这篇关于如何使用 Mockito 测试 DAO 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!