前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot之单元测试

springboot之单元测试

作者头像
顾翔
发布2019-12-11 14:10:34
2630
发布2019-12-11 14:10:34
举报

来源:http://www.51testing.com

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-10-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 软件测试培训 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档