2013-12-18 55 views
0

我正在用java編寫MVC應用程序。目前我正在爲我的RESTfullController編寫一個測試。當我嘗試get()試圖工作時出現問題。我找回了一個404而不是202.爲了解決這個問題,我投入了大量的時間,但是不能。我會後我的控制器,在這裏Restfull控制器測試leasds到錯誤404是200.沒有錯字錯誤

package com.bestbuy.supportspace.videolibrary.web; 

import com.bestbuy.supportspace.videolibrary.config.WebAppInitializer; 
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.web.context.WebApplicationContext; 

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; 


/** 
* User: nikhil.thakur 
* Date: 12/18/13 
*/ 
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = {WebAppInitializer.class}) 
@WebAppConfiguration 
public class RestfullControllerTest { 

    private MockMvc mockMvc; 


    @SuppressWarnings("SpringJavaAutowiringInspection") 
    @Autowired 
    protected WebApplicationContext wac; 

    @Before 
    public void setup() { 
     this.mockMvc = webAppContextSetup(this.wac).build(); 
    } 

    @Test 
    public void testFindAll() throws Exception { 
     mockMvc.perform(get("/rest/videos/")) 
       .andExpect(status().isOk()); 
//    .andExpect(jsonPath("$", hasSize(4))); 

    } 

} 

,我試圖測試控制器是 包com.bestbuy.supportspace.videolibrary.web的ControllerTest;

import com.bestbuy.supportspace.videolibrary.entity.Video; 
    import com.bestbuy.supportspace.videolibrary.services.LookupService; 
    import com.bestbuy.supportspace.videolibrary.services.VideoService; 
    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.stereotype.Controller; 
    import org.springframework.ui.Model; 
    import org.springframework.util.Assert; 
    import org.springframework.web.bind.annotation.*; 

    import java.util.ArrayList; 
    import java.util.List; 

    @Controller 
    @RequestMapping("/") 
    public class RESTfulController { 

     @Autowired 
     LookupService lookupService; 

     @Autowired 
     VideoService videoService; 

     @RequestMapping("keywords") 
     public 
     @ResponseBody 
     String findAllKeywords() { 
      return lookupService.findAllKeywords().toString(); 
     } 

     @RequestMapping("subjects") 
     public 
     @ResponseBody 
     String findAllSubjects() { 
      return lookupService.findAllSubjects().toString(); 
     } 

     @RequestMapping("presenters") 
     public 
     @ResponseBody 
     String findAllPresenters() { 
      return lookupService.findAllPresenters().toString(); 
     } 

     @RequestMapping(value = "videos", method = RequestMethod.GET, produces = "application/json") 
     public 
     @ResponseBody 
     String findAllVideos() { 
      return videoService.findAll().toString(); 
     } 

     @RequestMapping(value = "videos/findVideosByKeywordsIdIn", method = RequestMethod.GET, produces = "application/json") 
     public 
     @ResponseBody 
     String findVideosByKeywordsIdIn(@RequestParam(value = "keywords") String keywords) { 

      return videoService.findVideosByKeywordsIdIn(getListOfIntegers(keywords)).toString(); 

     } 

     @RequestMapping(value = "videos/findByPresentersIdIn", method = RequestMethod.GET, produces = "application/json") 
     public 
     @ResponseBody 
     String findByPresentersIdIn(@RequestParam(value = "presenters") String presenters) { 

      return videoService.findByPresentersIdIn(getListOfIntegers(presenters)).toString(); 

     } 


     @RequestMapping(value = "videos/findBySubjectsIdIn", method = RequestMethod.GET, produces = "application/json") 
     public 
     @ResponseBody 
     String findBySubjectsIdIn(@RequestParam(value = "subjects") String subjects) { 

      return videoService.findBySubjectsIdIn(getListOfIntegers(subjects)).toString(); 

     } 

     @RequestMapping(value = "videos/findByKeywordsIdInAndPresentersIdInAndSubjectsIdIn", method = RequestMethod.GET, produces = "application/json") 
     public 
     @ResponseBody 
     String findByKeywordsIdInAndPresentersIdInAndSubjectsIdIn(@RequestParam(value = "keywords") String keywords, @RequestParam(value = "presenters") String presenters, @RequestParam(value = "subjects") String subjects) { 


      return videoService.findByKeywordsIdInAndPresentersIdInAndSubjectsIdIn(getListOfIntegers(keywords), getListOfIntegers(presenters), getListOfIntegers(subjects)).toString(); 

     } 

     @RequestMapping(value = "videos/findByKeywordsIdInAndPresentersIdIn", method = RequestMethod.GET, produces = "application/json") 
     public 
     @ResponseBody 
     String findByKeywordsIdInAndPresentersIdIn(@RequestParam(value = "keywords") String keywords, @RequestParam(value = "presenters") String presenters) { 

      return videoService.findByKeywordsIdInAndPresentersIdIn(getListOfIntegers(keywords),getListOfIntegers(presenters)).toString(); 

     } 

     @RequestMapping(value = "videos/findBySubjectsIdInAndPresentersIdIn", method = RequestMethod.GET, produces = "application/json") 
     public 
     @ResponseBody 
     String findBySubjectsIdInAndPresentersIdIn(@RequestParam(value = "subjects") String subjects, @RequestParam(value = "presenters") String presenters) { 

      return videoService.findBySubjectsIdInAndPresentersIdIn(getListOfIntegers(subjects),getListOfIntegers(presenters)).toString(); 

     } 

     @RequestMapping(value = "videos/findBySubjectsIdInAndKeywordsIdIn", method = RequestMethod.GET, produces = "application/json") 
     public 
     @ResponseBody 
     String findBySubjectsIdInAndKeywordsIdIn(@RequestParam(value = "subjects") String subjects, @RequestParam(value = "keywords") String keywords) { 

      return videoService.findBySubjectsIdInAndKeywordsIdIn(getListOfIntegers(subjects),getListOfIntegers(keywords)).toString(); 

     } 

     @RequestMapping(value = "video/{videoId}", method = RequestMethod.GET, produces = "application/json") 
     public 
     @ResponseBody 
     String readVideo(@PathVariable Integer videoId, Model model) { 
      Assert.notNull(videoId); 
      return videoService.findOne(videoId).toString(); 
     } 

     @RequestMapping(value = "video/{videoId}", method = RequestMethod.PUT, consumes = "application/json", produces = "application/json") 
     public 
     @ResponseBody 
     String updateVideo(@RequestBody Video video, @PathVariable Integer videoId, Model model) { 
      Assert.notNull(video); 
      return videoService.save(video).toString(); 
     } 

     @RequestMapping(value = "video", method = RequestMethod.POST, consumes = "application/json", produces = "application/json") 
     public 
     @ResponseBody 
     String createVideo(@RequestBody Video video, Model model) { 
      Assert.notNull(video); 
      return videoService.save(video).toString(); 
     } 

     private List<Integer> getListOfIntegers(String keywords) { 
      List<Integer> ids = new ArrayList<Integer>(); 
      for (String id : keywords.split(";")) { 
       new Integer(id); 
      } 
      return ids; 
     } 
    } 

需要注意的是,當我訪問的 「http://:8080/REST /視頻/」 然後我看到頁面上的輸出。

My WebAppInitializer看起來像這樣。

package com.bestbuy.supportspace.videolibrary.config; 

import org.springframework.web.WebApplicationInitializer; 
import org.springframework.web.context.ContextLoaderListener; 
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 
import org.springframework.web.filter.HiddenHttpMethodFilter; 
import org.springframework.web.servlet.DispatcherServlet; 

import javax.servlet.FilterRegistration; 
import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRegistration; 

/** 
* @author nikhil.thakur 
* @since 12/12/13 
*/ 
public class WebAppInitializer implements WebApplicationInitializer { 

    public static final String CONFIG_PACKAGE = "com.bestbuy.supportspace.videolibrary.config"; 

    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); 
     ctx.scan(CONFIG_PACKAGE); 

     addHttpMethodFilter(servletContext); 

     ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); 
     servlet.addMapping("/rest/*"); 
     servlet.setLoadOnStartup(1); 

     servletContext.addListener(new ContextLoaderListener(ctx)); 
    } 

    private void addHttpMethodFilter(ServletContext servletContext) { 
     FilterRegistration.Dynamic httpMethodFilter = servletContext.addFilter("HttpMethodFilter", new HiddenHttpMethodFilter()); 
     httpMethodFilter.addMappingForUrlPatterns(null, false, "/*"); 
    } 
} 

回答

1
mockMvc.perform(get("/rest/videos/")) 

我敢肯定,你的情況「休息」是應用程序的名稱/ URL的一部分。這與測試無關。只需使用「/ videos」。

+0

我的WebAppInitializer看起來像這樣。 –

+0

所以我認爲只是使用/視頻會運行它,但它不會。我如何解決這個問題是調試測試方法,看看這個映射的前綴是什麼。我也無法找到那 –

+0

@NikhilThakur我不認爲這裏的servlet映射(「/ rest/*」)是相關的,但我不確定。無論如何,我只是注意到,實際上沒有控制器方法映射到「視頻/」,只有「視頻」,沒有尾隨斜線。 – zeroflagL