• 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);
        }
    
    }
  • 相关阅读:
    ubuntu 安装FoxitReader福昕阅读器(转载)
    添加中文字库
    操作系统常用调度算法(转载https://www.cnblogs.com/kxdblog/p/4798401.html)
    2802:小游戏利用bfs来实现
    2802:小游戏
    适合使用并行的一种bfs
    (转载)关于usr/bin/ld: cannot find -lxxx问题总结
    gcc5.4报错对‘std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()’未定义的引用
    centos7如何安装gcc5.4
    将含有makefile文件的源码加入Eclipse工程
  • 原文地址:https://www.cnblogs.com/shaozhiqi/p/8706594.html
Copyright © 2020-2023  润新知