发布于 2024-11-26 12:34:03 字数 373 浏览 0 评论 0原文

I am going to use a template engine in Java (probably FreeMarker). Now I wonder how to write a unit test for it.

I can prepare an expected output as a text file but I will have to change it manually whenever I change the template. I would like to parse the output using the template to get the model data and it with the original model.我可以这样做吗?

I am going to use a template engine in Java (probably FreeMarker). Now I wonder how to write a unit test for it.

I can prepare an expected output as a text file but I will have to change it manually whenever I change the template. I would like to parse the output using the template to get the model data and it with the original model. Can I do that?

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

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

发布评论

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

评论(1

梦一生花开无言 2024-12-03 12:34:03

continue

Depends on what your template will output. Of course if it produced a well formed format like XML or JSON you could easily parse it so I'm left to assume it's probably HTML. You might be able to use JTidy to parse it into a DOM model that can be used from Java.

http://jtidy.sourceforge.net/

Another option might be to use good old fashion grep routine, then build primitives like assertContains, assertDoesNotContain, etc. I have a JSON library I write unit tests for and I took that route to just do old fashion grep, and it's worked well in that it catches bugs. I worried about parsing the JSON code directly because I'd be using the code I wrote in my tests to test that very code. Your case might be different so parsing to a DOM model might make more sense.

The big thing, to consider, is you're testing the view. So as things change visually you might have a very hard time keeping your tests up to date. The view gets changed a lot in most programs because your client, PM, person in control of what you're doing, changes their mind frequently.

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