- 内容提要
- 作者简介
- 译者简介
- 前言
- HTTP
- Servlet 和 JSP
- 下载 Spring 或使用 STS 与 Maven/Gradle
- 手动下载 Spring
- 使用 STS 和 Maven/Gradle
- 下载 Spring 源码
- 本书内容简介
- 下载示例应用
- 第 1 章Spring 框架
- 第 2 章模型 2 和 MVC 模式
- 第 3 章Spring MVC 介绍
- 第 4 章基于注解的控制器
- 第 5 章数据绑定和表单标签库
- 第 6 章转换器和格式化
- 第 7 章验证器
- 第 8 章表达式语言
- 第 9 章JSTL
- 第 10 章国际化
- 第 11 章上传文件
- 第 12 章下载文件
- 第 13 章应用测试
- 附录 A Tomcat
- 附录 B Spring Tool Suite 和 Maven
- 附录 C Servlet
- 附录 D JavaServer Pages
- 附录 E 部署描述符
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
13.7 修改集成测试中 Web 根路径
默认情况下,使用 @WebAppConfiguration 注解的 Spring 集成测试类将使用相对于项目目录的/src/main/webapp 目录作为根目录。这是一个 Maven 标准布局。如果你不使用 STS,而是使用不依赖于 Maven 的 IDE(如 Eclipse 或 NetBeans),在某些罕见情况下,这可能会出现问题。例如,如果你需要使用 ServletContext.getRealPath() 的值,你将获得相对于/src/main/webapp 的值,而不是相对于你的 Web 应用程序目录的值。
然而,你也不能使用/src/main/webapp/WEB-INF/classes 目录作为 Eclipse 中的输出目录,因为这意味着将其嵌套在源目录中,而在 Eclipse 是禁止这样做的。好在,可以将实际应用程序目录传递到 @WebAppConfiguration 来修复此问题。例如,以下将让任何 WebApplicationContext 使用/ approotdir 作为应用程序目录。
@WebAppConfiguration("/approotdir")清单 13.29 展示了一个 WebAppController 类,其有一个方法返回在线资源的真实路径。
清单 13.29 WebAppController 类
package controller;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class WebAppController {
// ServletContext cannot be used as a method parameter, inject
// instead.
@Autowired
private ServletContext servletContext;
@RequestMapping(value="/getWebAppDir")
public String getWebAppDirectory(Model model) {
model.addAttribute("webAppDir", servletContext.getRealPath("/"));
return "success";
}
}清单 13.30 展示了一个用于集成测试 WebAppController 类的测试类。注意传递给 @WebAppConfiguration 注解的值。
清单 13.30 WebAppControllerTest 类
package com.example.controller;
import static org.springframework.test.web.servlet.request.
→ MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.
→ MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.
→ MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.
→ MockMvcResultMatchers.status;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setUp.MockMvcBuilders;
import org .springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration("/webapp")
@ContextConfiguration("test-config.xml")
public class WebAppControllerTest {
@Autowired
private WebApplicationContext webAppContext;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc =
MockMvcBuilders.webAppContextSetup(webAppContext).build();
}
@After
public void cleanUp() {
}
@Test
public void testWebAppDir() throws Exception {
mockMvc.perform(get("/getWebAppDir"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("webAppDir"))
.andDo(print());
}
} 绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论