热部署
引入依赖包
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
热部署还可以根据实际需求来改变热部署的监控内容和方式,配置项我就不一 一介绍了
测试
依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
IDE搭建测试类
简单测试
@RunWith(SpringRunner.class)
//使用随机端口启动测试服务
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class TestBushuApplicationTests {
@Test
void contextLoads() {
System.out.println("---------简单测试--------");
}
}
Rest接口测试:使用Rest测试模版TestRestTemplate restTemplate
//Rest测试模版
@Autowired
private TestRestTemplate restTemplate;
@Test
void contextLoads() {
System.out.println("---------test99000--------");
//根据具体的返回对象进行封装
String rs= restTemplate.getForObject("/test",String.class,1L);
System.out.println("/结果:"+rs);
}
Mock测试
如果当前服务需要其他服务提供相关的功能,而其他服务还不能正常调度时,我们可以模拟服务返回数据进行调试。
步骤一:构造虚拟对象
步骤二:指定Mock bean对象和参数
步骤三:调用
代码如下:
@MockBean
private UserService userService = null;
@Test
public void testMock(){
//构建虚拟对象
User user = new User(1,"张三",new Date());
//指定MockBean方法和参数
BDDMockito.given(this.userService.getUserById(1)).willReturn(user);
//进行Mock测试
User u = userService.getUserById(1);
System.out.println("测试返回的用户:{}"+u.toString());
}