5

次のコード スニペットを使用して、スプリング レスト モックのコンテキスト パスを設定しようとしています。

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]

なにが問題ですか? コード内の 1 つの場所 (ビルダー内など) で 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);
    }
}
4

2 に答える 2

16

に渡すパスにコンテキスト パスを含める必要があります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())));
}
于 2016-02-16T17:16:23.163 に答える