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("15210886857");
        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);
    }

}

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. ZooKeeper学习总结(1)——ZooKeeper入门介绍

    1. 概述 Zookeeper是Hadoop的一个子项目,它是分布式系统中的协调系统,可提供的服务主要有:配置服务.名字服务.分布式同步.组服务等. 它有如下的一些特点: 简单 Zookeeper的核 ...

  2. Java基础学习总结(42)——Log4j 2使用教程

    1. 去官方下载log4j 2,导入jar包,基本上你只需要导入下面两个jar包就可以了(xx是乱七八糟的版本号): log4j-core-xx.jar log4j-api-xx.jar 2. 导入到 ...

  3. CSVHelper在Asp.Net MVC中的使用

    1,从数据库读取数据,然后导出CSV文件 [HttpPost] public FileResult ExportCSV() { var apps =....//linq以及EF从数据库查询数据 Mem ...

  4. 洛谷 P1843 奶牛晒衣服

    题目背景 熊大妈决定给每个牛宝宝都穿上可爱的婴儿装 . 于是 , 为牛宝宝洗晒衣服就成了很不爽的事情. 题目描述 熊大妈请你帮助完成这个重任 . 洗完衣服后 , 你就要弄干衣服 . 衣服在自然条件下用 ...

  5. git游戏杂谈

    git作为一个知名小游戏,在被Linus开发出来后就广受好评,在程序员圈子内迅速传播,以至于现在很多程序员可以一日无饭,却不能一日无git.是什么能让各路程序员如此着迷?今天,让我们走进git,看一看 ...

  6. [转]十五天精通WCF——第二天 告别烦恼的config配置

    经常搞wcf的基友们肯定会知道,当你的应用程序有很多的“服务引用”的时候,是不是有一种疯狂的感觉...从一个环境迁移到另外一个环境,你需要改变的 endpoint会超级tmd的多,简直就是搞死了人.. ...

  7. open cursor too much error

    今天遇到一个错误ORA-01000: maximum open cursors exceeded. 客户想增加 DB 的open_cursor这个参数. 但是我看了下,她的程序要打开几千个cursor ...

  8. ExtJS学习笔记3:载入、提交和验证表单

    载入数据 1.比較好用的设置form数据的方法: formPanel.getForm().setValues([{id: 'FirstName', value: 'Joe'}]); 当中id值为for ...

  9. java 集合交并补

    通过使用泛型方法和Set来表达数学中的表达式:集合的交并补.在下面三个方法中都将第一个參数Set复制了一份,并未直接改动參数中Set. package Set; import java.util.Ha ...

  10. ACM Amman Collegiate Programming Contest(7.22随机组队娱乐赛)

    题目链接 https://vjudge.net/contest/240074#overview 只写一下自己做的几个题吧 /* D n^2的暴力dp怎么搞都可以的 这里先预处理 i到j的串时候合法 转 ...