单元测试冰面

发布于 2024-08-12 04:14:43 字数 291 浏览 9 评论 0原文

您能否将 IceFaces 应用程序的组件分开,以便可以单独测试它们,而不是在组装的应用程序上使用 Selenium 或 HttpUnit 之类的东西?

支持 bean 可以轻松隔离(如果编写为可测试),但我有兴趣测试应用程序的模板/显示部分,同时尽可能少地使用应用程序的其余部分。这可以做到吗?如何?

有没有办法使用“虚拟数据”将 IceFaces 对象渲染为文本,然后我可以运行传统的单元测试?

我可以想出完成所有这些的方法,但它们涉及创建多个应用程序(我希望测试的每个组件一个)。然而,这似乎是一种次优的做事方式。

Can you separate components of an IceFaces application so they can be tested in isolation instead of using something like Selenium or HttpUnit on the assembled application?

Backing beans can be easily isolated (if written to be testable) but I am interested in testing the template/display parts of the application while using as little of the rest of the application as possible. Can this be done? How?

Is there a way to render an IceFaces object as text using "dummy data" that I can then run through traditional unit tests?

I can think of ways to do all of this, but they involve creating multiple applications (one for each component I wish to test). However, this seems like a sub-optimal way of doing things.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百合的盛世恋 2024-08-19 04:14:43

如果我正确理解您的问题,那么为您的页面创建特殊的虚拟支持 bean,然后创建一个测试 JSF 配置文件,将这些 bean 映射到 .jspx 文件,这应该是一个简单的问题。当然,虚拟 bean 不会触及任何业务逻辑或后端服务——它们只是简单的数据集,很容易在测试中验证。

创建一个 ant 脚本来替换您的虚拟支持 bean 和测试配置文件。运行您的测试。如果您不想要像 HTTPUnit 这样繁重的东西,并且您在应用程序中使用 Spring,请查看 这篇博文提供了一种在没有 Web 服务器的情况下模拟完整 Web 上下文的绝佳方法。您的测试可能需要嗅探原始 HTML 输出来验证结果。这会很棘手,因为 IceFaces 喜欢修改 DIV ID 以及您可能想要嗅探的 DOM 树的其他相关部分。 (仅此一点可能就是很少有 JSF 开发人员尝试对 JSF 输出进行单元测试的原因。)

验证测试后,将常规 bean 和配置文件交换回应用程序中。

瞧!您刚刚对 JSF 组件进行了单元测试。

请注意,交换 bean 和配置文件的整个过程很混乱。如果 IceFaces 使用 Spring 将支持 bean 与 JSF 页面相匹配,那么事情会容易得多——然后您可以简单地在 application.xml 中使用相关测试类定义测试 bean。但这就是生活。

祝你好运,让我知道你的效果如何!

If I understand your question correctly, then it ought to be a simple matter of creating special dummy backing beans for your pages, and then creating a test JSF configuration file mapping those beans to the .jspx files. The dummy beans, of course, won't touch any business logic or back-end services -- they'll simply be simple sets of data that will be easy to verify in your tests.

Create an ant script to substitute in your dummy backing beans and the test config file. Run your tests. If you don't want something as heavy as HTTPUnit, and if you're using Spring in your app, look at this blog post for an excellent way to mock up a full web context without a web server. Your tests will probably need to sniff the raw HTML output to verify the results. This is going to be tricky, because IceFaces loves to munge DIV IDs and other relevant parts of the DOM tree that you may want to sniff for. (This alone may be the reason why very few JSF developers try to unit test JSF output.)

Once your tests are verified, swap the regular beans and config file back into the app.

Voila! You've just unit-tested your JSF components.

Mind you, the whole business of swapping out beans and config files is messy. It would be much, much easier if IceFaces used Spring to match backing beans to JSF pages -- then you could simply define the test-beans in an application.xml with the relevant test classes. But such is life.

Good luck, and let me know how it works out for you!

梦过后 2024-08-19 04:14:43

这并不是您所要求的,而是 JSFUnit (它使用 JUnit、Cactus、HtmlUnit 和 HttpUnit )似乎是在 JSF 领域进行测试的重要候选者。您考虑过这个选择吗?也许看看 JSFUnit Wiki 及其 入门指南

请注意,常见问题解答报告了 IceFaces 的一些问题,但它已经很老了(2009 年初),从那时起情况可能已经发生了变化(有一些演示项目,例如 jboss-jsfunit-examples-icefacesicefaces-demo-address 在 JBoss 存储库中,因此可能值得在 JSFUnit 或 IceFaces 邮件上询问确切的状态列表)。

编辑:正如评论中提到的,OP正在寻找不太“高水平”的东西。也许看看页岩测试框架

页岩测试框架提供模拟
对象库,加上基类
用于创建您自己的 JUnit TestCase

包中提供了模拟对象
org.apache.shale.test.mock 为
以下容器 API:

  • JavaServer Faces
  • Servlet

免责声明:Apache Shale 于 2009 年 5 月移至 Attic(即已达到它的生命周期已结束),但我不知道还有任何其他“成熟”的 JSF 模拟框架,所以无论如何我都会提到它(代码仍然存在)。我将以对其他解决方案非常感兴趣的方式关注此线程:)

This is not what exactly what you are asking for but JSFUnit (which uses JUnit, Cactus, HtmlUnit, and HttpUnit) seems to be a serious candidate for testing in the JSF land. Did you consider this option? Maybe have a look at the JSFUnit Wiki and its Getting Started Guide.

Please note that the FAQ is reporting some problems with IceFaces but its pretty old (early 2009) and the situation might have changed since then (there are some demo projects like jboss-jsfunit-examples-icefaces or icefaces-demo-address in JBoss repository so it may be worth to ask the exact status either on JSFUnit or IceFaces mailing lists).

EDIT: As mentioned in a comment, the OP is looking for something less "high level". Maybe have a look at the Shale Test Framework:

The Shale Test Framework provides mock
object libraries, plus base classes
for creating your own JUnit TestCases.

Mock objects are provided in package
org.apache.shale.test.mock for the
following container APIs:

  • JavaServer Faces
  • Servlet

Disclaimer: Apache Shale moved into the Attic in May 2009 (i.e. it has reached its end of life) but I don't know any other "mature" mock framework for JSF so I'm mentioning it anyway (the code is still there). I'll follow this thread with a very high interest for other solutions :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文