Spring Boot 热部署 与 单元测试

热部署

  1. 添加依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>
  1. 加入 spring-boot-maven-plugin
 <build>
   <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
            </configuration>
        </plugin>
    </plugins>
</build>

配置完成后,我们可以实现所有操作的热部署。如果有的操作不起效果,可能的原因有:

  • spring boot版本不正确。这里使用的是2.0.0版本。
  • 设置了SpringApplication.setRegisterShutdownHook(false)。
  • 页面热部署的实现需要添加配置:spring.thymeleaf.cache=false。

单元测试

  1. 添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
  1. 示例:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class StudentControllerTest{
	// 可以写 Controller 的测试、Service 的测试等。。。。
}

Spring Boot 项目中的测试类,需要在类上定义 @RunWith(SpringRunner.class) ,让测试运行于 Spring 的测试环境,以及注解 @SpringBootTest(classes = App.class),其中 App.class 是项目的启动类。

猜你喜欢

转载自blog.csdn.net/Gnd15732625435/article/details/84942594