溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用SpringBoot如何實(shí)現(xiàn)一個(gè)測(cè)試功能

發(fā)布時(shí)間:2020-11-20 15:04:24 來源:億速云 閱讀:233 作者:Leah 欄目:開發(fā)技術(shù)

使用SpringBoot如何實(shí)現(xiàn)一個(gè)測(cè)試功能?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

普通測(cè)試

假設(shè)要測(cè)試一個(gè)工具類 StringUtil(com.rxliuli.example.springboottest.util.StringUtil)

/**
 * 用于測(cè)試的字符串工具類
 *
 * @author rxliuli
 */
public class StringUtil {
 /**
  * 判斷是否為空
  *
  * @param string 要進(jìn)行判斷的字符串
  * @return 是否為 null 或者空字符串
  */
 public static boolean isEmpty(String string) {
  return string == null || string.isEmpty();

 }

 /**
  * 判斷是否為空
  *
  * @param string 要進(jìn)行判斷的字符串
  * @return 是否為 null 或者空字符串
  */
 public static boolean isNotEmpty(String string) {
  return !isEmpty(string);
 }

 /**
  * 判斷是否有字符串為空
  *
  * @param strings 要進(jìn)行判斷的一個(gè)或多個(gè)字符串
  * @return 是否有 null 或者空字符串
  */
 public static boolean isAnyEmpty(String... strings) {
  return Arrays.stream(strings)
    .anyMatch(StringUtil::isEmpty);
 }

 /**
  * 判斷字符串是否全部為空
  *
  * @param strings 要進(jìn)行判斷的一個(gè)或多個(gè)字符串
  * @return 是否全部為 null 或者空字符串
  */
 public static boolean isAllEmpty(String... strings) {
  return Arrays.stream(strings)
    .allMatch(StringUtil::isEmpty);
 }
}

需要添加依賴 spring-boot-starter-test 以及指定 assertj-core 的最新版本

<dependencies>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
 </dependency>
</dependencies>
<dependencyManagement>
 <dependencies>
  <dependency>
   <groupId>org.assertj</groupId>
   <artifactId>assertj-core</artifactId>
   <version>3.9.1</version>
   <scope>test</scope>
  </dependency>
 </dependencies>
</dependencyManagement>

這里指定 assertj-core 的版本是為了使用較新的一部分?jǐn)嘌怨δ埽ɡ鐚傩?lambda 斷言)

/**
 * @author rxliuli
 */
public class StringUtilTest {
 private String strNull = null;
 private String strEmpty = "";
 private String strSome = "str";

 @Test
 public void isEmpty() {
  //測(cè)試 null
  assertThat(StringUtil.isEmpty(strNull))
    .isTrue();
  //測(cè)試 empty
  assertThat(StringUtil.isEmpty(strEmpty))
    .isTrue();
  //測(cè)試 some
  assertThat(StringUtil.isEmpty(strSome))
    .isFalse();
 }

 @Test
 public void isNotEmpty() {
  //測(cè)試 null
  assertThat(StringUtil.isNotEmpty(strNull))
    .isFalse();
  //測(cè)試 empty
  assertThat(StringUtil.isNotEmpty(strEmpty))
    .isFalse();
  //測(cè)試 some
  assertThat(StringUtil.isNotEmpty(strSome))
    .isTrue();
 }

 @Test
 public void isAnyEmpty() {
  assertThat(StringUtil.isAnyEmpty(strNull, strEmpty, strSome))
    .isTrue();
  assertThat(StringUtil.isAnyEmpty())
    .isFalse();
 }

 @Test
 public void isAllEmpty() {
  assertThat(StringUtil.isAllEmpty(strNull, strEmpty, strSome))
    .isFalse();
  assertThat(StringUtil.isAnyEmpty(strNull, strEmpty))
    .isTrue();
 }
}

這里和非 SpringBoot 測(cè)試時(shí)沒什么太大的區(qū)別,唯一的一點(diǎn)就是引入 Jar 不同,這里雖然我們只引入了 spring-boot-starter-test,但它本身已經(jīng)幫我們引入了許多的測(cè)試相關(guān)類庫了。

Dao/Service 測(cè)試

從這里開始就和標(biāo)準(zhǔn)的 Spring 不太一樣了

首先,我們需要 Dao 層,這里使用 H2DB 和 SpringJDBC 做數(shù)據(jù)訪問層(比較簡(jiǎn)單)。

依賴

<dependency>
 <groupId>com.h3database</groupId>
 <artifactId>h3</artifactId>
 <scope>runtime</scope>
</dependency>
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

添加兩個(gè)初始化腳本

數(shù)據(jù)庫結(jié)構(gòu) db_schema.sqldb/db_schema.sql

drop table if exists user;
create table user (
 id int auto_increment not null
 comment '編號(hào)',
 name varchar(20)  not null
 comment '名字',
 sex boolean   null
 comment '性別',
 age int    null
 comment '年齡'
);

數(shù)據(jù)庫數(shù)據(jù) db_data.sqldb/db_data.sql

insert into user (id, name, sex, age)
values
 (1, '琉璃', false, 17),
 (2, '月姬', false, 1000);

為 SpringBoot 配置一下數(shù)據(jù)源及初始化腳本

spring:
 datasource:
 driver-class-name: org.h3.Driver
 platform: h3
 schema: classpath:db/db_schema.sql
 data: classpath:db/db_data.sql

然后是實(shí)體類與 Dao

用戶實(shí)體類 Usercom.rxliuli.example.springboottest.entity.User

/**
 * @author rxliuli
 */
public class User implements Serializable {
 private Integer id;
 private String name;
 private Boolean sex;
 private Integer age;

 public User() {
 }

 public User(String name, Boolean sex, Integer age) {
  this.name = name;
  this.sex = sex;
  this.age = age;
 }

 public User(Integer id, String name, Boolean sex, Integer age) {
  this.id = id;
  this.name = name;
  this.sex = sex;
  this.age = age;
 }
 //getter() and setter()
}

用戶 Dao UserDaocom.rxliuli.example.springboottest.dao.UserDao

/**
 * @author rxliuli
 */
@Repository
public class UserDao {
 private final RowMapper<User> userRowMapper = (rs, rowNum) -> new User(
   rs.getInt("id"),
   rs.getString("name"),
   rs.getBoolean("sex"),
   rs.getInt("age")
 );
 @Autowired
 private JdbcTemplate jdbcTemplate;

 /**
  * 根據(jù) id 獲取一個(gè)對(duì)象
  *
  * @param id id
  * @return 根據(jù) id 查詢到的對(duì)象,如果沒有查到則為 null
  */
 public User get(Integer id) {
  return jdbcTemplate.queryForObject("select * from user where id = &#63;", userRowMapper, id);
 }

 /**
  * 查詢?nèi)坑脩?
  *
  * @return 全部用戶列表
  */
 public List<User> listForAll() {
  return jdbcTemplate.query("select * from user", userRowMapper);
 }

 /**
  * 根據(jù) id 刪除用戶
  *
  * @param id 用戶 id
  * @return 受影響行數(shù)
  */
 public int deleteById(Integer id) {
  return jdbcTemplate.update("delete from user where id = &#63;", id);
 }
}

接下來才是正事,測(cè)試 Dao 層需要加載 Spring 容器,自動(dòng)回滾以避免污染數(shù)據(jù)庫。

/**
 * {@code @SpringBootTest} 和 {@code @RunWith(SpringRunner.class)} 是必須的,這里貌似一直有人誤會(huì)需要使用 {@code @RunWith(SpringJUnit4ClassRunner.class)},但其實(shí)并不需要了
 * 下面的 {@code @Transactional} 和 {@code @Rollback}則是開啟事務(wù)控制以及自動(dòng)回滾
 *
 * @author rxliuli
 */
@SpringBootTest
@RunWith(SpringRunner.class)
@Transactional
@Rollback
public class UserDaoTest {
 @Autowired
 private UserDao userDao;

 @Test
 public void get() {
  int id = 1;
  User result = userDao.get(id);
  //斷言 id 和 get id 相同
  assertThat(result)
    .extracting(User::getId)
    .contains(id);
 }

 @Test
 public void listForAll() {
  List<User> userList = userDao.listForAll();
  //斷言不為空
  assertThat(userList)
    .isNotEmpty();
 }

 @Test
 public void deleteById() {
  int result = userDao.deleteById(1);
  assertThat(result)
    .isGreaterThan(0);
 }
}

Web 測(cè)試

與傳統(tǒng)的 SpringTest 一樣,SpringBoot 也分為兩種。

  • 獨(dú)立安裝測(cè)試:

手動(dòng)加載單個(gè) Controller,所以測(cè)試其他 Controller 中的接口會(huì)發(fā)生異常。但測(cè)試速度上較快,所以應(yīng)當(dāng)優(yōu)先選擇。

  • 集成 Web 環(huán)境測(cè)試:

將啟動(dòng)并且加載所有的 Controller, 所以效率上之于 BaseWebUnitTest 來說非常低下, 僅適用于集成測(cè)試多個(gè) Controller 時(shí)使用。

獨(dú)立安裝測(cè)試

主要是設(shè)置需要使用的 Controller 實(shí)例,然后用獲得 MockMvc 對(duì)象進(jìn)行測(cè)試即可。

/**
 * @author rxliuli
 */
@SpringBootTest
@RunWith(SpringRunner.class)
@Transactional
@Rollback
public class UserControllerUnitTest {
 @Autowired
 private UserController userController;
 /**
  * 用于測(cè)試 API 的模擬請(qǐng)求對(duì)象
  */
 private MockMvc mockMvc;

 @Before
 public void before() {
  //模擬一個(gè) Mvc 測(cè)試環(huán)境,獲取一個(gè) MockMvc 實(shí)例
  mockMvc = MockMvcBuilders.standaloneSetup(userController)
    .build();
 }

 @Test
 public void testGet() throws Exception {
  //測(cè)試能夠正常獲取
  Integer id = 1;
  mockMvc.perform(
    //發(fā)起 get 請(qǐng)求
    get("/user/" + id)
  )
    //斷言請(qǐng)求的狀態(tài)是成功的(200)
    .andExpect(status().isOk())
    //斷言返回對(duì)象的 id 和請(qǐng)求的 id 相同
    .andExpect(jsonPath("$.id").value(id));
 }

 @Test
 public void listForAll() throws Exception {
  //測(cè)試正常獲取
  mockMvc.perform(
    //發(fā)起 post 請(qǐng)求
    post("/user/listForAll")
  )
    //斷言請(qǐng)求狀態(tài)
    .andExpect(status().isOk())
    //斷言返回結(jié)果是數(shù)組
    .andExpect(jsonPath("$").isArray())
    //斷言返回?cái)?shù)組不是空的
    .andExpect(jsonPath("$").isNotEmpty());
 }
}

集成 Web 環(huán)境測(cè)試

/**
 * @author rxliuli
 */
@SpringBootTest
@RunWith(SpringRunner.class)
@Transactional
@Rollback
public class UserControllerIntegratedTest {
 @Autowired
 private WebApplicationContext context;
 /**
  * 用于測(cè)試 API 的模擬請(qǐng)求對(duì)象
  */
 private MockMvc mockMvc;

 @Before
 public void before() {
  //這里把整個(gè) WebApplicationContext 上下文都丟進(jìn)去了,所以可以測(cè)試所有的 Controller
  mockMvc = MockMvcBuilders.webAppContextSetup(context)
    .build();
 }

 @Test
 public void testGet() throws Exception {
  //測(cè)試能夠正常獲取
  Integer id = 1;
  mockMvc.perform(
    //發(fā)起 get 請(qǐng)求
    get("/user/" + id)
  )
    //斷言請(qǐng)求的狀態(tài)是成功的(200)
    .andExpect(status().isOk())
    //斷言返回對(duì)象的 id 和請(qǐng)求的 id 相同
    .andExpect(jsonPath("$.id").value(id));
 }

 @Test
 public void listForAll() throws Exception {
  //測(cè)試正常獲取
  mockMvc.perform(
    //發(fā)起 post 請(qǐng)求
    post("/user/listForAll")
  )
    //斷言請(qǐng)求狀態(tài)
    .andExpect(status().isOk())
    //斷言返回結(jié)果是數(shù)組
    .andExpect(jsonPath("$").isArray())
    //斷言返回?cái)?shù)組不是空的
    .andExpect(jsonPath("$").isNotEmpty());
 }
}

關(guān)于使用SpringBoot如何實(shí)現(xiàn)一個(gè)測(cè)試功能問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI