Spring Boot 單元測試、常用配置

Spring Boot 單元測試

 

概述

主要是通過 @RunWith@SpringBootTest 註解來開啓單元測試功能

package com.funtl.hello.spring.boot;

import org.junit.Before;
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.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.net.URL;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = HelloSpringBootApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloSpringBootApplicationTests {

    @LocalServerPort
    private int port;

    private URL base;

    @Autowired
    private TestRestTemplate template;

    @Before
    public void setUp() throws Exception {
        this.base = new URL("http://localhost:" + port + "/");
    }

    @Test
    public void contextLoads() {
        ResponseEntity<String> response = template.getForEntity(base.toString(), String.class);
        assertThat(response.getBody(), equalTo("Hello Spring Boot"));
    }

}

運行它會先啓動 Spring Boot 工程,再啓動單元測試

 

Spring Boot 常用配置

 

概述

本章節主要介紹一下 Spring Boot 中的一些常用配置,比如:自定義 Banner、配置日誌、關閉特定的自動配置等。

自定義 Banner

在 Spring Boot 啓動的時候會有一個默認的啓動圖案

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.8.RELEASE)

我們在 src/main/resources 目錄下新建一個 banner.txt

通過 http://patorjk.com/software/taag

網站生成字符串,將網站生成的字符複製到 banner.txt 中

再次運行這個程序

${AnsiColor.BRIGHT_RED}
////////////////////////////////////////////////////////////////////
//                          _ooOoo_                               //
//                         o8888888o                              //
//                         88" . "88                              //
//                         (| ^_^ |)                              //
//                         O\  =  /O                              //
//                      ____/`---'\____                           //
//                    .'  \\|     |//  `.                         //
//                   /  \\|||  :  |||//  \                        //
//                  /  _||||| -:- |||||-  \                       //
//                  |   | \\\  -  /// |   |                       //
//                  | \_|  ''\---/''  |   |                       //
//                  \  .-\__  `-`  ___/-. /                       //
//                ___`. .'  /--.--\  `. . ___                     //
//              ."" '<  `.___\_<|>_/___.'  >'"".                  //
//            | | :  `- \`.;`\ _ /`;.`/ - ` : | |                 //
//            \  \ `-.   \_ __\ /__ _/   .-` /  /                 //
//      ========`-.____`-.___\_____/___.-`____.-'========         //
//                           `=---='                              //
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //
//            佛祖保佑       永不宕機     永無BUG                  //
////////////////////////////////////////////////////////////////////

常用屬性設置:

  • ${AnsiColor.BRIGHT_RED}:設置控制檯中輸出內容的顏色
  • ${application.version}:用來獲取 MANIFEST.MF 文件中的版本號
  • ${application.formatted-version}:格式化後的 ${application.version} 版本信息
  • ${spring-boot.version}:Spring Boot 的版本號
  • ${spring-boot.formatted-version}:格式化後的 ${spring-boot.version} 版本信息

配置文件

Spring Boot 項目使用一個全局的配置文件 application.properties 或者是 application.yml,在 resources 目錄下或者類路徑下的 /config 下,一般我們放到 resources 下。

修改 Tomcat 的端口爲 9090,並將默認的訪問路徑 "/" 修改爲 "boot",可以在 application.properties 中添加:

server.port=9090
server.context-path=/boot

或在 application.yml 中添加:

server:
  port: 9090
  context-path: /boot

測試效果:

 

Starter POM

Spring Boot 爲我們提供了簡化企業級開發絕大多數場景的 starter pom ,只要使用了應用場景所需要的 starter pom ,相關的技術配置將會消除,就可以得到 Spring Boot 爲我們提供的自動配置的 Bean。

 

日誌配置

Spring Boot 對各種日誌框架都做了支持,我們可以通過配置來修改默認的日誌的配置

默認情況下,Spring Boot 使用 Logback 作爲日誌框架

logging:
  file: ../logs/spring-boot-hello.log
  level.org.springframework.web: DEBUG

 

關閉特定的自動配置

關閉特定的自動配置使用 @SpringBootApplication 註解的 exclude 參數即可,這裏以關閉數據源的自動配置爲例

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章