在要测试的类上ctrl + shift + t 新建测试类
普通测试
@SpringBootTest@RunWith(SpringRunner.class)public class GirlServiceTest {@Autowiredprivate GirlService girlService;@Testpublic void findById() throws Exception {Girl girl = girlService.findById(1);assertEquals(new Integer(20), girl.getAge());}}
controller层测试
模拟http请求
@RunWith(SpringRunner.class)@SpringBootTest@AutoConfigureMockMvcpublic class GirlControllerTest {@Autowiredprivate MockMvc mvc;/*** andReturn().getResponse().getContentAsString() 会返回请求结果**/@Testpublic void girlFindOne() throws Exception {mvc.perform(MockMvcRequestBuilders.get("/girls/1")).andReturn().getResponse().getContentAsString() // 会返回请求结果}}
