0

I'm writing tests for a spring code, one of my function contain PathVariable, which is used in the function itself, and i want to test it whenever it passes as null, but if i sent it as null, i got 400 status because it's not getting it pathvariable. my code:

@RequestMapping(value = "/{name}", method = RequestMethod.POST)
public importedName importName(... @PathVariable("name") String name ..) throws Exception {
    if (name == null) {
        do some stuff..
    }

My naive approach:

String name = null;
mockMvc.perform(post(".." + "/{name}", name).header(..).accept(..))

and i cannot find a way to enter inside the if statement

neorus
  • 477
  • 1
  • 6
  • 19
  • 1
    a path variable cannot be `null`, hence your check/if is useless. – M. Deinum Nov 29 '18 at 11:14
  • @neorus look over similar example here : https://stackoverflow.com/questions/45825955/using-spring-mockmvc-to-test-optional-path-variables – KOTIOS Nov 29 '18 at 11:17
  • the problem is i cannot edit the actual code(legacy), only write tests to it in the if statement, if the name is null, the code executing the name from another method – neorus Nov 29 '18 at 11:30
  • 1
    As stated it cannot be `null` if there is no path variable it won't match this method but the one ending with `/` and not `/{name}`. So your test shouldn't include a path-variable and you will probably get a 404. – M. Deinum Nov 29 '18 at 11:39
  • and if the mapping will be "/{name}/import"? still the same or there a way to pass null? – neorus Nov 29 '18 at 11:56

0 Answers0