Spring Boot 2.X 如何快速集成單元測試?


本文將詳細介紹下使用Spring Boot 2.X 集成單元測試,對API(Controller)測試的過程。

一、實現原理

使用MockMvc發起請求,然后執行API中相應的代碼,在執行的過程中使mock模擬底層數據的返回,最后結果驗證。

二、常用注解介紹

@SpringBootTest是SpringBoot的一個用於測試的注解,通過SpringApplication在測試中創建ApplicationContext。

@AutoConfigureMockMvc是用於自動配置MockMvc。

@RunWith在JUnit中有很多個Runner,他們負責調用你的測試代碼,每一個Runner都有各自的特殊功能,你要根據需要選擇不同的Runner來運行你的測試代碼。

@Before在每個測試方法前執行,一般用來初始化方法。

@After在每個測試方法后執行,在方法執行完成后要做的事情。

三、主要代碼

  1. 引入測試jar包
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
  1. 測試類中添加注解和測試代碼
package com.example.helloSpringBoot;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.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;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {HelloSpringBootApplication.class})
@AutoConfigureMockMvc  //測試接口用
public class HelloControllerTest {

    private static final Logger log = LoggerFactory.getLogger(HelloControllerTest.class);

    @Before
    public void testBefore(){
        log.info("測試前");
    }

    @After
    public void testAfter(){
        log.info("測試后");
    }

    @Autowired
    private MockMvc mockMvc;

    /**
     *  測試 /mockTest
     *
     *
     */
    @Test
    public void mockTest()throws Exception{
        MvcResult mvcResult=mockMvc.perform(MockMvcRequestBuilders.get("/mockTest")).
                andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
        int status=mvcResult.getResponse().getStatus();
        //打印出狀態碼,200就是成功
        log.info("狀態碼="+status);
        Assert.assertEquals(200,status);
    }

}
  1. 運行mockTest

運行成功后截圖如下:

 

 

上述三步操作完成后即可實現對API(Controller)測試,有問題歡迎留言溝通哦!

完整源碼地址:https://github.com/suisui2019/helloSpringBoot

推薦閱讀

1.Spring Boot入門-快速搭建web項目
2.Spring Boot 2.X 整合Redis
3.Spring Boot 2.X 如何優雅的解決跨域問題?
4.Spring Boot 2.X 如何添加攔截器?
5.Spring Boot 2.X 集成spring session實現session共享
6.Redis Cluster搭建高可用Redis服務器集群
7.為什么單線程的Redis這么快?
8.一篇文章搞定SpringMVC參數綁定
9.SpringMVC+Mybatis 如何配置多個數據源並切換?

 

限時領取免費Java相關資料,涵蓋了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo/Kafka、Hadoop、Hbase、Flink等高並發分布式、大數據、機器學習等技術。

資料傳送門:  https://mp.weixin.qq.com/s/u2b_NVNuMuAPE0w4lc45fw

關注下方公眾號即可免費領取:

Java碎碎念公眾號

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM