一张图说明Spring boot中单元测试

一张图说明Spring boot单元测试

单元测试代码一般放在src/test目录下,与src/main结构基本一致

一、添加测试依赖包

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-test</artifactId>
  4. <scope>test</scope>
  5. </dependency>

二、控制器测试

1.控制器类
(1)参数接收类
  1. @Data
  2. public class UserDTO {
  3. private String nickname;
  4. private String account;
  5. private String email;
  6. private String password;
  7. @Override
  8. public String toString() {
  9. return "UserDTO{" +
  10. "nickname='" + nickname + '\'' +
  11. ", account='" + account + '\'' +
  12. ", email='" + email + '\'' +
  13. ", password='" + password + '\'' +
  14. '}';
  15. }
  16. }
(2)控制器类
  1. /**
  2. * @author shixinke
  3. * @version 1.0
  4. * @Description
  5. * @Date 19-2-1 下午1:07
  6. */
  7. @RestController
  8. @RequestMapping("/user")
  9. @Slf4j
  10. public class UserController {
  11. @Resource
  12. private UserService userService;
  13. @PostMapping("register")
  14. public Result register(UserDTO userDTO) {
  15. Result result = null;
  16. try {
  17. result = userService.create(userDTO);
  18. } catch (Exception ex) {
  19. log.error("注册出错:", ex);
  20. result = Result.error(ex.getMessage());
  21. }
  22. return result;
  23. }
  24. }
2.业务服务类
(1)业务服务接口
  1. public interface UserService {
  2. public Result create(UserDTO userDTO);
  3. }
(2)业务服务实现
  1. @Service
  2. public class UserServiceImpl implements UserService {
  3. @Resource
  4. private KafkaMQProducerService producerService;
  5. @Override
  6. public Result create(UserDTO userDTO) {
  7. User user = new User(userDTO.getNickname(), userDTO.getEmail(), userDTO.getAccount(), userDTO.getPassword(), 0, Instant.now().getEpochSecond());
  8. /**
  9. * 数据操作在此不再赘述(插入数据库,更新缓存)
  10. */
  11. user.setUserId(3000001);
  12. return Result.success(user.getUserId());
  13. }
  14. }
3.定义测试类
  1. @Slf4j
  2. @RunWith(SpringRunner.class)
  3. @SpringBootTest(classes= UserApplication.class)
  4. @AutoConfigureMockMvc
  5. public class UserControllerTest {
  6. @Resource
  7. private UserController controller;
  8. private MockHttpServletRequest request;
  9. }
4.添加前置方法
  1. @Before
  2. public void before() {
  3. //公共参数初始化等
  4. }
5.添加测试方法
  1. @Test
  2. public void createUser() {
  3. UserDTO userDTO = new UserDTO();
  4. userDTO.setAccount("test888");
  5. userDTO.setEmail("test@qq.com");
  6. userDTO.setNickname("测试1");
  7. userDTO.setPassword("888888");
  8. Result result = controller.register(userDTO);
  9. Assert.isTrue(result.isSuccess(), "注册成功");
  10. }
6.通过IDE运行单元测试

通过IDE(如idea)或者命令行运行单元测试

(1)运行所有测试

右键单击单元测试的包,点击”Run Tests in …”

运行所有测试

运行结果:

运行结果

(2)运行某个测试类或运行某个测试方法

在测试类名称或方法名称前点击按钮

运行某个测试类或测试方法

运行结果:

运行某个测试类或测试方法的结果

三、业务类测试

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class UserServiceTest {
  4. @Resource
  5. private UserService userService;
  6. @Test
  7. public void create() {
  8. UserDTO userDTO = new UserDTO();
  9. userDTO.setAccount("test888");
  10. userDTO.setEmail("test@qq.com");
  11. userDTO.setNickname("测试1");
  12. userDTO.setPassword("888888");
  13. Result result = userService.create(userDTO);
  14. Assert.isTrue(result.isSuccess(), "注册成功");
  15. }
  16. }
  • 依赖注入业务服务类
  • 调用业务服务类的相关方法
  • 判断调用结果是否达到预期