Spring REST mock context path
Asked Answered
B

2

6

I try to set the context path for spring rest mocks using the following code snippet:

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())));
}

But I receive the following error:

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

What is wrong? Is it possible to specify the contextPath at a single place in code e.g. directly in the builder?

edit

here the controller

@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);
    }
}
Bloodstain answered 16/2, 2016 at 14:50 Comment(2)
Try to post your controllerAllx
see the edit. Why the downvote? Please keep in mind that the server.context-path=/api is set. As far as I know this should not have any impact on the controller.Bloodstain
D
17

You need to include the context path in the path that you're passing to get.

In the case you've shown in the question, the context path is /api and you want to make a request to / so you need to pass /api/ to 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())));
}
Douala answered 16/2, 2016 at 17:16 Comment(2)
Your (src/main) code can set the context path using property server.servlet.context-path. It's disappointing that your (src/test) code needs to specify the context path in each mockMvc.perform() call. There should be a way to do that in one place for all test cases.Luminance
@Value("${spring.data.rest.basePath}") String basePath; and then basePath+"/"Aswan
L
5

What many people do is simply not use the context path in mockMvc tests. You only specify the @RequestMapping URI:

@Test
public void index() throws Exception {
    this.mockMvc.perform(get("/business-case/1234").accept(MediaTypes.HAL_JSON))
            .andExpect(status().isOk())
            .andExpect(jsonPath("_links.business-cases", is(notNullValue())));
}
Luminance answered 25/1, 2019 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.