0

How to pass a ingredientGroup? Or is there some other way?

Controller:

@Controller
@RequestMapping("/ingredients/groups")
@RequiredArgsConstructor
@PermissionUserWrite
public class IngredientGroupController {
    private static final String VIEWS_PATH = "/pages/ingredient/group/";
    private final IngredientGroupService ingredientGroupService;

    @GetMapping("{id}")
    public String show(@PathVariable("id") IngredientGroup group, Model model) {
        if (group == null) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Ingredient group not found");
        }

        model.addAttribute("group", group);
        return VIEWS_PATH + "show";
    }
}

Test:

@SpringBootTest
@AutoConfigureMockMvc
class IngredientGroupControllerTest {
    private static final String VIEWS_PATH = "/pages/ingredient/group/";
    @Autowired
    private MockMvc mockMvc;

    @Test
    @WithMockAdmin
    void show_for_admin() throws Exception {
        var ingredientGroup = Mockito.mock(IngredientGroup.class);
        mockMvc.perform(MockMvcRequestBuilders.get("/ingredients/groups/{id}", 1))
                .andExpect(status().isOk())
                .andExpect(view().name(VIEWS_PATH+"show"));
    }
}
Stan
  • 155
  • 1
  • 6
  • What is `IngredientGroup` supposed to be? You would generally use a primitive data type for `@PathVariable` (e.g. long), not a complex data type. Have a look at https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/web.html#mvc-ann-arguments – B Thuy Oct 23 '20 at 22:50
  • It is entity [spring data](https://docs.spring.io/spring-data/data-commons/docs/2.3.0.RELEASE/reference/html/#core.web.basic) – Stan Oct 23 '20 at 23:42
  • Have you loaded any `IngredientGroup` with id=1 in your in-memory database that you setup for your test? – B Thuy Oct 24 '20 at 19:20
  • I wanted to avoid this by using mockito. But it looks like it's the only way – Stan Oct 24 '20 at 23:20

1 Answers1

0

I don't what the fields are in IngredientGroup. But, I presume that there are fields name and something.

When using an object as @PathVariable, you are expected to pass its properties as query parameters. So, in your case the url you are about to test looks like: http://localhost:8080/ingredients/groups/1?name=xxxxxx&something=otherthing

@Test
public void show_for_admin() throws Exception {
    var ingredientGroup = Mockito.mock(IngredientGroup.class);
     mockMvc.perform(MockMvcRequestBuilders.get(String.format("/ingredients/groups/%d", 1), 
                                            ingredientGroup.getName(), 
                                            ingredientGroup.getSomething()))
                .andExpect(status().isOk());
}
Aman
  • 1,627
  • 13
  • 19
  • 1
    Spring tries to call the method `findById` (JpaRepository) with `@PathVariable("id")`. It returns null (and controller return status 404), because I don't have a DB for tests. I wanted to avoid this by using mockito – Stan Nov 02 '20 at 14:01
  • If your question is answered, please mark this solved and add *mocking repository* in another question. Check this https://stackoverflow.com/questions/51247796/how-to-mock-jpa-repositorys-save-method-in-unit-tests – Aman Nov 02 '20 at 14:58