2015-08-28 5 views
7

द्वारा यूनिट टेस्ट स्थैतिक संसाधनों की सेवा कैसे करें मैं एक नई स्प्रिंग एमवीसी परियोजना का परीक्षण करने की कोशिश कर रहा हूं। मेरे पास इंडेक्स.जेएसपी की सेवा करने वाले होम कंट्रोलर के लिए पासिंग टेस्ट है। मैं अपने WebConfig में ResourceHandlerRegistry द्वारा प्रदत्त स्थैतिक संसाधनों की सेवा करने का प्रयास कर रहा हूं।स्प्रिंग रिसोर्स हैंडलर रजिस्ट्री

यह सही तरीके से कैसे करें इस पर कोई सुझाव? यदि आप एक प्राप्त "/ संसाधन/सीएसएस/परीक्षण करने

2015-08-28 12:53:09 WARN PageNotFound:1136 - No mapping found for HTTP request with URI [resources/css/test.css] in DispatcherServlet with name '' 

MockHttpServletRequest: 
    HTTP Method = GET 
    Request URI = resources/css/test.css 
     Parameters = {} 
     Headers = {} 

     Handler: 
      Type = null 

      Async: 
    Async started = false 
    Async result = null 

    Resolved Exception: 
      Type = null 

    ModelAndView: 
     View name = null 
      View = null 
      Model = null 

     FlashMap: 

MockHttpServletResponse: 
      Status = 404 
    Error message = null 
     Headers = {} 
    Content type = null 
      Body = 
    Forwarded URL = null 
    Redirected URL = null 
     Cookies = [] 

उत्तर

0
एक मानक विन्यास के साथ

तरह

<mvc:resources mapping="/resources/**" location="/resources/"/> 

: प्रिंट से

@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")); 
} 

} 

आउटपुट(): नीचे मेरी कोड है। सीएसएस "संसाधनों/सीएसएस/test.css के बजाय" आप सीएसएस फ़ाइल वापस प्राप्त करेंगे। आप आगे की उम्मीद क्यों करते हैं?

संबंधित मुद्दे