Controller测试类

  1. /**
  2. * Created by zhiqi.shao on 2017/5/12.
  3. */
  4. @RunWith(SpringJUnit4ClassRunner.class)
  5. @SpringBootTest(classes =MelonApplication.class)
  6. @WebAppConfiguration //启动一个真实web服务,然后调用Controller的Rest API,待单元测试完成之后再将web服务停掉
  7. public class TestUserController {
  8. @Autowired
  9. protected WebApplicationContext wac;
  10.  
  11. protected MockMvc mockMvc;
  12.  
  13. //private TestRestTemplate restTemplate = new TestRestTemplate();
  14.  
  15. @Before
  16. public void setup() throws IOException {
  17. mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
  18. }
  19.  
  20. @Test
  21. public void testf() throws Exception{
  22. String updateResult = mockMvc.perform(MockMvcRequestBuilders.post("/admin/test").param("id", "4"))
  23. .andReturn()
  24. .getResponse()
  25. .getContentAsString();
  26. System.out.println("----------查询----------" + updateResult);
  27.  
  28. HttpServletResponse response= mockMvc.perform(MockMvcRequestBuilders.post("/admin/test").param("id", "4"))
  29. .andReturn()
  30. .getResponse();
  31. System.out.println("***************************************************"+response);
  32.  
  33. }
  34.  
  35. }

Service测试类

  1. /**
  2. * Created by zhiqi.shao on 2017/5/12.
  3. */
  4. @Slf4j
  5. @RunWith(SpringRunner.class)
  6. @SpringBootTest(webEnvironment = RANDOM_PORT)
  7. public class TestUserService {
  8.  
  9. @Autowired
  10. private UserService userService;
  11.  
  12. private Long id;
  13.  
  14. @Before
  15. public void bf(){
  16. log.info("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%defore");//设置要mock的Controller类,可以是多个
  17. }
  18.  
  19. @Test
  20. public void testAll() {
  21. this.saveUser();
  22. this.getUser();
  23. this.findAll();
  24. this.delete();
  25. }
  26.  
  27. @Test
  28. public void saveUser(){
  29. User user=new User();
  30. user.setPassword("2345");
  31. user.setEmail("zhiqi@123.com");
  32. user.setPhone("15210886857");
  33. user.setUsername("shaoshao");
  34. userService.save(user);
  35. id=user.getUid();
  36. log.info("id:"+id);
  37. }
  38.  
  39. @Test
  40. public void getUser(){
  41. User user=userService.getUser(3L);
  42. log.info(GSON.toJson(user));
  43. }
  44.  
  45. @Test
  46. public void findAll(){
  47. List<User> users=userService.findAll();
  48. log.info(GSON.toJson(users));
  49.  
  50. }
  51.  
  52. @Test
  53. public void delete(){
  54. userService.deleteUserById(3L);
  55. }
  56.  
  57. }

springboot测试类的更多相关文章

  1. SpringBoot测试类启动错误 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

    报错 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @Cont ...

  2. JPA实体类注解、springboot测试类、lombok的使用

    前提准备: 搭建一个springboot项目,详情请参见其它博客:点击前往 1 引入相关依赖 web.mysql.jpa.lombok <?xml version="1.0" ...

  3. IntelliJ IDEA 2017版 SpringBoot测试类编写

    SpringBoot的测试类编写Demo 源码见 https://github.com/liushaoye/baseone.git

  4. Springboot 测试类没有找到bean注入

    其他乱七八糟配置就不扯了,先上项目结构图 配置好参数后我再src/test/java类测试访问数据库时发现bean没有正确的注入.值得注意的是,这个项目的启动类是叫App.java 所以我们必须在这个 ...

  5. springboot 测试类,项目使用shiro时报错UnavailableSecurityManagerException

    大概的问题就是,正常运行项目是没有问题的 使用测试类是,加载不了shiro的securityManager,主要导致不是很清楚,望告知, 解决方法 @Resource org.apache.shiro ...

  6. Springboot测试类之@RunWith注解

    @runWith注解作用: --@RunWith就是一个运行器 --@RunWith(JUnit4.class)就是指用JUnit4来运行 --@RunWith(SpringJUnit4ClassRu ...

  7. SpringBoot 测试类 @RunWith & @SpringBootTest

    @RunWith(SpringRunner.class) @SpringBootTest public class RabbitMqTest { @Autowired RabbitMqSender r ...

  8. 学习springboot整合mybatis并编写测试类

    报名立减200元.暑假直降6888. 邀请链接:http://www.jnshu.com/login/1/20535344 邀请码:20535344 遇到的问题: 1.原因是在启动类上只有一个@Map ...

  9. SpringBoot高版本修改为低版本时测试类报错解决

    有时在使用idea通过Spring Initailizr创建项目时,默认只能创建最近的版本的SpringBoot项目. 这是如果想要换成版本,就可以在项目创建好了之后,在pom文件中直接将版本修改过来 ...

随机推荐

  1. [bzoj3694]最短路_树链剖分_线段树

    最短路 bzoj-3694 题目大意:给你一个n个点m条边的无向图,源点为1,并且以点1为根给出最短路树.求对于2到n的每个点i,求最短路,要求不经过给出的最短路树上的1到i的路径上的最后一条边. 注 ...

  2. cppunit 的使用

    原文: http://blog.csdn.net/abcdef0966/article/details/5699248

  3. 在win10下改变默认的输入法切换Ctrl+Space

    在win10下改变默认的输入法切换Ctrl+Space 学习了:https://www.zhihu.com/question/22288432 在win10下面,有两种风格的控制面板设置: 在输入法上 ...

  4. OpenGL ES2.0 基本编程

    1. EGL OpenGL ES命令须要一个rendering context和一个drawing surface. Rendering Context: 保存当前的OpenGL ES状态. Draw ...

  5. Android 依据EditText搜索框ListView动态显示数据

    依据EditText搜索框ListView动态显示数据是依据需求来的,认为这之中涉及的东西可能比較的有意思,所以动手来写一写.希望对大家有点帮助. 首先.我们来分析下整个过程: 1.建立一个layou ...

  6. sap scriptfom 多语言翻译

    在某项目中,因为客户上线较早,非常多打印程序的form是由scriptform制做,又因为美国工厂要上线.免不了对scriptform进行多语言翻译.以下是对当中的一个交货单打印进行的多语言翻译,分享 ...

  7. 使用oracle数据库和MySQL数据库时hibernate的映射文件.hbm.xml的不同

    假设是使用oracle数据库.那么hibernate的映射文件.hbm.xml例如以下: <id name="xuehao" column="xuehao" ...

  8. iOS 9 平台上 AFNetworking 框架 3.0 版本号解决的问题和问题解决

    iOS 9 平台上 AFNetworking 框架 3.0 版本号解决的问题和问题解决 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名- ...

  9. layui日期输入框

    <div class="layui-form-item">                <label class="layui-form-label& ...

  10. go package包的使用

    一.标准库 引入 在我们之前所写的所以代码中,我们基本上可以看到fmt这个导入的包,但是我们却不知道如何去写这种包. 如果我们可以自己去写,那么我们就可以将一个功能的集合统一的放入包中,便于以后使用, ...