4
4
.
.
3
3
M
M
o
o
c
c
k
k
M
M
V
V
C
C
I
I
n
n
f
f
o
o
Following tutorials show how to use MockMvc to test Controllers by
performing HTTP Requests
verifying HTTP Responses
Syntax
mockMvc.perform(get("/Hello")) //perform HTTP Request
.andExpect(status().isOk()); //verify HTTP Response
I
I
n
n
s
s
t
t
a
a
n
n
t
t
i
i
a
a
t
t
e
e
M
M
o
o
c
c
k
k
M
M
v
v
c
c
MockMvc can be instantiated in different ways as shown below.
@SpringBootTest, @Autowired
@SpringBootTest
class MyControllerTest {
@Autowired MockMvc mockMvc;
@WebMvcTest, @Autowired
@WebMvcTest
class MyControllerTest {
@Autowired MockMvc mockMvc;
MockMvcBuilders
class MyControllerTest {
MyController myController = new MyController();
@Test
void hello() throws Exception {
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(myController).build();