小编fil*_*lup的帖子

如何单元测试Spring ResourceHandlerRegistry提供的静态资源

我正在尝试对一个新的Spring MVC项目进行单元测试.我有一个服务于index.jsp的家庭控制器的通过测试.我正在尝试在WebConfig中测试由ResourceHandlerRegistry提供的静态资源.

有关如何正确执行此操作的任何提示?以下是我的代码:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {WebConfig.class})
public class HomeControllerTest {

private MockMvc mockMvc;

@Autowired
private WebApplicationContext webApplicationContext;

@Before
public void setUp() throws Exception {
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
                            .build();
}

@Test
public void testGetHomepage() throws Exception { //passing :)
    this.mockMvc.perform(get("/"))
            .andExpect(status().isOk())
            .andExpect(view().name("index"))
            .andExpect(forwardedUrl("/WEB-INF/pages/index.jsp"));
}

@Test
public void testGetResources() throws Exception {   // TEST FAILS :( with 404
    this.mockMvc.perform(get("resources/css/test.css"))
            .andDo(print())
            .andExpect(status().isOk())
            .andExpect(forwardedUrl("/resources/css/test.css"));
}

}
Run Code Online (Sandbox Code Playgroud)

print()的输出:

    2015-08-28 12:53:09 WARN  PageNotFound:1136 - No mapping found for HTTP request with URI [resources/css/test.css] in …
Run Code Online (Sandbox Code Playgroud)

java spring unit-testing spring-mvc

7
推荐指数
1
解决办法
1080
查看次数

标签 统计

java ×1

spring ×1

spring-mvc ×1

unit-testing ×1