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)在application.properties文件中加入这行即可
1)在application.properties配置
2)或者在启动类加
1)加上这行配置
2)再新建一个.reloadTrigger的文本文件,里面什么内容都可以,原理就是这个文本的内容一旦发生改变,项目就会重启
1)单元测试的依赖是在创建项目时,默认就加入的
2)其核心模块就是这两个
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());
}
}
结果
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模拟提供的
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_41359273/article/details/122805429
内容来源于网络,如有侵权,请联系作者删除!