Jmock 模拟 DAO 对象
我编写了一个方法,在给定正确的字符串键的情况下检索某些字符串列表。现在,当我创建一个列表(通过上一句中描述的方法检索的列表)并创建测试时,我可以轻松获得结果并成功通过测试。
现在,另一方面,如果我将此列表的内容保存到数据库中的两列中,key
和 value
我编写了一个类,它使用其中的方法检索这些项目。当我打印出来以控制台预期结果是正确的时,现在我从应用程序上下文初始化我的 DAO,在它的 bean 内部它获取会话并且因为 DAO 工作。
现在我正在尝试编写一个模拟 DAO 的测试,因为我在本地运行测试而不是在服务器上..所以我告诉 jmock 来模拟它:
private MyDAO myDAO;
在 setup() 中
myDAO = context.mock(MyDAO.class);
我认为我正确地模拟了它或者不,我如何从数据库中模拟这些数据?最好的方法是什么?
有没有好的 Jmock 文档?在他们的官方网站上它不是很好也很清楚,你必须知道你在寻找什么才能找到它,同时不能发现一些很酷的东西。
或者有人可以帮助我使用这种方法:
我如何创建仅用于测试的应用程序上下文,以实例化 DAO 和一些 bean,就像在服务器上一样。那么我可以在测试中使用它吗?也欢迎建议、解释。谢谢
I wrote a method that retrieves certain list of strings, given a correct string key. Now when I create a list(the one to be retrieved by method descibed in previous sentence) and create test I can easily get results and test passes successfully.
Now on the other hand if I save the content of this list to database in 2 columns, key
and value
I wrote a class which retrieves this items with method inside it. And when I print it out to console the expected results are correct, now I initialize my DAO from application context where inside its bean it gets session and because of DAO works.
Now I'm trying to write a test which will mock the DAO, because I'm running test localy not on the server .. so I told jmock to mock it :
private MyDAO myDAO;
in the setup()
myDAO = context.mock(MyDAO.class);
I think I'm mocking it correctly or not, how can I mock this data from database? what is the best way?
Is there somewhere good Jmock documentation? on their official site its not very good and clear, you have to know what you seek in order to find it, can't discover something cool in the mean time.
OR can someone help me with this approach :
How can I create application context which I will use just for tests, to instansiate DAO and few beans there like on server. So I can use it in the tests? Also suggestions, explanation all is welcome . thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此和这篇文章描述了如何以及为何测试 DAO。 (如果您不使用 spring 和 maven,您可以轻松地将 spring 和 maven 从示例中分离出来 - 重点是使用 HSQLDB)
This and this posts describe how and why to test DAOs. (you can easily isolate spring and maven from the example, if you are not using them - the point is using HSQLDB)
MyDAO是一个接口,你只能模拟接口。
MyDAO is a interface, you only can make a mock of interfaces.