第02篇:web demo
流程
添加依赖编写控制器MockMvc测试
1. pom依赖
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-web
</artifactId>
</dependency>
2. 编写控制器
@RestController
public class IndexController {
@GetMapping("index")
public String
index() {
return "response string: index";
}
}
3. MockMvc测试
@Test
public void test() throws Exception
{
MockMvc mockMvc
= MockMvcBuilders
.standaloneSetup(new IndexController()).build();
String str
= mockMvc
.perform(MockMvcRequestBuilders
.get("/index").accept(MediaType
.APPLICATION_JSON
))
.andExpect(MockMvcResultMatchers
.status().isOk())
.andReturn()
.getResponse()
.getContentAsString();
System
.out
.println(str
);
}
转载请注明原文地址:https://tech.qufami.com/read-16202.html