SpringBoot單元測試與Idea熱部署、讀取配置文件


一、亂碼解決

二、單元測試

三、idea中springboot熱部署

四、springboot配置文件讀取屬性

 

一、亂碼解決

  方式一 、

   @RequestMapping(value = "/demo", produces = "application/json;charset=utf-8")

  方式二、

# 設置utf-8,
spring.http.encoding.force-response=true

     項目一般配置server.tomcat.uri-encoding=utf-8

二、單元測試

引入pom.xml 依賴

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

編寫測試類注意@SpringBootTest與@RunWith配合

package city.albert.springboot01;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
* @SpringBootTest 注解表明是springboot的測試類需要加載applicationContext的上下文
* @RunWith 啟動測試類,SpringRunner.class 指定springboot方式加載
*/
@SpringBootTest
@RunWith(SpringRunner.class)
class Springboot01ApplicationTests {

@Test
void contextLoads() {
//業務測試
}
}

三、idea中springboot熱部署

1、引入pom文件,devtools是springboot的熱部署包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

2.idea配置

idea桌的Filë́->̓Settings̈́配置文件中->Compiler設置(或者File->Other Settings->Default Settings->Compiler)

 

 然后點擊下面的apply 進行配置生效,然后點擊ok

 

使用快捷鍵“Ctrl+Shift+Alt+/” 選擇Maintenance中的選項框Registry然后確認,如下圖,勾選compiler.automake.allow.when.app.running,然后在close

 

四、springboot配置文件讀取屬性

 

 

 

 


免責聲明!

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



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