Blame view

yudao-framework/src/main/java/cn/iocoder/dashboard/util/AssertUtils.java 3.73 KB
533cd1d3   YunaiV   再次调整项目结构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  package cn.iocoder.dashboard.util;
  
  import cn.hutool.core.util.ArrayUtil;
  import cn.hutool.core.util.ReflectUtil;
  import cn.iocoder.dashboard.common.exception.ErrorCode;
  import cn.iocoder.dashboard.common.exception.ServiceException;
  import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil;
  import org.junit.jupiter.api.Assertions;
  import org.junit.jupiter.api.function.Executable;
  
  import java.lang.reflect.Field;
  import java.util.Arrays;
  import java.util.Objects;
  
  import static org.junit.jupiter.api.Assertions.assertThrows;
  
  /**
   * 单元测试,assert 断言工具类
   *
   * @author 芋道源码
   */
  public class AssertUtils {
  
      /**
       * 比对两个对象的属性是否一致
       *
       * 注意,如果 expected 存在的属性,actual 不存在的时候,会进行忽略
       *
       * @param expected 期望对象
       * @param actual 实际对象
       * @param ignoreFields 忽略的属性数组
       */
      public static void assertPojoEquals(Object expected, Object actual, String... ignoreFields) {
          Field[] expectedFields = ReflectUtil.getFields(expected.getClass());
          Arrays.stream(expectedFields).forEach(expectedField -> {
              // 如果是忽略的属性,则不进行比对
              if (ArrayUtil.contains(ignoreFields, expectedField.getName())) {
                  return;
              }
              // 忽略不存在的属性
              Field actualField = ReflectUtil.getField(actual.getClass(), expectedField.getName());
              if (actualField == null) {
                  return;
              }
              // 比对
              Assertions.assertEquals(
                      ReflectUtil.getFieldValue(expected, expectedField),
                      ReflectUtil.getFieldValue(actual, actualField),
                      String.format("Field(%s) 不匹配", expectedField.getName())
              );
          });
      }
  
      /**
       * 比对两个对象的属性是否一致
       *
       * 注意,如果 expected 存在的属性,actual 不存在的时候,会进行忽略
       *
       * @param expected 期望对象
       * @param actual 实际对象
       * @param ignoreFields 忽略的属性数组
       * @return 是否一致
       */
      public static boolean isPojoEquals(Object expected, Object actual, String... ignoreFields) {
          Field[] expectedFields = ReflectUtil.getFields(expected.getClass());
          return Arrays.stream(expectedFields).allMatch(expectedField -> {
              // 如果是忽略的属性,则不进行比对
              if (ArrayUtil.contains(ignoreFields, expectedField.getName())) {
                  return true;
              }
              // 忽略不存在的属性
              Field actualField = ReflectUtil.getField(actual.getClass(), expectedField.getName());
              if (actualField == null) {
                  return true;
              }
              return Objects.equals(ReflectUtil.getFieldValue(expected, expectedField),
                      ReflectUtil.getFieldValue(actual, actualField));
          });
      }
  
      /**
       * 执行方法,校验抛出的 Service 是否符合条件
       *
       * @param executable 业务异常
       * @param errorCode 错误码对象
       * @param messageParams 消息参数
       */
      public static void assertServiceException(Executable executable, ErrorCode errorCode, Object... messageParams) {
          // 调用方法
          ServiceException serviceException = assertThrows(ServiceException.class, executable);
          // 校验错误码
          Assertions.assertEquals(errorCode.getCode(), serviceException.getCode(), "错误码不匹配");
          String message = ServiceExceptionUtil.doFormat(errorCode.getCode(), errorCode.getMsg(), messageParams);
          Assertions.assertEquals(message, serviceException.getMessage(), "错误提示不匹配");
      }
  
  }