SpringBoot单元测试与Idea热部署

一、乱码解决

二、单元测试

三、idea中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

猜你喜欢

转载自www.cnblogs.com/niunafei/p/13195731.html