zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

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));  

此处需要指定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仓库 立即下载