Java 中的 DB 后端 Web 应用程序测试 [需要工具]
我想为我的 java web 应用程序创建一个测试套件。它是一个具有 JDBC 连接性的 JSP 应用程序。我的要求如下,
1 - 我应该能够通过我的模型测试我的数据库逻辑(查询等)。
2 - 如果我也能测试我的 .jsp 页面(如果可能的话),那就太好了
经过一些研究,我发现 DBUnit 对于数据库后端系统测试很有用,但不幸的是我找不到任何好的资源作为初学者,
你们都觉得怎么样关于我拥有的测试选项,如果您也可以发布一些资源/示例的链接以及
编辑:
并且我遇到了模拟对象(如 JMock..),想知道我可以使用它作为 DBUnit 的替代品吗?
预先感谢
干杯
Sameera
I want to create a test suit for my java web application. Its a JSP applications with JDBC connectivity . My requirements are as follows,
1 - I should be able to test my database logic (Queries etc) through my models.
2 - Its great if i could test my .jsp pages as well (if possible)
After doing some research I found that DBUnit is good for database backend system testing, but unfortunately i couldnt find any good resource as a starter
What are you all think about testing options I have and it would be great if you could post some links to resources/ examples as well
EDIT:
and I have come across with mock objects (like JMock..), wonder I could use it as a replacement for DBUnit ?
thanks in advance
cheers
sameera
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DBUnit 的官方入门文章这里对我有用。回复:数据库逻辑测试,您可能还想查看这个。
至于 JSP 测试,我之前曾使用 Cactus 成功测试过我的 servlet。但是,我不知道它的 JSP 测试设施。
DBUnit's official getting-started article here worked for me. Re: database logic testing, you might also want to check this out.
As for JSP testing, I have used Cactus to test my servlets before with success. However, I'm don't know about its JSP-testing facilities.
对于您的第一个问题,请查看 此 StackOverFlow线程...
第二,我会接受 Chry 的建议 Cactus 或 < a href="http://seleniumhq.org/" rel="nofollow noreferrer">硒。
希望有帮助。
For your 1st question have a look at this StackOverFlow thread...
For 2nd, I would go with Chry's suggestion of Cactus or Selenium.
Hope that helps.
从您的问题中不清楚您是否想针对数据库层运行集成测试(前端+后端)或单元测试。
如果您需要一个允许您编写集成测试的工具,那么您应该明确地查看 Selenium。
使用 Selenium,您可以通过简单地浏览您的网站(JSP 页面)并断言页面上的内容存在或等于某些值来生成功能测试。
Selenium 附带了一个 Firefox 插件,它基本上会为您生成代码。您可以在浏览器中重播测试或将它们导出为 Java 代码并使它们成为测试套件的一部分。硒是一种无价的工具。
使用 Selenium 这样的工具的缺点是,您的应用程序需要先部署在某个地方,然后才能运行测试套件。如果您计划运行使用 Selenium 生成的自动化测试,这可能是一个限制。
如果您只对测试数据库访问代码(DAO,数据访问层)感兴趣,那么 DBUnit 是完美的工具。
通常,DBUnit 用于在测试之前初始化数据库表,并且偶尔用于对数据库内容运行断言。 DBUnit 使用基于 XML 的格式来表示将插入数据库的数据。
包含预填充数据库的数据的 XML 文件通常由构建脚本(Ant、Maven 等)触发或直接在单元测试代码中触发。
我已经使用了这两种方法,这实际上取决于代码的结构以及访问数据库的方式(Hibernate、Spring+Hibernate、JDBC...)。
如果您的数据库不是太大,我建议您在运行测试套件之前填充它。或者,您可以在每次测试之前仅填充您有兴趣测试的表。
这里是 Unitils 的链接,这是一个可以在 DBUnit 之上使用的附加库,以简化数据库测试策略。我认为它可以作为您入门的参考:
http://www.unitils.org/tutorial.html#Database_testing
这是另一个链接(相当旧,2004 年)显示 DBUnit 的基本机制:
http: //onjava.com/pub/a/onjava/2004/01/21/dbunit.html
It's not clear from your question if you want to run Integration tests (Front end + back end) or Unit Tests against you Database layer.
If you need a tool that allows you to write Integration tests, you should definitively look at Selenium.
With Selenium you can generate functional tests by simply navigating your web site (JSP pages) and asserting that stuff on the page exists or it's equal to some values.
Selenium comes with a Firefox plugin that will basically generate the code for you. You can replay the test in the browser or export them as Java code and make them part of your test suite. Selenium is an invaluable tool.
The drawback of using a tool like Selenium is that your application need to be deployed somewhere before you can run your test suite. This may be a limitation if you plan to run automated tests generated using Selenium.
If you are only interested in testing your database access code (DAO, Data Access Layer) DBUnit is the perfect tool.
Generally, DBUnit is used to initialize the database tables before testing and, less often, to run assertions on the database content. DBUnit uses an XML based format to represent the data that will be inserted into the database.
The XML files containing the data to pre-populate the db are normally triggered by a build script (Ant, Maven, etc.) or directly in your unit test code.
I have used both approaches, it really depends on how your code is structured and how you access the database (Hibernate, Spring+Hibernate, JDBC...).
If your database is not too big, I'd recommend you populate it just before running your test suite. Alternatively, you can populate only the tables that you are interested in testing prior to every test.
Here is a link to Unitils, that is an additional library that can be used on top of DBUnit to simplify the database testing strategy. I think it can be used as a reference to get you started:
http://www.unitils.org/tutorial.html#Database_testing
Here is anoter link (quite old, 2004) showing the basic mechanics of DBUnit:
http://onjava.com/pub/a/onjava/2004/01/21/dbunit.html