Spring4.1新特性——Spring缓存框架增强

简介: <p>本文其实不应该算作Spring4.1新特性,该测试框架目前是独立于Spring Framework发展的。<a target="_blank" href="https://github.com/spring-projects/spring-test-htmlunit">Spring MVC Test HtmlUnit</a>提供了<a target="_blank" href="ht

本文其实不应该算作Spring4.1新特性,该测试框架目前是独立于Spring Framework发展的。Spring MVC Test HtmlUnit提供了Spring MVC测试框架HtmlUnit、 WebDriverGeb的集成测试,简化页面自动化测试,利用这些技术可以完成无需启动服务器即可进行页面测试、自动化页面/页面流程测试、Javascript测试、Mock Service提高集成测试速度。本文只会带你使用HtmlUnit和WebDriver进入基本的页面自动化测试一览,不会深入。

 

注:目前不支持JSP页面模板,因为其运行需要web容器支持,请选择如velocity、freemarker等模板引擎。

 

1、定义控制器 

Java代码   收藏代码
  1. @Controller  
  2. public class TestController {  
  3.   
  4.     @RequestMapping("/test1")  
  5.     public String test1(Model model) {  
  6.         return "test1";  
  7.     }  
  8.   
  9.     @RequestMapping("/test2")  
  10.     public String test2(@RequestParam Long id, @RequestParam String name, Model model) {  
  11.         model.addAttribute("id", id);  
  12.         model.addAttribute("name", name);  
  13.         return "test2";  
  14.     }  
  15. }  

2、页面test1.vm

Java代码   收藏代码
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title></title>  
  5.     <link href="/static/css/style.css" rel="stylesheet" type="text/css">  
  6.     <script type="text/javascript" src="/static/js/jquery-1.11.1.min.js"></script>  
  7. </head>  
  8. <body>  
  9.   
  10. <form id="form" action="/test2" method="post">  
  11.     <label for="id">id:</label>  
  12.     <input type="text" id="id" name="id"/><br/>  
  13.   
  14.     <label for="name">name:</label>  
  15.     <input type="text" id="name" name="name"/><br/>  
  16.   
  17.     <input type="submit" value="submit"/>  
  18. </form>  
  19.   
  20. </body>  
  21. </html>  

输入id和name会跳转到test2页面

 

3、页面test2.vm 

Java代码   收藏代码
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title></title>  
  5.     <link href="/static/css/style.css" rel="stylesheet" type="text/css">  
  6.     <script type="text/javascript" src="/static/js/jquery-1.11.1.min.js"></script>  
  7. </head>  
  8. <body>  
  9.   
  10. <form id="form" method="post">  
  11.     <label for="id">id:</label>  
  12.     <input type="text" id="id" name="id" value="${id}"/><br/>  
  13.   
  14.     <label for="name">name:</label>  
  15.     <input type="text" id="name" name="id" value="${name}"/><br/>  
  16.   
  17.     <input id="submit-btn" type="submit" value="submit"/>  
  18.   
  19. </form>  
  20.   
  21. <script type="text/javascript">  
  22.     $("#submit-btn").click(function() {  
  23.         $(this).closest("form").attr("action""/submit");  
  24.         $("#id").val("123");  
  25.         $("#name").val("zhangsan");  
  26.         return false;  
  27.     });  
  28. </script>  
  29.   
  30. </body>  
  31. </html>  

在该页面绑定id和name数据,然后点击submit按钮会重新设置id和name数据。

 

4、使用HtmlUnit测试 

4.1、初始化Web环境

Java代码   收藏代码
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(value = "classpath:spring-mvc.xml")  
  3. @WebAppConfiguration(value = "spring4.1-htmlunit/src/main/webapp")  
  4. public class MockMvcHtmlUnitHelloWorldTest {  
  5.     @Autowired  
  6.     private WebApplicationContext context;  
  7.   
  8.     MockMvc mockMvc;  
  9.     WebClient webClient;  

服务端端配置请参考《Spring MVC测试框架详解——服务端测试》。

 

4.2、创建WebClient

Java代码   收藏代码
  1. @Before  
  2. public void setup() throws Exception {  
  3.     mockMvc = webAppContextSetup(context).build();  
  4.   
  5.     String contextPath = "";  
  6.     webClient = new WebClient();  
  7.     webClient.setWebConnection(new MockMvcWebConnection(mockMvc, contextPath));  
  8. }  

此处需要指定contextPath,如果不指定会把uri路径中的第一个目录作为上下文,如http://localhost/ctx/path,即ctx是上下文,如果不想带上下文需要指定为“”。

 

获取页面1数据,然后设置form表单数据,其操作方式和Javascript DOM类似:

Java代码   收藏代码
  1. HtmlPage page1 = webClient.getPage("http://localhost/test1");  
  2. HtmlForm form1 = page1.getHtmlElementById("form");  
  3. assertEquals("/test2", form1.getAttribute("action"));  
  4.   
  5. page1.getElementById("id").setAttribute("value""1");  
  6. page1.getElementById("name").setAttribute("value""lisi");  

接着提交表单,当前页面会跳转到test2:

Java代码   收藏代码
  1. HtmlPage page2 = form1.getElementsByAttribute("input""type""submit").get(0).click();  
  2. assertEquals("http://localhost/test2", page2.getUrl().toString());  
  3. assertEquals("1", page2.getElementById("id").getAttribute("value"));  
  4. assertEquals("lisi", page2.getElementById("name").getAttribute("value"));  

然后断言该页面的数据是否是上个页面提交过来的。

 

接着点击表单的submit按钮:

Java代码   收藏代码
  1. HtmlForm form2 = page2.getHtmlElementById("form");  
  2. form2.getElementsByAttribute("input""type""submit").get(0).click();  
  3.   
  4. assertEquals("123", page2.getElementById("id").getAttribute("value"));  
  5. assertEquals("zhangsan", page2.getElementById("name").getAttribute("value"));  

点击该按钮后,会重新设置该页面的id和name输入框的数据。

 

整个测试过程还是比较简单的,当然实际页面要比这个复杂很多。

 

5、使用WebDriver进行测试

5.1、初始化Web环境

Java代码   收藏代码
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(value = "classpath:spring-mvc.xml")  
  3. @WebAppConfiguration(value = "spring4.1-htmlunit/src/main/webapp")  
  4. public class MockMvcWebDriverHelloWorldTest {  
  5.     @Autowired  
  6.     private WebApplicationContext context;  
  7.   
  8.     MockMvc mockMvc;  
  9.     MockMvcHtmlUnitDriver webDriver;  

和使用HtmlUnit类似,就不多介绍了。

 

5.2、创建MockMvcHtmlUnitDriver

Java代码   收藏代码
  1. @Before  
  2. public void setup() throws Exception {  
  3.     mockMvc = webAppContextSetup(context).build();  
  4.     boolean enableJavascript = true;  
  5.     String contextPath = "";  
  6.     webDriver = new MockMvcHtmlUnitDriver(mockMvc, enableJavascript);  
  7.     DirectFieldAccessor accessor = new DirectFieldAccessor(webDriver);  
  8.     BeanWrapper wrapper = new BeanWrapperImpl(accessor.getPropertyValue("webClient"));  
  9.     wrapper.setPropertyValue("webConnection"new MockMvcWebConnection(mockMvc, contextPath));  
  10. }  

此处需要使用反射把WebClient的上下文修改掉,否则必须带着上下文,这是目前它考虑不完善的地方。

 

最后测试完成后,关闭WebDriver

Java代码   收藏代码
  1. @After  
  2. public void tearDown() {  
  3.     webDriver.close();  
  4. }  

 

首先请求test1页面,然后查找相应的元素并输入数据

Java代码   收藏代码
  1. webDriver.get("http://localhost/test1");  
  2. WebElement form1 = webDriver.findElement(By.id("form"));  
  3. webDriver.findElement(By.id("id")).sendKeys("1");  
  4. webDriver.findElement(By.id("name")).sendKeys("lisi");  
  5. form1.findElement(By.cssSelector("input[type=submit]")).click();  

WebDriver支持CSS选择器,在实现负责逻辑时非常有用。

 

提交表单后,跳转到test2页面

Java代码   收藏代码
  1. assertEquals("http://localhost/test2", webDriver.getCurrentUrl());  
  2. assertEquals("1", webDriver.findElementById("id").getAttribute("value"));  
  3. assertEquals("lisi", webDriver.findElementById("name").getAttribute("value"));  

 

接着点击test2页面的submit按钮

Java代码   收藏代码
  1. webDriver.findElementByCssSelector("#form input[type=submit]").click();  
  2.   
  3. assertEquals("/submit", webDriver.findElementById("form").getAttribute("action"));  
  4. assertEquals("123", webDriver.findElementById("id").getAttribute("value"));  
  5. assertEquals("zhangsan", webDriver.findElementById("name").getAttribute("value"));  

   

整个测试过程和HtmlUnit类似,不过API更易用。

 

从目前来看,Spring MVC Test HtmlUnit框架本身只是起到了Spring MVC测试框架和HtmlUnit和WebDriver之间的粘合剂,把它们结合起来,如果没有Spring MVC测试框架的强大,这种融合还是比较麻烦的。


本文转自http://jinnianshilongnian.iteye.com/blog/2108400

目录
相关文章
|
4月前
|
安全 Java Ruby
我尝试了所有后端框架 — — 这就是为什么只有 Spring Boot 幸存下来
作者回顾后端开发历程,指出多数框架在生产环境中难堪重负。相比之下,Spring Boot凭借内置安全、稳定扩展、完善生态和企业级支持,成为构建高可用系统的首选,真正经受住了时间与规模的考验。
332 2
|
3月前
|
安全 前端开发 Java
《深入理解Spring》:现代Java开发的核心框架
Spring自2003年诞生以来,已成为Java企业级开发的基石,凭借IoC、AOP、声明式编程等核心特性,极大简化了开发复杂度。本系列将深入解析Spring框架核心原理及Spring Boot、Cloud、Security等生态组件,助力开发者构建高效、可扩展的应用体系。(238字)
|
5月前
|
XML JSON Java
Spring框架中常见注解的使用规则与最佳实践
本文介绍了Spring框架中常见注解的使用规则与最佳实践,重点对比了URL参数与表单参数的区别,并详细说明了@RequestParam、@PathVariable、@RequestBody等注解的应用场景。同时通过表格和案例分析,帮助开发者正确选择参数绑定方式,避免常见误区,提升代码的可读性与安全性。
|
3月前
|
消息中间件 缓存 Java
Spring框架优化:提高Java应用的性能与适应性
以上方法均旨在综合考虑Java Spring 应该程序设计原则, 数据库交互, 编码实践和系统架构布局等多角度因素, 旨在达到高效稳定运转目标同时也易于未来扩展.
161 8
|
4月前
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
679 5
|
4月前
|
监控 Kubernetes Cloud Native
Spring Batch 批处理框架技术详解与实践指南
本文档全面介绍 Spring Batch 批处理框架的核心架构、关键组件和实际应用场景。作为 Spring 生态系统中专门处理大规模数据批处理的框架,Spring Batch 为企业级批处理作业提供了可靠的解决方案。本文将深入探讨其作业流程、组件模型、错误处理机制、性能优化策略以及与现代云原生环境的集成方式,帮助开发者构建高效、稳定的批处理系统。
501 1
|
4月前
|
存储 缓存 Java
Spring中@Cacheable、@CacheEvict以及其他缓存相关注解的实用介绍
缓存是提升应用性能的重要技术,Spring框架提供了丰富的缓存注解,如`@Cacheable`、`@CacheEvict`等,帮助开发者简化缓存管理。本文介绍了如何在Spring中配置缓存管理器,使用缓存注解优化数据访问,并探讨了缓存的最佳实践,以提升系统响应速度与可扩展性。
345 0
Spring中@Cacheable、@CacheEvict以及其他缓存相关注解的实用介绍
|
6月前
|
安全 Java 微服务
Java 最新技术和框架实操:涵盖 JDK 21 新特性与 Spring Security 6.x 安全框架搭建
本文系统整理了Java最新技术与主流框架实操内容,涵盖Java 17+新特性(如模式匹配、文本块、记录类)、Spring Boot 3微服务开发、响应式编程(WebFlux)、容器化部署(Docker+K8s)、测试与CI/CD实践,附完整代码示例和学习资源推荐,助你构建现代Java全栈开发能力。
692 0
|
5月前
|
Cloud Native Java API
Java Spring框架技术栈选和最新版本及发展史详解(截至2025年8月)-优雅草卓伊凡
Java Spring框架技术栈选和最新版本及发展史详解(截至2025年8月)-优雅草卓伊凡
1036 0
|
6月前
|
缓存 安全 Java
第五章 Spring框架
Spring IOC(控制反转)通过工厂模式管理对象的创建与生命周期,DI(依赖注入)则让容器自动注入所需对象,降低耦合。常见注解如@Component、@Service用于声明Bean,@Autowired用于注入。Bean默认单例,作用域可通过@Scope配置,如prototype、request等。Spring通过三级缓存解决循环依赖问题,但构造函数循环依赖需用@Lazy延迟加载。AOP通过动态代理实现,用于日志、事务等公共逻辑。事务通过@Transactional实现,需注意异常处理及传播行为。
105 0