资讯专栏INFORMATION COLUMN

对象克隆问题

JeOam / 1810人阅读

摘要:断言不确定度,非强检器具,参量以及附加参量都修改成功。再次执行测试,出现了预期中的错误,证明就是对象引用的问题。

问题描述

编写观察者的单元测试,执行以下测试通过。断言不确定度,非强检器具,参量以及附加参量都修改成功。

</>复制代码

  1. @Test
  2. public void updateTest() {
  3. logger.debug("0. 基础信息准备");
  4. logger.debug("构造计量单位");
  5. MeasurementUnit oldMeasurementUnit = measurementUnitService.getOneSavedMeasurementUnit();
  6. logger.debug("1. 初始化不确定度实体");
  7. logger.debug("获取基础的不确定度");
  8. AccuracyUncertainty accuracyUncertainty1 = accuracyUncertaintyService.getOneUnSavedObject();
  9. AccuracyUncertainty accuracyUncertainty2 = accuracyUncertaintyService.getOneUnSavedObject();
  10. AccuracyUncertainty accuracyUncertainty3 = accuracyUncertaintyService.getOneUnSavedObject();
  11. accuracyUncertaintyService.getOneSavedObject();
  12. logger.debug("设置不确定度1的最小与最大单位");
  13. accuracyUncertainty1.setMinAccuracyUnit(oldMeasurementUnit);
  14. accuracyUncertainty1.setMaxAccuracyUnit(oldMeasurementUnit);
  15. logger.debug("设置不确定度2的最小单位");
  16. accuracyUncertainty2.setMinAccuracyUnit(oldMeasurementUnit);
  17. logger.debug("设置不确定度3的最大单位");
  18. accuracyUncertainty3.setMaxAccuracyUnit(oldMeasurementUnit);
  19. logger.debug("持久化不确定度列表");
  20. List accuracyUncertaintyList = new ArrayList<>();
  21. accuracyUncertaintyList.add(accuracyUncertainty1);
  22. accuracyUncertaintyList.add(accuracyUncertainty2);
  23. accuracyUncertaintyList.add(accuracyUncertainty3);
  24. accuracyUncertaintyRepository.save(accuracyUncertaintyList);
  25. logger.debug("2. 初始化内嵌不确定度");
  26. AccuracyEmbeddable accuracyEmbeddable1 = this.getOneAccuracyEmbeddable();
  27. AccuracyEmbeddable accuracyEmbeddable2 = this.getOneAccuracyEmbeddable();
  28. AccuracyEmbeddable accuracyEmbeddable3 = this.getOneAccuracyEmbeddable();
  29. logger.debug("设置内嵌不确定度1的最大与最小单位");
  30. accuracyEmbeddable1.setMinAccuracyUnit(oldMeasurementUnit);
  31. accuracyEmbeddable1.setMaxAccuracyUnit(oldMeasurementUnit);
  32. logger.debug("设置内嵌不确定度2的最小单位");
  33. accuracyEmbeddable2.setMinAccuracyUnit(oldMeasurementUnit);
  34. logger.debug("设置内嵌不确定度3的最大单位");
  35. accuracyEmbeddable3.setMaxAccuracyUnit(oldMeasurementUnit);
  36. logger.debug("3. 初始化内嵌测量范围");
  37. MeasureScaleEmbeddable measureScaleEmbeddable1 = this.getOneMeasureScaleEmbeddable();
  38. MeasureScaleEmbeddable measureScaleEmbeddable2 = this.getOneMeasureScaleEmbeddable();
  39. MeasureScaleEmbeddable measureScaleEmbeddable3 = this.getOneMeasureScaleEmbeddable();
  40. logger.debug("设置内嵌测量范围1的最大与最小单位");
  41. measureScaleEmbeddable1.setMinMeasureScaleUnit(oldMeasurementUnit);
  42. measureScaleEmbeddable1.setMaxMeasureScaleUnit(oldMeasurementUnit);
  43. logger.debug("设置内嵌测量范围2的最小单位");
  44. measureScaleEmbeddable2.setMinMeasureScaleUnit(oldMeasurementUnit);
  45. logger.debug("设置内嵌测量范围3的最大单位");
  46. measureScaleEmbeddable3.setMaxMeasureScaleUnit(oldMeasurementUnit);
  47. logger.debug("4. 初始化非强检器具");
  48. NonMandatoryInstrument nonMandatoryInstrument1 = nonMandatoryInstrumentService.getOneUnSavedObject();
  49. NonMandatoryInstrument nonMandatoryInstrument2 = nonMandatoryInstrumentService.getOneUnSavedObject();
  50. NonMandatoryInstrument nonMandatoryInstrument3 = nonMandatoryInstrumentService.getOneUnSavedObject();
  51. nonMandatoryInstrumentService.getOneSavedObject();
  52. nonMandatoryInstrument1.setMeasureScale(measureScaleEmbeddable1);
  53. nonMandatoryInstrument2.setMeasureScale(measureScaleEmbeddable2);
  54. nonMandatoryInstrument3.setMeasureScale(measureScaleEmbeddable3);
  55. List nonMandatoryInstrumentList = new ArrayList<>();
  56. nonMandatoryInstrumentList.add(nonMandatoryInstrument1);
  57. nonMandatoryInstrumentList.add(nonMandatoryInstrument2);
  58. nonMandatoryInstrumentList.add(nonMandatoryInstrument3);
  59. nonMandatoryInstrumentRepository.save(nonMandatoryInstrumentList);
  60. logger.debug("5. 初始化参量");
  61. Parameter parameter1 = parameterService.getOneUnsavedObject();
  62. Parameter parameter2 = parameterService.getOneUnsavedObject();
  63. Parameter parameter3 = parameterService.getOneUnsavedObject();
  64. Parameter parameter4 = parameterService.getOneUnsavedObject();
  65. Parameter parameter5 = parameterService.getOneUnsavedObject();
  66. Parameter parameter6 = parameterService.getOneUnsavedObject();
  67. parameterService.getOneSavedObject();
  68. parameter1.setAccuracy(accuracyEmbeddable1);
  69. parameter2.setAccuracy(accuracyEmbeddable2);
  70. parameter3.setAccuracy(accuracyEmbeddable3);
  71. parameter4.setMeasureScale(measureScaleEmbeddable1);
  72. parameter5.setMeasureScale(measureScaleEmbeddable2);
  73. parameter6.setMeasureScale(measureScaleEmbeddable3);
  74. List parameterList = new ArrayList<>();
  75. parameterList.add(parameter1);
  76. parameterList.add(parameter2);
  77. parameterList.add(parameter3);
  78. parameterList.add(parameter4);
  79. parameterList.add(parameter5);
  80. parameterList.add(parameter6);
  81. parameterRepository.save(parameterList);
  82. logger.debug("6. 初始化附加参量");
  83. AdditionalParameter additionalParameter1 = additionalParameterService.getOneUnsavedObject();
  84. AdditionalParameter additionalParameter2 = additionalParameterService.getOneUnsavedObject();
  85. AdditionalParameter additionalParameter3 = additionalParameterService.getOneUnsavedObject();
  86. additionalParameterService.getOneSavedObject();
  87. additionalParameter1.setMeasureScale(measureScaleEmbeddable1);
  88. additionalParameter2.setMeasureScale(measureScaleEmbeddable2);
  89. additionalParameter3.setMeasureScale(measureScaleEmbeddable3);
  90. List additionalParameterList = new ArrayList<>();
  91. additionalParameterList.add(additionalParameter1);
  92. additionalParameterList.add(additionalParameter2);
  93. additionalParameterList.add(additionalParameter3);
  94. additionalParameterRepository.save(additionalParameterList);
  95. logger.debug("7. 初始化检定能力");
  96. logger.debug("8. 初始化参量检定能力");
  97. logger.debug("9. 初始化附加参量检定能力");
  98. logger.debug("10. 初始化校准能力");
  99. logger.debug("11. 初始化参量校准能力");
  100. logger.debug("12. 初始化附加参量校准能力");
  101. logger.debug("13. 构造新计量单位");
  102. MeasurementUnit newMeasurementUnit = new MeasurementUnit();
  103. String name = CommonService.getRandomStringByLength(10);
  104. newMeasurementUnit.setName(name);
  105. newMeasurementUnit.setSymbol("km");
  106. newMeasurementUnit.setMultiple((oldMeasurementUnit.getMultiple() + 10) * 2);
  107. newMeasurementUnit.setMeasurementUnitCategory(measurementUnitCategoryService.getOneSavedMeasurementUnitCategory());
  108. logger.debug("14. 更新并断言");
  109. Long id = oldMeasurementUnit.getId();
  110. measurementUnitService.update(id, newMeasurementUnit);
  111. MeasurementUnit updatedMeasurementUnit = measurementUnitRepository.findOne(id);
  112. assertThat(updatedMeasurementUnit.getName()).isEqualTo(newMeasurementUnit.getName());
  113. assertThat(updatedMeasurementUnit.getSymbol()).isEqualTo(newMeasurementUnit.getSymbol());
  114. assertThat(updatedMeasurementUnit.getMultiple()).isEqualTo(newMeasurementUnit.getMultiple());
  115. assertThat(updatedMeasurementUnit.getMeasurementUnitCategory()).isEqualTo(newMeasurementUnit.getMeasurementUnitCategory());
  116. logger.debug("15. 断言相关不确定度");
  117. List accuracyUncertainties = accuracyUncertaintyRepository.findAllByMinAccuracyUnit_IdOrMaxAccuracyUnit_Id(id, id);
  118. Assertions.assertThat(accuracyUncertainties.size()).isEqualTo(3);
  119. for (AccuracyUncertainty accuracyUncertainty : accuracyUncertainties) {
  120. if (accuracyUncertainty.getMinAccuracyUnit().getId().equals(id)) {
  121. Assertions.assertThat(accuracyUncertainty.getMinAccuracyAbsoluteValueInTest()).isEqualTo(accuracyUncertainty.getMinAccuracyValue() * newMeasurementUnit.getMultiple());
  122. }
  123. if (accuracyUncertainty.getMaxAccuracyUnit().getId().equals(id)) {
  124. Assertions.assertThat(accuracyUncertainty.getMaxAccuracyAbsoluteValueInTest()).isEqualTo(accuracyUncertainty.getMaxAccuracyValue() * newMeasurementUnit.getMultiple());
  125. }
  126. }
  127. logger.debug("16. 断言相关非强检器具");
  128. List nonMandatoryInstruments = nonMandatoryInstrumentRepository.findAllByMeasureScale_MinMeasureScaleUnit_IdOrMeasureScale_MaxMeasureScaleUnit_Id(id, id);
  129. Assertions.assertThat(nonMandatoryInstruments.size()).isEqualTo(3);
  130. for (NonMandatoryInstrument nonMandatoryInstrument : nonMandatoryInstruments) {
  131. MeasureScaleEmbeddable measureScaleEmbeddable = nonMandatoryInstrument.getMeasureScale();
  132. if (measureScaleEmbeddable.getMinMeasureScaleUnit().getId().equals(id)) {
  133. Assertions.assertThat(measureScaleEmbeddable.getMinMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMinMeasureScaleValue() * newMeasurementUnit.getMultiple());
  134. }
  135. if (measureScaleEmbeddable.getMaxMeasureScaleUnit().getId().equals(id)) {
  136. Assertions.assertThat(measureScaleEmbeddable.getMaxMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMaxMeasureScaleValue() * newMeasurementUnit.getMultiple());
  137. }
  138. }
  139. logger.debug("17. 断言相关参量");
  140. List parameters = parameterRepository.findAllByAccuracy_MinAccuracyUnit_IdOrAccuracy_MaxAccuracyUnit_IdOrMeasureScale_MinMeasureScaleUnit_IdOrMeasureScale_MaxMeasureScaleUnit_Id(id, id, id, id);
  141. Assertions.assertThat(parameters.size()).isEqualTo(6);
  142. for (Parameter parameter : parameters) {
  143. AccuracyEmbeddable accuracyEmbeddable = parameter.getAccuracy();
  144. MeasureScaleEmbeddable measureScaleEmbeddable = parameter.getMeasureScale();
  145. if (accuracyEmbeddable.getMinAccuracyUnit().getId().equals(id)) {
  146. Assertions.assertThat(accuracyEmbeddable.getMinAccuracyAbsoluteValueInTest()).isEqualTo(accuracyEmbeddable.getMinAccuracyValue() * newMeasurementUnit.getMultiple());
  147. }
  148. if (accuracyEmbeddable.getMaxAccuracyUnit().getId().equals(id)) {
  149. Assertions.assertThat(accuracyEmbeddable.getMaxAccuracyAbsoluteValueInTest()).isEqualTo(accuracyEmbeddable.getMaxAccuracyValue() * newMeasurementUnit.getMultiple());
  150. }
  151. if (measureScaleEmbeddable.getMinMeasureScaleUnit().getId().equals(id)) {
  152. Assertions.assertThat(measureScaleEmbeddable.getMinMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMinMeasureScaleValue() * newMeasurementUnit.getMultiple());
  153. }
  154. if (measureScaleEmbeddable.getMaxMeasureScaleUnit().getId().equals(id)) {
  155. Assertions.assertThat(measureScaleEmbeddable.getMaxMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMaxMeasureScaleValue() * newMeasurementUnit.getMultiple());
  156. }
  157. }
  158. logger.debug("18. 断言相关附加参量");
  159. List additionalParameters = additionalParameterRepository.findAllByMeasureScale_MinMeasureScaleUnit_IdOrMeasureScale_MaxMeasureScaleUnit_Id(id, id);
  160. Assertions.assertThat(additionalParameters.size()).isEqualTo(3);
  161. for (AdditionalParameter additionalParameter : additionalParameters) {
  162. MeasureScaleEmbeddable measureScaleEmbeddable = additionalParameter.getMeasureScale();
  163. if (measureScaleEmbeddable.getMinMeasureScaleUnit().getId().equals(id)) {
  164. Assertions.assertThat(measureScaleEmbeddable.getMinMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMinMeasureScaleValue() * newMeasurementUnit.getMultiple());
  165. }
  166. if (measureScaleEmbeddable.getMaxMeasureScaleUnit().getId().equals(id)) {
  167. Assertions.assertThat(measureScaleEmbeddable.getMaxMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMaxMeasureScaleValue() * newMeasurementUnit.getMultiple());
  168. }
  169. }
  170. }
  171. /**
  172. * 获取一个精度内嵌实体
  173. */
  174. private AccuracyEmbeddable getOneAccuracyEmbeddable() {
  175. logger.debug("获取计量单位");
  176. MeasurementUnit otherMeasurementUnit = measurementUnitService.getOneSavedMeasurementUnit();
  177. logger.debug("构造精度内嵌实体");
  178. AccuracyEmbeddable accuracyEmbeddable = new AccuracyEmbeddable();
  179. accuracyEmbeddable.setMinAccuracyValue(1.0f);
  180. accuracyEmbeddable.setMinAccuracyUnit(otherMeasurementUnit);
  181. accuracyEmbeddable.setMaxAccuracyValue(10.0f);
  182. accuracyEmbeddable.setMaxAccuracyUnit(otherMeasurementUnit);
  183. return accuracyEmbeddable;
  184. }
  185. /**
  186. * 获取内嵌测量范围
  187. */
  188. private MeasureScaleEmbeddable getOneMeasureScaleEmbeddable() {
  189. logger.debug("获取计量单位");
  190. MeasurementUnit otherMeasurementUnit = measurementUnitService.getOneSavedMeasurementUnit();
  191. logger.debug("构造测量范围内嵌实体");
  192. MeasureScaleEmbeddable measureScaleEmbeddable = new MeasureScaleEmbeddable();
  193. measureScaleEmbeddable.setMinMeasureScaleValue(1.0f);
  194. measureScaleEmbeddable.setMinMeasureScaleUnit(otherMeasurementUnit);
  195. measureScaleEmbeddable.setMaxMeasureScaleValue(10.0f);
  196. measureScaleEmbeddable.setMaxMeasureScaleUnit(otherMeasurementUnit);
  197. return measureScaleEmbeddable;
  198. }

测试通过了,但是并不是我想要的。

因为我在执行该测试时,还没有写修改附加参量的方法。本测试是期待不通过的。

分析

具体内部怎么运行的不知道,但是应该是前面的引用修改状态,后面的就跟着变了。

之前为了少写几行代码,非强检器具、参量、附加参量这三个用的是同一个测量范围对象,不知道具体的执行过程,但是猜想应该是这东西的问题。

尝试写了一下克隆方法,每次都克隆一个新对象。

网上写的克隆方法,总觉得不好,最后还要强转。

在实体中写的克隆对象的方法,就是new一个然后返回去呗。

</>复制代码

  1. public AccuracyEmbeddable cloneAccuracyEmbeddable() {
  2. AccuracyEmbeddable accuracyEmbeddable = new AccuracyEmbeddable();
  3. accuracyEmbeddable.setMinAccuracyValue(this.minAccuracyValue);
  4. accuracyEmbeddable.setMinAccuracyUnit(this.minAccuracyUnit);
  5. accuracyEmbeddable.setMaxAccuracyValue(this.maxAccuracyValue);
  6. accuracyEmbeddable.setMaxAccuracyUnit(this.maxAccuracyUnit);
  7. accuracyEmbeddable.prePersist();
  8. return accuracyEmbeddable;
  9. }
  10. public MeasureScaleEmbeddable cloneMeasureScaleEmbeddable() {
  11. MeasureScaleEmbeddable measureScaleEmbeddable = new MeasureScaleEmbeddable();
  12. measureScaleEmbeddable.setMinMeasureScaleValue(this.minMeasureScaleValue);
  13. measureScaleEmbeddable.setMinMeasureScaleUnit(this.minMeasureScaleUnit);
  14. measureScaleEmbeddable.setMaxMeasureScaleValue(this.maxMeasureScaleValue);
  15. measureScaleEmbeddable.setMaxMeasureScaleUnit(this.maxMeasureScaleUnit);
  16. measureScaleEmbeddable.prePersist();
  17. return measureScaleEmbeddable;
  18. }

然后就用clone方法获取新对象并设置。

</>复制代码

  1. additionalParameter1.setMeasureScale(measureScaleEmbeddable1.cloneMeasureScaleEmbeddable());
  2. additionalParameter2.setMeasureScale(measureScaleEmbeddable2.cloneMeasureScaleEmbeddable());
  3. additionalParameter3.setMeasureScale(measureScaleEmbeddable3.cloneMeasureScaleEmbeddable());

再次执行测试,出现了预期中的错误,证明就是对象引用的问题。

总结

测试驱动开发,如果我是写完方法再写测试,这应该是一个能通过却有问题的测试,而我却不知道。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/8872.html

相关文章

  • js克隆一个对象,支持循环引用的克隆

    摘要:判断参数是否为待判断的参数克隆一个对象要克隆的目标对象克隆节点,绑定事件的有问题,暂不处理克隆在当前作用域,在全局克隆其它对象,通过识别复制后的对象与原对象是否相同来决定传不传参数,像数组是不能传参数的使用防止对象重写了方法支持节点克隆 (function(){ var toString=Object.prototype.toString,gObj={},cloneHelper=f...

    fai1017 评论0 收藏0
  • JavaScript对象克隆

    摘要:原始类型对象指的是字符串数值布尔值,引用类型对象指的是数组对象函数。既然对象分为这两类,他们的复制克隆也是有差别的。总结根据上面的情况,另外,克隆引用对象必须采用完整克隆深度克隆,包括对象的值也是一个对象也要进行完整克隆深度克隆。 前言 之前有人问我如何克隆一个JS对象,我当时没答上来;过后我查资料弄懂了这个问题,现在整理成文。 正文 JavaScript的一切实例都是对象,但他们也分...

    douzifly 评论0 收藏0
  • 【转】JavaScript 对象的深度克隆

    摘要:在聊以下简称深度克隆之前,我们先来了解一下中对象的组成。克隆或者拷贝分为种浅度克隆深度克隆。浅度克隆基本类型为值传递,对象仍为引用传递。 该文转载自http://www.cnblogs.com/zichi/p/4568150.html,有部分修改。 在聊JavaScript(以下简称js)深度克隆之前,我们先来了解一下js中对象的组成。在 js 中一切实例皆是对象,具体分为 原始类型 ...

    JowayYoung 评论0 收藏0
  • ES6时代,你真的会克隆对象吗(二)

    摘要:多个窗口意味着多个全局环境,不同的全局环境拥有不同的全局对象,从而拥有不同的内置类型构造函数。比如,表达式会返回,因为属性得到的仅仅是构造函数,而且是可以被手动更改的,只是返回的构造函数的名字,它并不返回类名。 原文:ES6时代,你真的会克隆对象吗(二) 上一篇,我们从Symbol和是否可枚举以及属性描述符的角度分析了ES6下怎么浅拷贝一个对象,发表在掘金和segmentfault上(...

    BoYang 评论0 收藏0
  • Java 作者谈克隆方法的实现

    摘要:不合规的代码示例合规解决方案参阅复制构造函数与克隆也可以参阅应该实现克隆覆盖的类应为并调用下面为引文翻译谈设计与作者的对话,作者首次在上发表,年月日复制构造函数与克隆在你的书中,你建议使用复制构造函数而不是实现和编写。 今天在用 sonar 审核代码, 偶然看到下面的提示:showImg(https://segmentfault.com/img/bVbqioZ?w=858&h=116)...

    gaomysion 评论0 收藏0
  • js对象详解(JavaScript对象深度剖析,深度理解js对象)

    摘要:对象详解对象深度剖析,深度理解对象这算是酝酿很久的一篇文章了。用空构造函数设置类名每个对象都共享相同属性每个对象共享一个方法版本,省内存。 js对象详解(JavaScript对象深度剖析,深度理解js对象) 这算是酝酿很久的一篇文章了。 JavaScript作为一个基于对象(没有类的概念)的语言,从入门到精通到放弃一直会被对象这个问题围绕。 平时发的文章基本都是开发中遇到的问题和对...

    CatalpaFlat 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<