0

Mockmvc test not work when Pathvariable is a String but work on int. Here is my code:

@Controller
@RequestMapping("/etablissement/{typeEtablissement}")
public class EtablissementController{
    @RequestMapping(method = RequestMethod.GET)
    public String accueil(@PathVariable("typeEtablissement") String typeEtablissement) {
        return "/test";
    }
}

// somewhere in my test
mockMvc.perform(get("/etablissement/{typeEtablissement}", "test")).andExpect(status().isOk()); // Error 400

But, if I use int instead of String it works

@RequestMapping(method = RequestMethod.GET)
public String accueil(@PathVariable("typeEtablissement") int typeEtablissement) {
    return "/test";
}

// somewhere in my test
mockMvc.perform(get("/etablissement/{typeEtablissement}", 123)).andExpect(status().isOk()); // Works

Using Object also work

public String accueil(@PathVariable("typeEtablissement") Object typeEtablissement) {}

Thanks for your help !

2 Answers 2

1

Here is an example of a GetMapping who should help you :

@GetMapping("/{typeEtablissement}/{something2}/{something3}")
    public List<Object> extract(
            @PathVariable String typeEtablissement,
            @PathVariable String something2,
            @PathVariable String something3) {

List<Object> object = objectService.extractDatas(typeEtablissement, something2, something3);

return object;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nope, still not work. I've already tried that
0

Strange cause it means typeEtablissement is an int. You can also create a new variable after with valueOf:

@GetMapping("/{typeEtablissement}/{something2}/{something3}")
    public List<Object> extract(
            @PathVariable String typeEtablissement,
            @PathVariable String something2,
            @PathVariable String something3) {

String typeEtablissementConverted = String.valueOf(typeEtablissement );

List<Object> object = objectService.extractDatas(typeEtablissementConverted, something2, something3);

return object;
}

1 Comment

No it will be a string. I've just notice that it only not work when I use @PathVariable String. Also, app work fine for now, it's just the Spring MockMVC for my web layer test that is not work. Very strange as you said

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.