MockMvc
MockMvc is mainly used to test the code through the controller. By calling the controller (REST endpoint) directly, we can cover the whole application from MockMvc testing itself. Also, if we keep any authentication or restriction on the controller, it will also be covered in MockMvc test cases.
The following code will test our basic API (localhost:8080/
) using MockMvc standards:
import static org.hamcrest.Matchers.is; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org...