2016-02-16 29 views
1

我尝试设置的弹簧安置上下文路径使用下面的代码片段嘲笑:弹簧安置模拟上下文路径

private MockMvc mockMvc; 

@Before 
public void setUp() { 
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) 
      .apply(documentationConfiguration(this.restDocumentation)) 
      .alwaysDo(document("{method-name}/{step}/", 
        preprocessRequest(prettyPrint()), 
        preprocessResponse(prettyPrint()))) 
      .build(); 
} 

@Test 
public void index() throws Exception { 
    this.mockMvc.perform(get("/").contextPath("/api").accept(MediaTypes.HAL_JSON)) 
      .andExpect(status().isOk()) 
      .andExpect(jsonPath("_links.business-cases", is(notNullValue()))); 
} 

但我收到以下错误:

java.lang.IllegalArgumentException: requestURI [/] does not start with contextPath [/api] 

什么是错的? 是否可以在代码中的单个位置指定contextPath?直接在建设者?

编辑

这里控制器

@RestController 
@RequestMapping(value = "/business-case", produces = MediaType.APPLICATION_JSON_VALUE) 
public class BusinessCaseController { 
    private static final Logger LOG = LoggerFactory.getLogger(BusinessCaseController.class); 

    private final BusinessCaseService businessCaseService; 

    @Autowired 
    public BusinessCaseController(BusinessCaseService businessCaseService) { 
     this.businessCaseService = businessCaseService; 
    } 

    @Transactional(rollbackFor = Throwable.class, readOnly = true) 
    @RequestMapping(value = "/{businessCaseId}", method = RequestMethod.GET) 
    public BusinessCaseDTO getBusinessCase(@PathVariable("businessCaseId") Integer businessCaseId) { 
     LOG.info("GET business-case for " + businessCaseId); 
     return businessCaseService.findOne(businessCaseId); 
    } 
} 
+0

尝试后您的控制器 – Abdelhak

+0

请参阅编辑。为什么downvote?请记住'server.context-path =/api'已设置。据我所知,这应该不会对控制器产生任何影响。 –

回答

3

您需要在您传递到get路径上下文路径。

你在问题中所显示的情况下,上下文路径是/api,你想做出/的请求,所以你需要通过/api/get

@Test 
public void index() throws Exception { 
    this.mockMvc.perform(get("/api/").contextPath("/api").accept(MediaTypes.HAL_JSON)) 
      .andExpect(status().isOk()) 
      .andExpect(jsonPath("_links.business-cases", is(notNullValue()))); 
}