SpringBoot使用MockMvc进行单元测试的实例代码
作者:冷风扇666
在Spring Boot应用程序中,使用MockMvc进行单元测试是一种有效的方式,可以验证控制器的行为和API的正确性,在这篇博客中,我们将介绍如何使用MockMvc对用户控制器进行测试,感兴趣的朋友可以参考下
引言
在Spring Boot应用程序中,使用MockMvc进行单元测试是一种有效的方式,可以验证控制器的行为和API的正确性。在这篇博客中,我们将介绍如何使用MockMvc对用户控制器(UserController)进行测试,该控制器涉及用户信息的增删改查操作。
UserController测试类
package com.lfsun.mockmvc; import com.fasterxml.jackson.databind.ObjectMapper; import com.lfsun.mockmvc.entity.User; import com.lfsun.mockmvc.service.UserService; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import java.util.Arrays; import java.util.List; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class UserControllerTest { @Autowired private MockMvc mockMvc; @Autowired private UserService userService; @Autowired private ObjectMapper objectMapper; @Before public void setUp() { // 在测试前初始化一些数据,或者使用Mockito进行一些mock设置 } @Test public void getAllUsersTest() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get("/user/getAllUsers")) .andExpect(status().isOk()) .andExpect(jsonPath("$").isArray()) .andExpect(jsonPath("$.length()").value(userService.getAllUsers().size())) .andDo(print()); } @Test public void getUserByIdTest() throws Exception { Long userId = 1L; mockMvc.perform(MockMvcRequestBuilders.get("/user/getUserById/{userId}", userId)) .andExpect(status().isOk()) .andExpect(jsonPath("$.userId").value(userId)) .andDo(print()); } @Test public void addUserTest() throws Exception { User user = new User(); user.setUsername("testUser"); user.setEmail("test@lfsun.com"); mockMvc.perform(MockMvcRequestBuilders.post("/user/addUser") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(user))) .andExpect(status().isOk()) .andExpect(jsonPath("$.username").value("testUser")) .andExpect(jsonPath("$.email").value("test@lfsun.com")) .andDo(print()); } @Test public void updateUserTest() throws Exception { Long userId = 1L; User updatedUser = new User(); updatedUser.setUserId(userId); updatedUser.setUsername("updatedUser"); updatedUser.setEmail("updated@lfsun.com"); mockMvc.perform(MockMvcRequestBuilders.put("/user/updateUser") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(updatedUser))) .andExpect(status().isOk()) .andExpect(jsonPath("$.userId").value(userId)) .andExpect(jsonPath("$.username").value("updatedUser")) .andExpect(jsonPath("$.email").value("updated@lfsun.com")) .andDo(print()); } @Test public void deleteUserTest() throws Exception { Long userId = 1L; mockMvc.perform(MockMvcRequestBuilders.delete("/user/deleteUser/{userId}", userId)) .andExpect(status().isOk()) .andDo(print()); } }
解释和注意事项
- MockMvc的注入:
@Autowired
注解用于将MockMvc实例注入测试类中。 - 数据初始化: 使用
@Before
注解的setUp
方法,可以在测试前进行一些数据初始化或使用Mockito进行一些mock设置。 - 测试方法: 每个测试方法使用MockMvc执行相应的HTTP请求,并使用断言验证控制器的响应是否符合预期。
- 数据序列化:
ObjectMapper
负责将对象序列化为JSON,以便在POST和PUT请求中传递。
以下附上完整代码
controller:
package com.lfsun.mockmvc.controller; import com.lfsun.mockmvc.entity.User; import com.lfsun.mockmvc.service.UserService; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; import java.util.List; /** * 用户 Controller * * @author CL */ @RestController @RequestMapping(value = "/user") @RequiredArgsConstructor public class UserController { private final UserService userService; // 查询所有用户 @GetMapping("/getAllUsers") public List<User> getAllUsers() { return userService.getAllUsers(); } // 根据用户ID查询用户 @GetMapping("/getUserById/{userId}") public User getUserById(@PathVariable Long userId) { return userService.getUserById(userId); } // 添加用户 @PostMapping("/addUser") public User addUser(@RequestBody User user) { return userService.addUser(user); } // 更新用户信息 @PutMapping("/updateUser") public User updateUser(@RequestBody User user) { return userService.updateUser(user); } // 根据用户ID删除用户 @DeleteMapping("/deleteUser/{userId}") public void deleteUser(@PathVariable Long userId) { userService.deleteUser(userId); } }
service、serviceimpl
package com.lfsun.mockmvc.service; import com.lfsun.mockmvc.entity.User; import java.util.List; /** * 用户 Service 接口 */ public interface UserService { // 查询所有用户 List<User> getAllUsers(); // 根据用户ID查询用户 User getUserById(Long userId); // 添加用户 User addUser(User user); // 更新用户信息 User updateUser(User user); // 根据用户ID删除用户 void deleteUser(Long userId); }
package com.lfsun.mockmvc.service.impl; import com.lfsun.mockmvc.entity.User; import com.lfsun.mockmvc.mapper.UserMapper; import com.lfsun.mockmvc.service.UserService; import lombok.AllArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * 用户 Service 实现类 */ @Service @AllArgsConstructor // Lombok 自动生成构造函数 public class UserServiceImpl implements UserService { private final UserMapper userMapper; @Override public List<User> getAllUsers() { return userMapper.getAllUsers(); } @Override public User getUserById(Long userId) { return userMapper.getUserById(userId); } @Override public User addUser(User user) { userMapper.addUser(user); return user; } @Override public User updateUser(User user) { userMapper.updateUser(user); return user; } @Override public void deleteUser(Long userId) { userMapper.deleteUser(userId); } }
mapper
package com.lfsun.mockmvc.mapper; import com.lfsun.mockmvc.entity.User; import org.apache.ibatis.annotations.*; import java.util.List; /** * 用户 Mapper 接口 */ @Mapper public interface UserMapper { List<User> getAllUsers(); User getUserById(Long userId); void addUser(User user); void updateUser(User user); void deleteUser(Long userId); }
实体类:
package com.lfsun.mockmvc.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class User { private Long userId; private String username; private String email; }
xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.lfsun.mockmvc.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.lfsun.mockmvc.entity.User"> <id column="user_id" property="userId" jdbcType="BIGINT"/> <result column="username" property="username" jdbcType="VARCHAR"/> <result column="email" property="email" jdbcType="VARCHAR"/> </resultMap> <!-- 查询所有用户 --> <select id="getAllUsers" resultMap="BaseResultMap"> SELECT * FROM user; </select> <!-- 根据用户ID查询用户 --> <select id="getUserById" resultMap="BaseResultMap" parameterType="java.lang.Long"> SELECT * FROM user WHERE user_id = #{userId}; </select> <!-- 添加用户 --> <insert id="addUser" parameterType="com.lfsun.mockmvc.entity.User"> INSERT INTO user (username, email) VALUES (#{username}, #{email}); </insert> <!-- 更新用户信息 --> <update id="updateUser" parameterType="com.lfsun.mockmvc.entity.User"> UPDATE user SET username = #{username}, email = #{email} WHERE user_id = #{userId}; </update> <!-- 根据用户ID删除用户 --> <delete id="deleteUser" parameterType="java.lang.Long"> DELETE FROM user WHERE user_id = #{userId}; </delete> </mapper>
配置
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/mockmvc?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true username: root password: 123321 mybatis: mapper-locations: classpath:/mapper/*.xml # 指定 MyBatis mapper 文件的位置 configuration: map-underscore-to-camel-case: true # 配置驼峰命名规则
maven
<?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.7.14</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.lfsun</groupId> <artifactId>lfsun-study-mockmvc</artifactId> <version>0.0.1-SNAPSHOT</version> <name>lfsun-study-mockmvc</name> <description>lfsun-study-mockmvc</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
最后
以上就是SpringBoot使用MockMvc进行单元测试的实例代码的详细内容,更多关于SpringBoot MockMvc单元测试的资料请关注脚本之家其它相关文章!