在要测试的类上ctrl + shift + t 新建测试类

    普通测试

    1. @SpringBootTest
    2. @RunWith(SpringRunner.class)
    3. public class GirlServiceTest {
    4. @Autowired
    5. private GirlService girlService;
    6. @Test
    7. public void findById() throws Exception {
    8. Girl girl = girlService.findById(1);
    9. assertEquals(new Integer(20), girl.getAge());
    10. }
    11. }

    controller层测试

    模拟http请求

    1. @RunWith(SpringRunner.class)
    2. @SpringBootTest
    3. @AutoConfigureMockMvc
    4. public class GirlControllerTest {
    5. @Autowired
    6. private MockMvc mvc;
    7. /**
    8. * andReturn().getResponse().getContentAsString() 会返回请求结果
    9. **/
    10. @Test
    11. public void girlFindOne() throws Exception {
    12. mvc.perform(MockMvcRequestBuilders.get("/girls/1"))
    13. .andReturn().getResponse().getContentAsString() // 会返回请求结果
    14. }
    15. }