AssertUtils.java 3.7 KB

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