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. CF586D. Phillip and Trains

    /* CF586D. Phillip and Trains http://codeforces.com/problemset/problem/586/D 搜索 */ #include<cstdi ...

  2. Solr插件的弊端

    在前文<Solr Update插件自定义条件索引>中,我介绍了如何通过插件的模式,自定义Solr的Update过程.但是在大半年的使用过程中,发现这种方式存在如下弊端. 1.环境难以维护. ...

  3. N天学习一个linux命令之ssh-keygen

    用途 生成ssh加密算法需要使用到的秘钥以及管理和转换 用法 ssh-keygen [-q] [-b bits] -t type [-N new_passphrase] [-C comment] [- ...

  4. RDS for MySQL查询缓存 (Query Cache) 的设置和使用

    https://help.aliyun.com/knowledge_detail/41717.html?spm=5176.7841698.2.11.aCvOXJ RDS for MySQL查询缓存 ( ...

  5. 开源GIS软件 2

    Android上的导航软件 AndNav AndNav 是一款 Android 手机上的 GPS导航软件(非开源).软件支持GPS定位信息,目的地查询,道路建议管理,导航提示等功能,十分强大的一款软件 ...

  6. HIHO 16 B

    卡了~卡了就写不下去了~其实是不会~ 大牛提醒,答案必定是SUM的因子~细细想了好久,才想通~差距~ 因为是所有的和GCD,所以GCD必定整除SUM.. 然后,枚举这些因子,统计前缀和的MOD,看有多 ...

  7. The superclass &quot;javax.servlet.http.HttpServlet&quot; was not found on the Java Build Path

    完整错误信息: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS"AS IS" AND ANY ...

  8. Cookie &amp;&amp; Session &amp;&amp; Token

    Cookies Cookie的由来: HTTP 本身是一个无状态的 request/response 协议. server接收一个来自client的request, 处理完以后返回一个response ...

  9. MongoDB改动、删除文档的域属性实例

    MongoDB改动.删除文档的域属性实例 在站点的开发中,可能最初的设计不合理.或者后期业务的变更,会造成文档结构会有些无用的属性.须要去删除或改动.因为MongoDB 是无 Schema 的,不像关 ...

  10. activeMQ公布订阅模式中中经常使用工具类

    package com.jms; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import javax.j ...