返回介绍

13.7 修改集成测试中 Web 根路径

发布于 2025-04-22 20:10:05 字数 3059 浏览 0 评论 0 收藏

默认情况下,使用 @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());
  } 
} 

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。