Spring4.1新特性——Spring缓存框架增强
2023-09-14 08:57:15 时间
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
@RequestMapping("/test2") public String test2(@RequestParam Long id, @RequestParam String name, Model model) { model.addAttribute("id", id); model.addAttribute("name", name); return "test2"; }
title /title link href="/static/css/style.css" rel="stylesheet" type="text/css" script type="text/javascript" src="/static/js/jquery-1.11.1.min.js" /script /head body form id="form" action="/test2" method="post" label for="id" id: /label input type="text" id="id" name="id"/ br/ label for="name" name: /label input type="text" id="name" name="name"/ br/ input type="submit" value="submit"/ /form /body /html
title /title link href="/static/css/style.css" rel="stylesheet" type="text/css" script type="text/javascript" src="/static/js/jquery-1.11.1.min.js" /script /head body form id="form" method="post" label for="id" id: /label input type="text" id="id" name="id" value="${id}"/ br/ label for="name" name: /label input type="text" id="name" name="id" value="${name}"/ br/ input id="submit-btn" type="submit" value="submit"/ /form script type="text/javascript" $("#submit-btn").click(function() { $(this).closest("form").attr("action", "/submit"); $("#id").val("123"); $("#name").val("zhangsan"); return false; }); /script /body /html
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(value = "classpath:spring-mvc.xml") @WebAppConfiguration(value = "spring4.1-htmlunit/src/main/webapp") public class MockMvcHtmlUnitHelloWorldTest { @Autowired private WebApplicationContext context; MockMvc mockMvc; WebClient webClient;
public void setup() throws Exception { mockMvc = webAppContextSetup(context).build(); String contextPath = ""; webClient = new WebClient(); webClient.setWebConnection(new MockMvcWebConnection(mockMvc, contextPath));
HtmlPage page1 = webClient.getPage("http://localhost/test1"); HtmlForm form1 = page1.getHtmlElementById("form"); assertEquals("/test2", form1.getAttribute("action")); page1.getElementById("id").setAttribute("value", "1"); page1.getElementById("name").setAttribute("value", "lisi");
HtmlPage page2 = form1.getElementsByAttribute("input", "type", "submit").get(0).click(); assertEquals("http://localhost/test2", page2.getUrl().toString()); assertEquals("1", page2.getElementById("id").getAttribute("value")); assertEquals("lisi", page2.getElementById("name").getAttribute("value"));
HtmlForm form2 = page2.getHtmlElementById("form"); form2.getElementsByAttribute("input", "type", "submit").get(0).click(); assertEquals("123", page2.getElementById("id").getAttribute("value")); assertEquals("zhangsan", page2.getElementById("name").getAttribute("value"));
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(value = "classpath:spring-mvc.xml") @WebAppConfiguration(value = "spring4.1-htmlunit/src/main/webapp") public class MockMvcWebDriverHelloWorldTest { @Autowired private WebApplicationContext context; MockMvc mockMvc; MockMvcHtmlUnitDriver webDriver;
public void setup() throws Exception { mockMvc = webAppContextSetup(context).build(); boolean enableJavascript = true; String contextPath = ""; webDriver = new MockMvcHtmlUnitDriver(mockMvc, enableJavascript); DirectFieldAccessor accessor = new DirectFieldAccessor(webDriver); BeanWrapper wrapper = new BeanWrapperImpl(accessor.getPropertyValue("webClient")); wrapper.setPropertyValue("webConnection", new MockMvcWebConnection(mockMvc, contextPath));
webDriver.get("http://localhost/test1"); WebElement form1 = webDriver.findElement(By.id("form")); webDriver.findElement(By.id("id")).sendKeys("1"); webDriver.findElement(By.id("name")).sendKeys("lisi"); form1.findElement(By.cssSelector("input[type=submit]")).click();
assertEquals("http://localhost/test2", webDriver.getCurrentUrl()); assertEquals("1", webDriver.findElementById("id").getAttribute("value")); assertEquals("lisi", webDriver.findElementById("name").getAttribute("value"));
assertEquals("/submit", webDriver.findElementById("form").getAttribute("action")); assertEquals("123", webDriver.findElementById("id").getAttribute("value")); assertEquals("zhangsan", webDriver.findElementById("name").getAttribute("value"));
【Spring】Spring 6 新特性一一HTTP Interface Spring 6 的第一个 GA 版本发布了,其中带来了一个新的特性——HTTP Interface。 这个新特性,可以让开发者将 HTTP 服务,定义成一个包含特定注解标记的方法的 Java 接口,然后通过对接口方法的调用,完成 HTTP 请求。下面我们参考官方文档来完成一个 Demo。
Spring Boot 3.0发布,最低支持Java 17,新特性介绍 Spring Boot 3.0正式发布,距离Spring 2.0发布过去了4年半时间。最低支持Java 17,Spring Framework 6.0.本文详细介绍Spring Boot 3.0 新特性。
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载
@RequestMapping("/test2") public String test2(@RequestParam Long id, @RequestParam String name, Model model) { model.addAttribute("id", id); model.addAttribute("name", name); return "test2"; }
title /title link href="/static/css/style.css" rel="stylesheet" type="text/css" script type="text/javascript" src="/static/js/jquery-1.11.1.min.js" /script /head body form id="form" action="/test2" method="post" label for="id" id: /label input type="text" id="id" name="id"/ br/ label for="name" name: /label input type="text" id="name" name="name"/ br/ input type="submit" value="submit"/ /form /body /html
title /title link href="/static/css/style.css" rel="stylesheet" type="text/css" script type="text/javascript" src="/static/js/jquery-1.11.1.min.js" /script /head body form id="form" method="post" label for="id" id: /label input type="text" id="id" name="id" value="${id}"/ br/ label for="name" name: /label input type="text" id="name" name="id" value="${name}"/ br/ input id="submit-btn" type="submit" value="submit"/ /form script type="text/javascript" $("#submit-btn").click(function() { $(this).closest("form").attr("action", "/submit"); $("#id").val("123"); $("#name").val("zhangsan"); return false; }); /script /body /html
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(value = "classpath:spring-mvc.xml") @WebAppConfiguration(value = "spring4.1-htmlunit/src/main/webapp") public class MockMvcHtmlUnitHelloWorldTest { @Autowired private WebApplicationContext context; MockMvc mockMvc; WebClient webClient;
public void setup() throws Exception { mockMvc = webAppContextSetup(context).build(); String contextPath = ""; webClient = new WebClient(); webClient.setWebConnection(new MockMvcWebConnection(mockMvc, contextPath));
此处需要指定contextPath,如果不指定会把uri路径中的第一个目录作为上下文,如http://localhost/ctx/path,即ctx是上下文,如果不想带上下文需要指定为“”。
获取页面1数据,然后设置form表单数据,其操作方式和Javascript DOM类似:
HtmlPage page1 = webClient.getPage("http://localhost/test1"); HtmlForm form1 = page1.getHtmlElementById("form"); assertEquals("/test2", form1.getAttribute("action")); page1.getElementById("id").setAttribute("value", "1"); page1.getElementById("name").setAttribute("value", "lisi");
HtmlPage page2 = form1.getElementsByAttribute("input", "type", "submit").get(0).click(); assertEquals("http://localhost/test2", page2.getUrl().toString()); assertEquals("1", page2.getElementById("id").getAttribute("value")); assertEquals("lisi", page2.getElementById("name").getAttribute("value"));
HtmlForm form2 = page2.getHtmlElementById("form"); form2.getElementsByAttribute("input", "type", "submit").get(0).click(); assertEquals("123", page2.getElementById("id").getAttribute("value")); assertEquals("zhangsan", page2.getElementById("name").getAttribute("value"));
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(value = "classpath:spring-mvc.xml") @WebAppConfiguration(value = "spring4.1-htmlunit/src/main/webapp") public class MockMvcWebDriverHelloWorldTest { @Autowired private WebApplicationContext context; MockMvc mockMvc; MockMvcHtmlUnitDriver webDriver;
public void setup() throws Exception { mockMvc = webAppContextSetup(context).build(); boolean enableJavascript = true; String contextPath = ""; webDriver = new MockMvcHtmlUnitDriver(mockMvc, enableJavascript); DirectFieldAccessor accessor = new DirectFieldAccessor(webDriver); BeanWrapper wrapper = new BeanWrapperImpl(accessor.getPropertyValue("webClient")); wrapper.setPropertyValue("webConnection", new MockMvcWebConnection(mockMvc, contextPath));
此处需要使用反射把WebClient的上下文修改掉,否则必须带着上下文,这是目前它考虑不完善的地方。
最后测试完成后,关闭WebDriver
webDriver.get("http://localhost/test1"); WebElement form1 = webDriver.findElement(By.id("form")); webDriver.findElement(By.id("id")).sendKeys("1"); webDriver.findElement(By.id("name")).sendKeys("lisi"); form1.findElement(By.cssSelector("input[type=submit]")).click();
assertEquals("http://localhost/test2", webDriver.getCurrentUrl()); assertEquals("1", webDriver.findElementById("id").getAttribute("value")); assertEquals("lisi", webDriver.findElementById("name").getAttribute("value"));
assertEquals("/submit", webDriver.findElementById("form").getAttribute("action")); assertEquals("123", webDriver.findElementById("id").getAttribute("value")); assertEquals("zhangsan", webDriver.findElementById("name").getAttribute("value"));
从目前来看,Spring MVC Test HtmlUnit框架本身只是起到了Spring MVC测试框架和HtmlUnit和WebDriver之间的粘合剂,把它们结合起来,如果没有Spring MVC测试框架的强大,这种融合还是比较麻烦的。
本文转自http://jinnianshilongnian.iteye.com/blog/2108400
【Spring】Spring 6 新特性一一HTTP Interface Spring 6 的第一个 GA 版本发布了,其中带来了一个新的特性——HTTP Interface。 这个新特性,可以让开发者将 HTTP 服务,定义成一个包含特定注解标记的方法的 Java 接口,然后通过对接口方法的调用,完成 HTTP 请求。下面我们参考官方文档来完成一个 Demo。
Spring Boot 3.0发布,最低支持Java 17,新特性介绍 Spring Boot 3.0正式发布,距离Spring 2.0发布过去了4年半时间。最低支持Java 17,Spring Framework 6.0.本文详细介绍Spring Boot 3.0 新特性。
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载
相关文章
- spring 之 lazy-init Autowired depends-on
- ssh(Spring+Spring mvc+hibernate)——showDept.jsp
- Spring异常解决 java.lang.NullPointerException,配置spring管理hibernate时出错
- spring boot:使用spring cache+caffeine做进程内缓存(本地缓存)(spring boot 2.3.1)
- spring 学习3-Spring AOP
- spring boot配置静态资源及其缓存
- spring boot2X集成spring cloud config
- Spring boot后台搭建二为Shiro权限控制添加缓存
- 深入理解Spring Redis的使用 (八)、Spring Redis实现 注解 自动缓存
- Spring异常解决 java.lang.NullPointerException,配置spring管理hibernate时出错
- [Kotlin spring boot] Work with database
- Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
- Spring+EhCache缓存实例(详细讲解+源码下载)
- spring boot:使用spring cache+caffeine做进程内缓存(本地缓存)(spring boot 2.3.1)
- Spring三级缓存
- Spring XSD validation cache issue
- Spring实战3:装配bean的进阶知识
- Atitit spring注解事务的demo与代码说明 目录 1.1. Spring框架中,要如何实现事务?有一个注解,@EnableTransactionManagement1 1.2. 事务管理
- Atitit spring单元测试 注解 获取服务名 Spring文件单独放在一个文件夹,去掉dubbo配置,方便测试 里面包含的mybatis 找不到,只好设置成相对于class绝对路径可以了
- Redis整合Spring结合使用缓存实例
- 学习Spring Boot:(二十三)Spring Boot 中使用 Docker
- 学习Spring Boot:(十九)Shiro 中使用缓存
- 阿里云服务器 配置 tomcat 发布spring boot项目 的具体操作 【使用公网ip】
- Spring框架基础
- 007-spring cache-缓存实现-02-springboot ehcahe2、ehcache3实现、springboot caffeine实现
- 005-spring cache-原理、缓存AOP机制、Spring Cache抽象集成机制、springboot自动配置机制
- 004-spring cache-声明性的基于XML的缓存
- Spring Boot云配置服务器
- 服务降级是什么?Spring Cloud如何实现?
- Spring自动装配Bean
- 【工作流Activiti7】2、Activiti7 与 Spring Boot 及 Spring Security 整合