Devtools与单元测试
devtools
vuejs/devtools: Vue.js 开发者工具,这是一个浏览器插件,可以安装在 Chrome 和 Firefox 等现代浏览器中,用于调试 Vue 应用程序,提供了组件树查看、状态快照、时间旅行等高级调试功能。
项目地址:https://gitcode.com/gh_mirrors/de/devtools
免费下载资源
·
Devtools与单元测试
1.Devtools
1.1 体验Devtools
1)pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yl</groupId>
<artifactId>devtools</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>devtools</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2)测试,没改之前访问接口
3)改动代码后,不用重启项目只需编译一下项目就发现服务会自动重启,注意:devtools只会在dev时才有效
1.2.静态资源修改后,重启项目的配置
1)在application.properties文件中加入这行即可
1.2.禁用自动重启
1)在application.properties配置
2)或者在启动类加
1.2.Trigger控制重启行为
1)加上这行配置
2)再新建一个.reloadTrigger的文本文件,里面什么内容都可以,原理就是这个文本的内容一旦发生改变,项目就会重启
2.单元测试
2.1 单元测试简介
1)单元测试的依赖是在创建项目时,默认就加入的
2)其核心模块就是这两个
2.2 Spring Boot Mock测试
1)实体类
package com.yl.test.domain;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
2)controller
package com.yl.test.controller;
import com.yl.test.domain.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@GetMapping("/user/{id}")
public User getUserById(@PathVariable Integer id) {
User user = new User();
user.setId(id);
user.setUsername("root");
user.setPassword("123");
return user;
}
}
3)测试,调用controller的get请求
package com.yl.test;
import com.yl.test.controller.UserController;
import com.yl.test.dao.UserDao;
import com.yl.test.domain.User;
import com.yl.test.service.UserService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MockMvcBuilder;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
/**
* webEnvironment:指定web应用环境
*
* -MOCK 虚拟的servlet环境
* -RANDOM_PORT 随机端口,提供一个真正的servlet环境
* -DEFINED_PORT 默认端口,提供一个真正的servlet环境
* -NONE 不提供web环境
*
* classes:指定应用启动类
*/
@SpringBootTest
class TestApplicationTests {
@Autowired
WebApplicationContext webApplicationContext;
MockMvc mockMvc;
//获取mockMvc实例
@BeforeEach
void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
mockMvc = MockMvcBuilders.standaloneSetup(new UserController()).build();
}
@Test
void contextLoads() throws Exception {
//发送get请求
mockMvc.perform(MockMvcRequestBuilders.get("/user/12").accept(MediaType.APPLICATION_JSON_UTF8))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string("{\"id\":12,\"username\":\"root\",\"password\":\"123\"}"))
.andDo(MockMvcResultHandlers.print());
}
}
结果
2.3 Spring Boot @MockBean模拟数据测试
1)dao
package com.yl.test.dao;
import com.yl.test.domain.User;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
public User getUserById(Integer id) {
User user = new User();
user.setId(id);
user.setUsername("root");
user.setPassword("root");
return user;
}
}
2)service
package com.yl.test.service;
import com.yl.test.dao.UserDao;
import com.yl.test.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
UserDao userDao;
public User getUserById(Integer id) {
return userDao.getUserById(id);
}
}
3)测试,发现获取到的user对象并不是userDao提供的,而是MockBean模拟提供的
GitHub 加速计划 / de / devtools
24.59 K
4.14 K
下载
vuejs/devtools: Vue.js 开发者工具,这是一个浏览器插件,可以安装在 Chrome 和 Firefox 等现代浏览器中,用于调试 Vue 应用程序,提供了组件树查看、状态快照、时间旅行等高级调试功能。
最近提交(Master分支:2 个月前 )
79116147 - 4 个月前
f0359002 - 4 个月前
更多推荐
已为社区贡献1条内容
所有评论(0)