测试Thymeleaf表单/ Spring MVC Controller交互的最佳方法 [英] Best way to test Thymeleaf form / Spring MVC Controller interaction
问题描述
我正在使用Thymeleaf& amp; Spring MVC,我遇到了代码中的一个错误,有人将Spring MVC模型绑定到2个不同的HTML表单字段:
I am working on a Spring Boot app using Thymeleaf & Spring MVC, and I came across a bug in the code where someone had bound the Spring MVC model to 2 different HTML form fields:
<input th:field="*{userModel.name}" type="text" />
<input id="name" th:field="*{userModel.name}" type="hidden" />
这导致控制器模型中的name字段被设置为逗号的字符串分开的值。 史蒂夫,史蒂夫,例如。我解决了这个问题,但我想知道为此编写回归测试的最简单方法。有一个Spring MVC测试框架,我可以使用就像在这篇博文上,但我真正想要测试的是渲染模板和控制器之间的交互,而不仅仅是控制器。
This was causing the name field in the model of the controller to get set to a string of comma-separated values. "Steve,Steve" for example. I fixed the problem, but I'm wondering the simplest way to write a regression test for this. There is a Spring MVC testing framework, which I could use as on this blog post, but what I really want to test is the interaction between the rendered template and the controller, not just the controller.
我可以使用硒测试,但我最近读过这个Martin Fowler bliki /文章(blikticle?),其中他说:
I could use a selenium test, but I recently read this Martin Fowler bliki/article (blikticle?) in which he says:
特别是一个常见的问题是团队将端到端测试,UI测试和面向客户的测试的概念混为一谈。这些都是正交特征。
In particular a common problem is that teams conflate the concepts of end-to-end tests, UI tests, and customer facing tests. These are all orthogonal characteristics.
我认为这是一个很好的观点。我想要写的是UI组件(集成?)测试。比装载整个页面小的东西。只测试表单生成和提交的东西。
I think this is a great point. What I would like to write is a UI component (integration?) test, I think. Something smaller than loading up the whole page. Something only testing the form generation and submission.
我的另一个想法是,这种错误可能最好通过静态分析工具捕获,但这有点超出我的范围。
Another idea I had was that this sort of bug might be best caught through a static-analysis tool, but that's a bit beyond my scope.
在这个项目中,我意识到Spring MVC和HTML表单之间的交互是错误的常见位置,所以最好有一种测试这些互动的方法。
In this project, I've realized that the interactions between Spring MVC and the HTML forms are a common place for errors, so it would be nice to have a way to test just these interactions.
编辑:
进一步考虑,我认为这些是我在测试中想要的步骤:
Upon further consideration, I think these are the steps I want in my test:
- 选择
< form>
标记出模板的百万美元模板并进行渲染,并在模型上传递适当的数据 - 可能以编程方式编辑渲染的表单值(来自Java)以模拟JavaScript交互。
- 根据呈现的表单生成浏览器的http POST请求。
- 使用Spring用于将POST请求转换为控制器的任何内容model参数
- 调用控制器并验证结果,或者只是断言模型是否正确创建
- Select
<form>
tag out of a template's thymeleaf template and render it, passing in appropriate data on the model - Possibly programmatically edit the rendered form values (from Java) to simulate JavaScript interactions.
- Generate a browser's http POST request based on rendered form.
- Use whatever Spring uses to convert POST request to the controller's model parameter
- Either call the controller and verify the results, or just assert that the model was created correctly
我想我可以用Thymeleaf的片段选择器做#1,或者将我的表格重构成一个单独的模板。 #2我可以轻松地使用JSoup。这是#3& #4,我不知道该怎么做。 #3我也许可以写自己,这取决于HttpServletRequest模拟的工作方式。 #4看起来好像它必须在Spring的某个地方可用,但我是Spring的新手。
I think I may able to do #1 with Thymeleaf's fragment selectors or by refactoring my form out into a separate template. #2 I can do easily with JSoup. It's #3 & #4 that I'm not sure how to do. #3 I might be able to write myself maybe, depending on how the HttpServletRequest mocks work. #4 seems like it's gotta be available in Spring somewhere but I'm new to Spring.
更新:
查看 WebDataBinder 作为#4的可能解决方案。
Looking at the WebDataBinder as a possible solution for #4.
推荐答案
我认为你应该测试一下功能和获得控制器直接调用控制器的良好路径覆盖,模拟底层服务。然后,您可以测试模型是否正确填充。
I think you should test the functionality and to get good path-coverage of the controller calling the controller directly, mocking the underlying services. Then you can test if the model is correctly filled.
您可以使用Thymeleaf测试基础架构测试复杂模板。
You can test a complex template with Thymeleaf-test infrastructure.
然而,在一天结束时,一切都必须协同工作。这个测试应该用selenium来完成,以检查变量名匹配(所以你的回归)。通常,您只需要为每个页面进行一些测试。如果你有很多JS,你应该用buster.js测试复杂的JS功能。硒的htmlunitdriver非常快,所以我认为性能不应该是一个大问题。但是不要用硒测试所有控制器或服务路径。
However, at the end of the day, everything must work together. This test should be done with selenium, to check the the variable-names match (So your regression). Normally you should only need a few tests for each page. If you have a lot of JS you should test complex JS functionality with buster.js. The htmlunitdriver of selenium is really fast, so I assume that the performance shouldn't be a big issue. But don't test all controller- or service-paths with selenium.
这篇关于测试Thymeleaf表单/ Spring MVC Controller交互的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!