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

5
  • 1
    a path variable cannot be null, hence your check/if is useless. Commented Nov 29, 2018 at 11:14
  • @neorus look over similar example here : stackoverflow.com/questions/45825955/… Commented Nov 29, 2018 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 Commented Nov 29, 2018 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. Commented Nov 29, 2018 at 11:39
  • and if the mapping will be "/{name}/import"? still the same or there a way to pass null? Commented Nov 29, 2018 at 11:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.