springboot之单元测试

发表于:2018-10-26 13:35

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:海之浪子    来源:51testing采编

分享:
  springboot在写完之后,肯定都需要进行单元测试,如下给出一些样例
  工程层次结构如图
   
  代码如下:
  controller:
  package com.rookie.bigdata.controller;
  import com.rookie.bigdata.domain.User;
  import com.rookie.bigdata.service.UserService;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.RestController;
  /**
  * controller层
  * Created by on 2018/9/28.
  */
  @RestController
  public class UserController {
  @Autowired
  private UserService userService;
  /**
  * 查询用户
  *
  * @return
  */
  @GetMapping(value = "/user")
  public User findUser() {
  return userService.findOne(10);
  }
  }
  User:
  package com.rookie.bigdata.domain;
  /**
  * domain实体对象
  * Created by on 2018/9/28.
  */
  public class User {
  private int id;
  private String name;
  private Integer age;
  public Integer getAge() {
  return age;
  }
  public void setAge(Integer age) {
  this.age = age;
  }
  public User() {
  }
  public int getId() {
  return id;
  }
  public void setId(int id) {
  this.id = id;
  }
  public String getName() {
  return name;
  }
  public void setName(String name) {
  this.name = name;
  }
  @Override
  public String toString() {
  return "User{" +
  "id=" + id +
  ", name='" + name + '\'' +
  ", age=" + age +
  '}';
  }
  }
  service:
  package com.rookie.bigdata.service;
  import com.rookie.bigdata.domain.User;
  import org.springframework.stereotype.Service;
  /**
  * service层
  * Created by  on 2018/9/28.
  */
  @Service
  public class UserService {
  public User findOne(Integer id) {
  User user = new User();
  user.setId(id);
  user.setName("张三");
  user.setAge(23);
  return user;
  }
  }
  启动程序:
  package com.rookie.bigdata;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.scheduling.annotation.EnableScheduling;
  /**
  * 应用程序启动类
  * Created by on 2018/8/2.
  */
  @SpringBootApplication
  public class Application {
  public static void main(String[] args) {
  SpringApplication.run(Application.class, args);
  }
  }
  测试类:
  package com.rookie.bigdata.controller;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.http.MediaType;
  import org.springframework.test.context.junit4.SpringRunner;
  import org.springframework.test.web.servlet.MockMvc;
  import org.springframework.test.web.servlet.MvcResult;
  import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
  import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
  import static org.junit.Assert.*;
  /**
  * 测试contoller层
  * Created by on 2018/9/28.
  */
  @RunWith(SpringRunner.class)
  @SpringBootTest
  @AutoConfigureMockMvc
  public class UserControllerTest {
  @Autowired
  private MockMvc mvc;
  @Test
  public void findUser() throws Exception {
  //        mvc.perform(MockMvcRequestBuilders.get("/user"))
  //                .andExpect(MockMvcResultMatchers.status().isOk());
  MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get("/user"))
  .andExpect(MockMvcResultMatchers.status().isOk())//模拟发送get请求
  .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))//预期返回值的媒体类型 application/json;charset=UTF-8
  .andReturn();//返回执行的请求结果
  System.out.println(mvcResult.getResponse().getContentAsString());
  }
  }
  package com.rookie.bigdata.service;
  import com.rookie.bigdata.domain.User;
  import org.junit.Assert;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.test.context.junit4.SpringRunner;
  /**
  * 测试service层
  * Created by on 2018/9/28.
  */
  @RunWith(SpringRunner.class)
  @SpringBootTest
  public class UserServiceTest {
  @Autowired
  private UserService userService;
  @Test
  public void findOne() throws Exception {
  User user = userService.findOne(1);
  Assert.assertEquals(new Integer(23),user.getAge());
  }
  }

    上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号