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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.