Using Spring mockMvc to test optional path variables
Using an array of @RequestMapping
values like this ...
@RequestMapping(
value = {"/some/uri/{foo}", "/some/uri/{foo}/{bar}"},
method = RequestMethod.PUT)
public ResponseEntity<String> someMethod(
@PathVariable("foo") String foo,
@PathVariable(value = "bar", required = false) String bar
) {
return new ResponseEntity<>(foo + " and " + (bar == null ? "<null>" : bar), HttpStatus.OK);
}
... will enable this test to pass:
@Test
public void someMethodTest() throws Exception {
MvcResult mvcResult = mockMvc.perform(put("/some/uri/{foo}/{bar}", "foo", "bar"))
.andExpect(status().isOk()).andReturn();
Assert.assertEquals("foo and bar", mvcResult.getResponse().getContentAsString());
mvcResult = mockMvc.perform(put("/some/uri/{foo}/{bar}", "foo", null))
.andExpect(status().isOk()).andReturn();
Assert.assertEquals("foo and <null>", mvcResult.getResponse().getContentAsString());
mvcResult = mockMvc.perform(put("/some/uri/{foo}/{bar}", "foo", ""))
.andExpect(status().isOk()).andReturn();
Assert.assertEquals("foo and <null>", mvcResult.getResponse().getContentAsString());
mvcResult = mockMvc.perform(put("/some/uri/{foo}", "foo"))
.andExpect(status().isOk()).andReturn();
Assert.assertEquals("foo and <null>", mvcResult.getResponse().getContentAsString());
}
That certainly seems to be the simplest solution and it is likely to be more friendly to tools such as Swagger since it make the mappings explicit.
However, you could also declare a wildcard mapping and then use a path matcher within your controller method to interpret the request URI. For example, this method ...
private final AntPathMatcher antPathMatcher = new AntPathMatcher();
@RequestMapping(value = "/some/uri/with/wildcards/**", method = RequestMethod.PUT)
public ResponseEntity<String> someMethod(HttpServletRequest request) {
String matched = antPathMatcher.extractPathWithinPattern(
(String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE), request.getPathInfo());
// ugly parsing code to read the path variables, allowing for the optionality of the second one
String foo = matched;
String bar = null;
String[] pathVariables = matched.split("/");
if (pathVariables.length > 1) {
foo = pathVariables[0];
bar = pathVariables[1];
}
return new ResponseEntity<>(foo + " and " + (bar == null ? "<null>" : bar), HttpStatus.OK);
}
... will enable this test to pass:
@Test
public void someMethodTestWithWildcards() throws Exception {
MvcResult mvcResult = mockMvc.perform(put("/some/uri/with/wildcards/{foo}/{bar}", "foo", "bar"))
.andExpect(status().isOk()).andReturn();
Assert.assertEquals("foo and bar", mvcResult.getResponse().getContentAsString());
mvcResult = mockMvc.perform(put("/some/uri/with/wildcards/{foo}/{bar}", "foo", null))
.andExpect(status().isOk()).andReturn();
Assert.assertEquals("foo and <null>", mvcResult.getResponse().getContentAsString());
mvcResult = mockMvc.perform(put("/some/uri/with/wildcards/{foo}/{bar}", "foo", ""))
.andExpect(status().isOk()).andReturn();
Assert.assertEquals("foo and <null>", mvcResult.getResponse().getContentAsString());
mvcResult = mockMvc.perform(put("/some/uri/with/wildcards/{foo}", "foo"))
.andExpect(status().isOk()).andReturn();
Assert.assertEquals("foo and <null>", mvcResult.getResponse().getContentAsString());
}
This is late but I faced recently this situation and thought this post would help others.
In case of mocking endpoints with optional request param or path variable, you can specify it like this.
Say I have a method with params as m1(String param1, String param2)
called from controller.
Where param 2 is an optional param for controller, so at runtime null would be passed if it is not passed.
How to mock:
Mockito.when(m1(Mockito.anyString(), Mockito.eq(null)).the return(<whatever you want to return>)
Use Mockito.eq(null)
in your test to pass it as null for optional param.