- 引入spring-boot-starter-test 即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
- 编写测试用例
@RestController
public class DemoController {
@GetMapping("test1")
public Map<String,Object> test1(){
Map<String,Object> response= new HashMap<>();
response.put("id",2);
response.put("code",800);
response.put("msg","成功");
return response;
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
private MockMvc mockMvc;
@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.standaloneSetup(new DemoController()).build();
}
@Test
public void contextLoads() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/test1?3").accept(MediaType.APPLICATION_JSON_UTF8)).andDo(MockMvcResultHandlers.print());
}
}
返回结果