Introduction

During this exercise we will add some mock-mvc tests to test your fresly created controller(s)

Background info

Mock mvc is a framework which enables you to test your controller physically and in isolation without using your Postman tool or such And it allows you to repeat this test during every Maven build

Code

This exercise is pretty code-based, hence below you will find some code for a Sport oriented mock-mvc controller

Be aware of the comments in the code. They are added to prevent you from (abusively) removing the static imports and then not knowing anymore which imports it were …​
Example code for a SportController POST
package be.qnh.apps.sport.rest;

import be.qnh.apps.sport.domain.Sport;
import be.qnh.apps.sport.service.SportService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;


import static org.hamcrest.core.Is.is;
import static org.mockito.Mockito.any;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/*
import static org.hamcrest.core.Is.is;
import static org.mockito.Mockito.any;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 */


@RunWith(MockitoJUnitRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SportControllerTest {

    @InjectMocks
    private SportController sportEndpoint;

    @Mock
    private SportService sportService;

    private MockMvc mockMvc;

    @Before
    public void setUp() {
        this.mockMvc = MockMvcBuilders.standaloneSetup(this.sportEndpoint).build();
    }

    @Test
    public void addSportTest() throws Exception {
        Sport sport = new Sport(3L);
        sport.setName("Volleybal");
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(sport);

        Mockito.when(this.sportService.insert(any(Sport.class))).thenReturn(sport);

        this.mockMvc.perform(post("/api/sports")
                .contentType(MediaType.APPLICATION_JSON)
                .content(json)).andDo(print())
                .andExpect(jsonPath("$.id", is(Long.valueOf(sport.getId()).intValue())))
                .andExpect(jsonPath("$.name", is(sport.getName())))
                .andExpect(jsonPath("$.mixed", is(sport.isMixed())))
                .andExpect(status().isCreated()
                );
    }

}
Example code for DrinkController using GET
 @Test
    public void testGetDrink() throws Exception {
        Drink drink = new Drink();
        drink.setId(3);
        drink.setName("Cola");
        drink.setPrice(2.0);

        Mockito.when(this.drinkRepository.findById(drink.getId())).thenReturn(Optional.of(drink));

        this.mockMvc.perform(get(baseUrl+"find/"+drink.getId())
                .contentType(MediaType.APPLICATION_JSON))
                .andDo(print())
                .andExpect(jsonPath("$.id", is(Long.valueOf(drink.getId()))))
                .andExpect(jsonPath("$.name", is(drink.getName())))
                .andExpect(jsonPath("$.price", is(drink.getPrice())))
                .andExpect(status().isOk()
                );
    }

Exercise

The exercise consist of moving the code above to your frame of reference, hence you hobby or such …​