Brian的雜記
  • Introduction
  • Brian's 雜記
    • My Awesome API
    • FB 大頭貼
    • 硬體雜記
    • PHP
    • project
      • 模擬器
      • WAMP
    • WinMerge
    • 雜記
      • LINQ
      • 方法
      • Grid View
      • namespace
      • global
      • 物件導向
      • Excel
      • VS2017
      • single sign on
      • Master
      • Https
      • 憑證
      • 略過憑證不符
      • NLog
      • 團隊開發
      • .NET Core
      • 共用網路上的芳鄰
      • 爬蟲
      • NPOI
      • RSS
      • 多執行緒
      • 記憶體回收
      • 密碼學
        • BCrypt
        • AES
      • 主機環境建置
      • Session
      • Error
      • IIS 相關
      • 無障礙相關
      • 介面
        • 影像地圖
      • telnet
        • smtp
      • nslookup
      • 協助客戶解決問題
      • 驗證欄位
      • 網站管理
      • 工具整理
    • 正規表示法
    • 影像處理
    • IntelliJ Idea
    • 觀念
      • Clean Code
        • 命名
        • 函式
        • 註解
        • 編排
        • 物件及資料結構
        • 錯誤處理
        • 邊界
        • 單元測試
        • 類別
      • Java 程式風格
      • Design Pattern
        • 單例模式
      • 同步
      • 畫圖
        • ER-Model
        • 類別圖
        • Use Case
        • 有限狀態機
      • 資料 API 文件 分析
      • CORS & SSL
      • 利用DISC幫助溝通
      • OAS
    • 檔案上傳
      • FileStore
      • App Engine
      • Google Storage
    • OAuth vs Open ID
    • MIME
    • 虛擬桌面
    • 待看資料
    • Selenium
    • CDN
    • HTTP
    • 編碼
    • 2nd-ML100Days
      • jupyter
    • 微服務
      • 設計
        • 1 ~ 5
        • 6
        • 7
        • 8
        • 9
    • Gradle
    • Maven
    • Error
    • 批次檔 BAT
    • Kurento
    • WebSocket & WebRTC
  • 需求面能力
    • User Story
  • Google Cloud Platform
    • Compute Engine
  • Python
    • 基本語法
    • Pandas
    • 套件
    • Matplotlib
    • Encoder
    • jupyter
  • Java
    • Java
      • File
      • Exception
      • 物件導向觀念
      • 加密
      • HTTP
      • 集合
      • Stream()
      • Web
      • ResultSet
      • JDK6
      • JDK8
    • 讀取、複寫MP3 Tag
    • Log4j2
    • Servlet
      • 容器
    • JSP
    • JBOSS
    • JWT
    • PreparedStatement
    • Error
    • Spring
      • Spring Boot
        • @Value
        • Build
      • RequestParameter
      • Error
      • Autowired
      • JPA
      • FeignClient
      • WebSocket
      • thymeleaf
      • Security
      • Test
      • Scheduled
      • Redirect
    • IntelliJ
  • Linux
    • Linux
    • Shell Script
    • Cygwin(在Windows執行Linux指令)
  • 前端
    • HTML
      • Link
    • CSS
      • Position
      • padding color
      • display
    • JS
      • jQuery
        • Select2
      • fancybox
      • ES6
      • 效能
      • GoogleMap API
        • Marker
        • InfoWindow
      • 事件
      • CKEditor
      • TGOS
      • JSON
      • QRcode
      • 核心概念
        • 物件 變數 型別
          • number
          • String
          • boolean
          • null & undefined
          • Symbol
        • JS 物件概念
        • 深入理解JS 函式物件
        • 更多ES2015/ES6 全新語言特性
      • Promise
    • 效果應用
  • 資料庫
    • 注意事項
    • MariaDB
    • MySQL
      • inner join 和 join
      • 字串比對
      • 倒數資料
    • SQL
      • DDL 資料定義語言
      • DML 資料操縱語言
      • DCL 資料控制語言
      • TCL 交易控制語言
      • T-SQL
      • CTE
      • JOIN
    • Oracle
    • MSSQL 操作
      • 新增使用者
      • SQL 指令
      • Sequence
    • 差異比較
    • MyBatis
    • Workbench 操作
    • SQL Injection
  • 版本控制
    • Gitlab
      • sign up
      • sign in
      • add project
      • add members
    • SourceGit
      • install
    • SmartGit
      • install
      • operate
      • git 操作雜記
    • TortoiseGit
    • Git
    • TFS
    • SVN
  • Test
    • 軟體測試原則
    • 演算法
    • XMind
      • install
    • Jenkins
      • 建置
    • HTTPie
    • Postman
    • 測試驅動開發
    • 撰寫測試的觀念
    • 測試框架
    • IoC & DI
    • 隔絕相依性的方式
    • JUnit
    • NUnit
    • 習慣
    • 虛設常式
  • Angular
    • hello world
    • ng-options
    • ES6
    • Build & Conponect
    • HttpClient
    • 部署
  • ASP.NET Web Form
    • Chapter 2
      • 2-1
        • 小東西
    • 略過請求驗證
  • Go
Powered by GitBook
On this page
  • 取得 profile 內的 server.port
  • Conditional Test 根據註解的條件去決定是否執行測試
  • MockMvc
  • 基本建置
  • 參數設定
  • 範例
  • Response 亂碼

Was this helpful?

  1. Java
  2. Spring

Test

取得 profile 內的 server.port

當我們想要取得 yml 裡面設定的 port 時,會使用 @Value("${server.port}")

如果想要取得其他屬性,就修改大括號內的值,而在一般的屬性,都可以這樣直接取

但這裡面有一些隱藏的坑,像是取 port 的時候,會如下表

yml \ code

main

test

applicaition.yml

O

O

application-local.yml

O

X

這個表的意思是說,如果你直接把 server.port 寫在 application.yml 裡面,那很簡單,在 main 跟 test 裡面,都可以用上述方法直接取得。

但因為不同環境,會有相對應的配置文件,我們可以在 application.yml 只填寫

spring: 
  profiles: 
    active: local

而將 local 環境的配置,寫在 application-local.yml 這個配置文件裡

server: 
  port: 9999

這樣我們就可以根據不同的環境,在環境的 application.yml 去選擇對應的配置文件

但這樣當我們在測試程式裡,就沒辦法簡單的抓到 server.port 了

必須在我們的測試程式的類別上,加上這個註解

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)

Conditional Test 根據註解的條件去決定是否執行測試

@EnabledOnOs 假如符合條件的作業系統,則執行這段測試

@Test
@EnabledOnOs({OS.WINDOWS, OS.MAC})
public void test(){
    //...
}

Super-powers are granted randomly so please submit an issue if you're not happy with yours.

Once you're strong enough, save the world:

// Ain't no code for that yet, sorry
echo 'You got to trust me on this, I saved the world'

MockMvc

基本建置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

注意 此範例為 spring boot 2.1.2

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ProductTest {

    @Autowired
    private MockMvc mockMvc;

}

當spring boot 改為2.4.3時,要改為以下寫法

@RunWith(SpringRunner.class)
改為
@ExtendWith(SpringExtension.class)

參數設定

測試程式和原本業務邏輯的程式一樣,也有自己的設定檔。 因此請在src\test路徑下新增「resources」資料夾,建立「application.properties」檔案。 並添加測試程式使用的資料庫專屬位址。請不要與開發中程式使用相同的資料庫,避免互相影響。

範例

@Test
public void testCreateProduct() throws Exception {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.add("Content-Type", "application/json");

    JSONObject request = new JSONObject();
    request.put("name", "Harry Potter");
    request.put("price", 450);

    RequestBuilder requestBuilder =
            MockMvcRequestBuilders
                    .post("/products")
                    .headers(httpHeaders)
                    .content(request.toString());

    mockMvc.perform(requestBuilder)
            .andDo(print())
            .andExpect(status().isCreated())
            .andExpect(jsonPath("$.id").hasJsonPath())
            .andExpect(jsonPath("$.name").value(request.getString("name")))
            .andExpect(jsonPath("$.price").value(request.getInt("price")))
            .andExpect(header().exists("Location"))
            .andExpect(header().string("Content-Type", "application/json;charset=UTF-8"));
}

請求發出後,隨即透過「andExpect」方法進行回應資料的驗證。以下介紹在範例中使用到的驗證參數。

  1. 「status().isCreated()」:驗證HTTP狀態碼應為201。讀者可自行探索其他狀態,如「isOk」、「isNotFound」等。或透過「is」方法直接傳入狀態碼。

  2. 「jsonPath()」:指定JSON欄位。以「$」符號開始,使用「.」符號深入下一層的路徑。

  3. 「hasJsonPath()」:驗證某個JSON欄位存在。

  4. 「value()」:驗證某個JSON欄位值為何。

  5. 「header().exists()」:驗證回應標頭中的某欄位存在。

  6. 「header().string()」:驗證回應標頭中的某欄位值為何。

Response 亂碼

protected ResultActions getResponseEncoding(RequestBuilder builder) throws Exception {
        ResultActions resultActions = mockMvc.perform(builder);
        resultActions
                .andReturn()
                .getResponse()
                .setCharacterEncoding("UTF-8");
        return resultActions;
}

PreviousSecurityNextScheduled

Last updated 3 years ago

Was this helpful?

參考資料:springboot test @value(“$ {server.port}”)()

參考資料:()

http://cn.voidcc.com/question/p-vkkqsybe-tz.html
https://medium.com/chikuwa-tech-study/spring-boot-%E7%AC%AC9%E8%AA%B2-%E4%BD%BF%E7%94%A8mockmvc%E8%87%AA%E5%8B%95%E5%8C%96%E6%B8%AC%E8%A9%A6-%E4%B8%80-3e3d031f8d68