九、SpringBoot入门之单元测试

匿名 (未验证) 提交于 2019-12-03 00:37:01


(一)Controller层测试

//测试环境 @RunWith(SpringRunner.class) //测试注解 @SpringBootTest //测试Controller层专门的注解 @AutoConfigureMockMvc public class GirlControllerTest {     //测试Controller层专门的对象     @Autowired     private MockMvc mockMvc;     @Test     public void getGirlById() throws Exception {         //请求方法时“GET”,那么就用get()方法         mockMvc.perform(MockMvcRequestBuilders.get("/girls/3"))                 //期望返回的Status是200                 .andExpect(MockMvcResultMatchers.status().isOk())                 //期望返回的数据是“abc”                 .andExpect(MockMvcResultMatchers.content().string("abc"));     }  }

(二)Service层测试

//测试环境 @RunWith(SpringRunner.class) //测试注解 @SpringBootTest public class GirlServiceImplTest {     //注入Service对象     @Autowired     private GirlServiceImpl girlServiceImpl;     @Test     public void getGirlById() throws Exception {         Girl girl = girlServiceImpl.getGirlById(3);         Assert.assertEquals(new Integer(15), girl.getAge());     } }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!