简体   繁体   English

Jmock 模拟 DAO 对象

[英]Jmock mock DAO object

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.现在另一方面,如果我将此列表的内容保存到数据库中的 2 列, keyvalue我编写了一个类,该类使用其中的方法检索这些项目。 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.当我将它打印到控制台时,预期的结果是正确的,现在我从应用程序上下文初始化我的 DAO,它在它的 bean 内部获取会话并且因为 DAO 工作。

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 :现在我正在尝试编写一个模拟 DAO 的测试,因为我不是在服务器上运行本地测试..所以我告诉 jmock 模拟它:

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?有没有好的 Jmock 文档? 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.如何创建仅用于测试的应用程序上下文,以在服务器上实例化 DAO 和少数 bean。 So I can use it in the tests?所以我可以在测试中使用它吗? Also suggestions, explanation all is welcome .还有建议,欢迎解释。 thank you谢谢你

This and this posts describe how and why to test DAOs.这个这个帖子描述了如何以及为什么测试 DAO。 (you can easily isolate spring and maven from the example, if you are not using them - the point is using HSQLDB) (如果您不使用它们,您可以轻松地将 spring 和 maven 与示例隔离开来——重点是使用 HSQLDB)

MyDAO 是一个接口,你只能模拟接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM