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. hdu_1048_The Hardest Problem Ever_201311052052

    The Hardest Problem Ever Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  2. MySQL5.7出现无法启动服务等问题

    MySQL5.7版本后有点不同,就是没有DATA文件夹.总是莫名其妙出现一些错误.比如连不上数据库了,出现错误 can't connect mysql on localhost,键入 net star ...

  3. mongodb之配置

    前言 最新版本支持yaml格式的配置文件,只支持空格,不能使用tab 详细配置说明 #系统日志配置 systemLog: destination: file path: /var/log/mongod ...

  4. keras 与tensorflow 混合使用

    keras 与tensorflow 混合使用 tr:nth-child(odd) > td, .table-striped tbody > tr:nth-child(odd) > t ...

  5. java 执行可执行文件时提示“could not find or load main class ”的问题

  6. lsyncd + rsync 实时同步搭建

    一.inotify和lsync inotify和lsyncd对比一下,发现虽然lsyncd没有inotify那么真正的实时同步,但是lsyncd的同步基本上可以满足基本实时同步的要求,而且lsyncd ...

  7. CSDN挑战编程——《绝对值最小》

    绝对值最小 题目详情: 给你一个数组A[n],请你计算出ans=min(|A[i]+A[j]|)(0<=i,j<n). 比如:A={1, 4, -3}, 则: |A[0] + A[0]| ...

  8. LeetCode之LCP(Longest Common Prefix)问题

    这个也是简单题目.可是关键在于题意的理解. 题目原文就一句话:Write a function to find the longest common prefix string amongst an ...

  9. 深入剖析Android四大组件(一)——Activity生命周期具体解释

    1.管理Activity的生命周期 不管是正在执行的Activity还是没有执行的Activity,它们都接受Android的框架管理,这使得Activity处于不同的生命周期. ①Activity的 ...

  10. Python字符串转为字典方法大全

    方法一: 通过内置函数eval str_info = '{"name": "test", "age": 18}' dict_info = e ...