zoukankan      html  css  js  c++  java
  • springboot测试类

    Controller测试类

    /**
     * Created by zhiqi.shao on 2017/5/12.
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes =MelonApplication.class)
    @WebAppConfiguration //启动一个真实web服务,然后调用Controller的Rest API,待单元测试完成之后再将web服务停掉
    public class TestUserController {
        @Autowired
        protected WebApplicationContext wac;
    
        protected MockMvc mockMvc;
    
        //private TestRestTemplate restTemplate = new TestRestTemplate();
    
        @Before
        public void setup() throws IOException {
            mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
        }
    
        @Test
        public void testf() throws Exception{
            String updateResult = mockMvc.perform(MockMvcRequestBuilders.post("/admin/test").param("id", "4"))
                    .andReturn()
                    .getResponse()
                    .getContentAsString();
            System.out.println("----------查询----------" + updateResult);
    
             HttpServletResponse response= mockMvc.perform(MockMvcRequestBuilders.post("/admin/test").param("id", "4"))
                    .andReturn()
                    .getResponse();
             System.out.println("***************************************************"+response);
    
        }
    
    }

    Service测试类

    /**
     * Created by zhiqi.shao on 2017/5/12.
     */
    @Slf4j
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = RANDOM_PORT)
    public class TestUserService {
    
        @Autowired
        private UserService userService;
    
        private Long id;
    
        @Before
        public void bf(){
            log.info("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%defore");//设置要mock的Controller类,可以是多个
        }
    
    
    
        @Test
        public void testAll()  {
            this.saveUser();
            this.getUser();
            this.findAll();
            this.delete();
        }
    
        @Test
        public void saveUser(){
            User user=new User();
            user.setPassword("2345");
            user.setEmail("zhiqi@123.com");
            user.setPhone("1521088XXXXX");
            user.setUsername("shaoshao");
            userService.save(user);
            id=user.getUid();
            log.info("id:"+id);
        }
    
    
        @Test
        public void getUser(){
            User user=userService.getUser(3L);
            log.info(GSON.toJson(user));
        }
    
        @Test
        public void findAll(){
            List<User> users=userService.findAll();
            log.info(GSON.toJson(users));
    
        }
    
        @Test
        public void delete(){
            userService.deleteUserById(3L);
        }
    
    }
  • 相关阅读:
    c++ stl中的二分查找
    2015年---移动端webapp知识总结
    移动端网站优化指南-WAP篇
    ASO优化总结(基于网络分享的知识总结归纳)
    验证数字的正则表达式集
    个人的浏览器重置样式表(总结)
    微信或移动端网页的meta
    移动端字体和字体大小规范
    min-device-pixel-ratio
    Emmet语法实例(帮助快速开发)
  • 原文地址:https://www.cnblogs.com/shaozhiqi/p/8706594.html
Copyright © 2011-2022 走看看