摘要:斷言不確定度,非強(qiáng)檢器具,參量以及附加參量都修改成功。再次執(zhí)行測(cè)試,出現(xiàn)了預(yù)期中的錯(cuò)誤,證明就是對(duì)象引用的問題。
問題描述
編寫觀察者的單元測(cè)試,執(zhí)行以下測(cè)試通過。斷言不確定度,非強(qiáng)檢器具,參量以及附加參量都修改成功。
@Test public void updateTest() { logger.debug("0. 基礎(chǔ)信息準(zhǔn)備"); logger.debug("構(gòu)造計(jì)量單位"); MeasurementUnit oldMeasurementUnit = measurementUnitService.getOneSavedMeasurementUnit(); logger.debug("1. 初始化不確定度實(shí)體"); logger.debug("獲取基礎(chǔ)的不確定度"); AccuracyUncertainty accuracyUncertainty1 = accuracyUncertaintyService.getOneUnSavedObject(); AccuracyUncertainty accuracyUncertainty2 = accuracyUncertaintyService.getOneUnSavedObject(); AccuracyUncertainty accuracyUncertainty3 = accuracyUncertaintyService.getOneUnSavedObject(); accuracyUncertaintyService.getOneSavedObject(); logger.debug("設(shè)置不確定度1的最小與最大單位"); accuracyUncertainty1.setMinAccuracyUnit(oldMeasurementUnit); accuracyUncertainty1.setMaxAccuracyUnit(oldMeasurementUnit); logger.debug("設(shè)置不確定度2的最小單位"); accuracyUncertainty2.setMinAccuracyUnit(oldMeasurementUnit); logger.debug("設(shè)置不確定度3的最大單位"); accuracyUncertainty3.setMaxAccuracyUnit(oldMeasurementUnit); logger.debug("持久化不確定度列表"); ListaccuracyUncertaintyList = new ArrayList<>(); accuracyUncertaintyList.add(accuracyUncertainty1); accuracyUncertaintyList.add(accuracyUncertainty2); accuracyUncertaintyList.add(accuracyUncertainty3); accuracyUncertaintyRepository.save(accuracyUncertaintyList); logger.debug("2. 初始化內(nèi)嵌不確定度"); AccuracyEmbeddable accuracyEmbeddable1 = this.getOneAccuracyEmbeddable(); AccuracyEmbeddable accuracyEmbeddable2 = this.getOneAccuracyEmbeddable(); AccuracyEmbeddable accuracyEmbeddable3 = this.getOneAccuracyEmbeddable(); logger.debug("設(shè)置內(nèi)嵌不確定度1的最大與最小單位"); accuracyEmbeddable1.setMinAccuracyUnit(oldMeasurementUnit); accuracyEmbeddable1.setMaxAccuracyUnit(oldMeasurementUnit); logger.debug("設(shè)置內(nèi)嵌不確定度2的最小單位"); accuracyEmbeddable2.setMinAccuracyUnit(oldMeasurementUnit); logger.debug("設(shè)置內(nèi)嵌不確定度3的最大單位"); accuracyEmbeddable3.setMaxAccuracyUnit(oldMeasurementUnit); logger.debug("3. 初始化內(nèi)嵌測(cè)量范圍"); MeasureScaleEmbeddable measureScaleEmbeddable1 = this.getOneMeasureScaleEmbeddable(); MeasureScaleEmbeddable measureScaleEmbeddable2 = this.getOneMeasureScaleEmbeddable(); MeasureScaleEmbeddable measureScaleEmbeddable3 = this.getOneMeasureScaleEmbeddable(); logger.debug("設(shè)置內(nèi)嵌測(cè)量范圍1的最大與最小單位"); measureScaleEmbeddable1.setMinMeasureScaleUnit(oldMeasurementUnit); measureScaleEmbeddable1.setMaxMeasureScaleUnit(oldMeasurementUnit); logger.debug("設(shè)置內(nèi)嵌測(cè)量范圍2的最小單位"); measureScaleEmbeddable2.setMinMeasureScaleUnit(oldMeasurementUnit); logger.debug("設(shè)置內(nèi)嵌測(cè)量范圍3的最大單位"); measureScaleEmbeddable3.setMaxMeasureScaleUnit(oldMeasurementUnit); logger.debug("4. 初始化非強(qiáng)檢器具"); NonMandatoryInstrument nonMandatoryInstrument1 = nonMandatoryInstrumentService.getOneUnSavedObject(); NonMandatoryInstrument nonMandatoryInstrument2 = nonMandatoryInstrumentService.getOneUnSavedObject(); NonMandatoryInstrument nonMandatoryInstrument3 = nonMandatoryInstrumentService.getOneUnSavedObject(); nonMandatoryInstrumentService.getOneSavedObject(); nonMandatoryInstrument1.setMeasureScale(measureScaleEmbeddable1); nonMandatoryInstrument2.setMeasureScale(measureScaleEmbeddable2); nonMandatoryInstrument3.setMeasureScale(measureScaleEmbeddable3); List nonMandatoryInstrumentList = new ArrayList<>(); nonMandatoryInstrumentList.add(nonMandatoryInstrument1); nonMandatoryInstrumentList.add(nonMandatoryInstrument2); nonMandatoryInstrumentList.add(nonMandatoryInstrument3); nonMandatoryInstrumentRepository.save(nonMandatoryInstrumentList); logger.debug("5. 初始化參量"); Parameter parameter1 = parameterService.getOneUnsavedObject(); Parameter parameter2 = parameterService.getOneUnsavedObject(); Parameter parameter3 = parameterService.getOneUnsavedObject(); Parameter parameter4 = parameterService.getOneUnsavedObject(); Parameter parameter5 = parameterService.getOneUnsavedObject(); Parameter parameter6 = parameterService.getOneUnsavedObject(); parameterService.getOneSavedObject(); parameter1.setAccuracy(accuracyEmbeddable1); parameter2.setAccuracy(accuracyEmbeddable2); parameter3.setAccuracy(accuracyEmbeddable3); parameter4.setMeasureScale(measureScaleEmbeddable1); parameter5.setMeasureScale(measureScaleEmbeddable2); parameter6.setMeasureScale(measureScaleEmbeddable3); List parameterList = new ArrayList<>(); parameterList.add(parameter1); parameterList.add(parameter2); parameterList.add(parameter3); parameterList.add(parameter4); parameterList.add(parameter5); parameterList.add(parameter6); parameterRepository.save(parameterList); logger.debug("6. 初始化附加參量"); AdditionalParameter additionalParameter1 = additionalParameterService.getOneUnsavedObject(); AdditionalParameter additionalParameter2 = additionalParameterService.getOneUnsavedObject(); AdditionalParameter additionalParameter3 = additionalParameterService.getOneUnsavedObject(); additionalParameterService.getOneSavedObject(); additionalParameter1.setMeasureScale(measureScaleEmbeddable1); additionalParameter2.setMeasureScale(measureScaleEmbeddable2); additionalParameter3.setMeasureScale(measureScaleEmbeddable3); List additionalParameterList = new ArrayList<>(); additionalParameterList.add(additionalParameter1); additionalParameterList.add(additionalParameter2); additionalParameterList.add(additionalParameter3); additionalParameterRepository.save(additionalParameterList); logger.debug("7. 初始化檢定能力"); logger.debug("8. 初始化參量檢定能力"); logger.debug("9. 初始化附加參量檢定能力"); logger.debug("10. 初始化校準(zhǔn)能力"); logger.debug("11. 初始化參量校準(zhǔn)能力"); logger.debug("12. 初始化附加參量校準(zhǔn)能力"); logger.debug("13. 構(gòu)造新計(jì)量單位"); MeasurementUnit newMeasurementUnit = new MeasurementUnit(); String name = CommonService.getRandomStringByLength(10); newMeasurementUnit.setName(name); newMeasurementUnit.setSymbol("km"); newMeasurementUnit.setMultiple((oldMeasurementUnit.getMultiple() + 10) * 2); newMeasurementUnit.setMeasurementUnitCategory(measurementUnitCategoryService.getOneSavedMeasurementUnitCategory()); logger.debug("14. 更新并斷言"); Long id = oldMeasurementUnit.getId(); measurementUnitService.update(id, newMeasurementUnit); MeasurementUnit updatedMeasurementUnit = measurementUnitRepository.findOne(id); assertThat(updatedMeasurementUnit.getName()).isEqualTo(newMeasurementUnit.getName()); assertThat(updatedMeasurementUnit.getSymbol()).isEqualTo(newMeasurementUnit.getSymbol()); assertThat(updatedMeasurementUnit.getMultiple()).isEqualTo(newMeasurementUnit.getMultiple()); assertThat(updatedMeasurementUnit.getMeasurementUnitCategory()).isEqualTo(newMeasurementUnit.getMeasurementUnitCategory()); logger.debug("15. 斷言相關(guān)不確定度"); List accuracyUncertainties = accuracyUncertaintyRepository.findAllByMinAccuracyUnit_IdOrMaxAccuracyUnit_Id(id, id); Assertions.assertThat(accuracyUncertainties.size()).isEqualTo(3); for (AccuracyUncertainty accuracyUncertainty : accuracyUncertainties) { if (accuracyUncertainty.getMinAccuracyUnit().getId().equals(id)) { Assertions.assertThat(accuracyUncertainty.getMinAccuracyAbsoluteValueInTest()).isEqualTo(accuracyUncertainty.getMinAccuracyValue() * newMeasurementUnit.getMultiple()); } if (accuracyUncertainty.getMaxAccuracyUnit().getId().equals(id)) { Assertions.assertThat(accuracyUncertainty.getMaxAccuracyAbsoluteValueInTest()).isEqualTo(accuracyUncertainty.getMaxAccuracyValue() * newMeasurementUnit.getMultiple()); } } logger.debug("16. 斷言相關(guān)非強(qiáng)檢器具"); List nonMandatoryInstruments = nonMandatoryInstrumentRepository.findAllByMeasureScale_MinMeasureScaleUnit_IdOrMeasureScale_MaxMeasureScaleUnit_Id(id, id); Assertions.assertThat(nonMandatoryInstruments.size()).isEqualTo(3); for (NonMandatoryInstrument nonMandatoryInstrument : nonMandatoryInstruments) { MeasureScaleEmbeddable measureScaleEmbeddable = nonMandatoryInstrument.getMeasureScale(); if (measureScaleEmbeddable.getMinMeasureScaleUnit().getId().equals(id)) { Assertions.assertThat(measureScaleEmbeddable.getMinMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMinMeasureScaleValue() * newMeasurementUnit.getMultiple()); } if (measureScaleEmbeddable.getMaxMeasureScaleUnit().getId().equals(id)) { Assertions.assertThat(measureScaleEmbeddable.getMaxMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMaxMeasureScaleValue() * newMeasurementUnit.getMultiple()); } } logger.debug("17. 斷言相關(guān)參量"); List parameters = parameterRepository.findAllByAccuracy_MinAccuracyUnit_IdOrAccuracy_MaxAccuracyUnit_IdOrMeasureScale_MinMeasureScaleUnit_IdOrMeasureScale_MaxMeasureScaleUnit_Id(id, id, id, id); Assertions.assertThat(parameters.size()).isEqualTo(6); for (Parameter parameter : parameters) { AccuracyEmbeddable accuracyEmbeddable = parameter.getAccuracy(); MeasureScaleEmbeddable measureScaleEmbeddable = parameter.getMeasureScale(); if (accuracyEmbeddable.getMinAccuracyUnit().getId().equals(id)) { Assertions.assertThat(accuracyEmbeddable.getMinAccuracyAbsoluteValueInTest()).isEqualTo(accuracyEmbeddable.getMinAccuracyValue() * newMeasurementUnit.getMultiple()); } if (accuracyEmbeddable.getMaxAccuracyUnit().getId().equals(id)) { Assertions.assertThat(accuracyEmbeddable.getMaxAccuracyAbsoluteValueInTest()).isEqualTo(accuracyEmbeddable.getMaxAccuracyValue() * newMeasurementUnit.getMultiple()); } if (measureScaleEmbeddable.getMinMeasureScaleUnit().getId().equals(id)) { Assertions.assertThat(measureScaleEmbeddable.getMinMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMinMeasureScaleValue() * newMeasurementUnit.getMultiple()); } if (measureScaleEmbeddable.getMaxMeasureScaleUnit().getId().equals(id)) { Assertions.assertThat(measureScaleEmbeddable.getMaxMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMaxMeasureScaleValue() * newMeasurementUnit.getMultiple()); } } logger.debug("18. 斷言相關(guān)附加參量"); List additionalParameters = additionalParameterRepository.findAllByMeasureScale_MinMeasureScaleUnit_IdOrMeasureScale_MaxMeasureScaleUnit_Id(id, id); Assertions.assertThat(additionalParameters.size()).isEqualTo(3); for (AdditionalParameter additionalParameter : additionalParameters) { MeasureScaleEmbeddable measureScaleEmbeddable = additionalParameter.getMeasureScale(); if (measureScaleEmbeddable.getMinMeasureScaleUnit().getId().equals(id)) { Assertions.assertThat(measureScaleEmbeddable.getMinMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMinMeasureScaleValue() * newMeasurementUnit.getMultiple()); } if (measureScaleEmbeddable.getMaxMeasureScaleUnit().getId().equals(id)) { Assertions.assertThat(measureScaleEmbeddable.getMaxMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMaxMeasureScaleValue() * newMeasurementUnit.getMultiple()); } } } /** * 獲取一個(gè)精度內(nèi)嵌實(shí)體 */ private AccuracyEmbeddable getOneAccuracyEmbeddable() { logger.debug("獲取計(jì)量單位"); MeasurementUnit otherMeasurementUnit = measurementUnitService.getOneSavedMeasurementUnit(); logger.debug("構(gòu)造精度內(nèi)嵌實(shí)體"); AccuracyEmbeddable accuracyEmbeddable = new AccuracyEmbeddable(); accuracyEmbeddable.setMinAccuracyValue(1.0f); accuracyEmbeddable.setMinAccuracyUnit(otherMeasurementUnit); accuracyEmbeddable.setMaxAccuracyValue(10.0f); accuracyEmbeddable.setMaxAccuracyUnit(otherMeasurementUnit); return accuracyEmbeddable; } /** * 獲取內(nèi)嵌測(cè)量范圍 */ private MeasureScaleEmbeddable getOneMeasureScaleEmbeddable() { logger.debug("獲取計(jì)量單位"); MeasurementUnit otherMeasurementUnit = measurementUnitService.getOneSavedMeasurementUnit(); logger.debug("構(gòu)造測(cè)量范圍內(nèi)嵌實(shí)體"); MeasureScaleEmbeddable measureScaleEmbeddable = new MeasureScaleEmbeddable(); measureScaleEmbeddable.setMinMeasureScaleValue(1.0f); measureScaleEmbeddable.setMinMeasureScaleUnit(otherMeasurementUnit); measureScaleEmbeddable.setMaxMeasureScaleValue(10.0f); measureScaleEmbeddable.setMaxMeasureScaleUnit(otherMeasurementUnit); return measureScaleEmbeddable; }
測(cè)試通過了,但是并不是我想要的。
因?yàn)槲以趫?zhí)行該測(cè)試時(shí),還沒有寫修改附加參量的方法。本測(cè)試是期待不通過的。
分析具體內(nèi)部怎么運(yùn)行的不知道,但是應(yīng)該是前面的引用修改狀態(tài),后面的就跟著變了。
之前為了少寫幾行代碼,非強(qiáng)檢器具、參量、附加參量這三個(gè)用的是同一個(gè)測(cè)量范圍對(duì)象,不知道具體的執(zhí)行過程,但是猜想應(yīng)該是這東西的問題。
嘗試寫了一下克隆方法,每次都克隆一個(gè)新對(duì)象。
網(wǎng)上寫的克隆方法,總覺得不好,最后還要強(qiáng)轉(zhuǎn)。
在實(shí)體中寫的克隆對(duì)象的方法,就是new一個(gè)然后返回去唄。
public AccuracyEmbeddable cloneAccuracyEmbeddable() { AccuracyEmbeddable accuracyEmbeddable = new AccuracyEmbeddable(); accuracyEmbeddable.setMinAccuracyValue(this.minAccuracyValue); accuracyEmbeddable.setMinAccuracyUnit(this.minAccuracyUnit); accuracyEmbeddable.setMaxAccuracyValue(this.maxAccuracyValue); accuracyEmbeddable.setMaxAccuracyUnit(this.maxAccuracyUnit); accuracyEmbeddable.prePersist(); return accuracyEmbeddable; } public MeasureScaleEmbeddable cloneMeasureScaleEmbeddable() { MeasureScaleEmbeddable measureScaleEmbeddable = new MeasureScaleEmbeddable(); measureScaleEmbeddable.setMinMeasureScaleValue(this.minMeasureScaleValue); measureScaleEmbeddable.setMinMeasureScaleUnit(this.minMeasureScaleUnit); measureScaleEmbeddable.setMaxMeasureScaleValue(this.maxMeasureScaleValue); measureScaleEmbeddable.setMaxMeasureScaleUnit(this.maxMeasureScaleUnit); measureScaleEmbeddable.prePersist(); return measureScaleEmbeddable; }
然后就用clone方法獲取新對(duì)象并設(shè)置。
additionalParameter1.setMeasureScale(measureScaleEmbeddable1.cloneMeasureScaleEmbeddable()); additionalParameter2.setMeasureScale(measureScaleEmbeddable2.cloneMeasureScaleEmbeddable()); additionalParameter3.setMeasureScale(measureScaleEmbeddable3.cloneMeasureScaleEmbeddable());
再次執(zhí)行測(cè)試,出現(xiàn)了預(yù)期中的錯(cuò)誤,證明就是對(duì)象引用的問題。
總結(jié)測(cè)試驅(qū)動(dòng)開發(fā),如果我是寫完方法再寫測(cè)試,這應(yīng)該是一個(gè)能通過卻有問題的測(cè)試,而我卻不知道。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/72249.html
摘要:判斷參數(shù)是否為待判斷的參數(shù)克隆一個(gè)對(duì)象要克隆的目標(biāo)對(duì)象克隆節(jié)點(diǎn),綁定事件的有問題,暫不處理克隆在當(dāng)前作用域,在全局克隆其它對(duì)象,通過識(shí)別復(fù)制后的對(duì)象與原對(duì)象是否相同來決定傳不傳參數(shù),像數(shù)組是不能傳參數(shù)的使用防止對(duì)象重寫了方法支持節(jié)點(diǎn)克隆 (function(){ var toString=Object.prototype.toString,gObj={},cloneHelper=f...
摘要:原始類型對(duì)象指的是字符串?dāng)?shù)值布爾值,引用類型對(duì)象指的是數(shù)組對(duì)象函數(shù)。既然對(duì)象分為這兩類,他們的復(fù)制克隆也是有差別的。總結(jié)根據(jù)上面的情況,另外,克隆引用對(duì)象必須采用完整克隆深度克隆,包括對(duì)象的值也是一個(gè)對(duì)象也要進(jìn)行完整克隆深度克隆。 前言 之前有人問我如何克隆一個(gè)JS對(duì)象,我當(dāng)時(shí)沒答上來;過后我查資料弄懂了這個(gè)問題,現(xiàn)在整理成文。 正文 JavaScript的一切實(shí)例都是對(duì)象,但他們也分...
摘要:在聊以下簡(jiǎn)稱深度克隆之前,我們先來了解一下中對(duì)象的組成。克隆或者拷貝分為種淺度克隆深度克隆。淺度克隆基本類型為值傳遞,對(duì)象仍為引用傳遞。 該文轉(zhuǎn)載自http://www.cnblogs.com/zichi/p/4568150.html,有部分修改。 在聊JavaScript(以下簡(jiǎn)稱js)深度克隆之前,我們先來了解一下js中對(duì)象的組成。在 js 中一切實(shí)例皆是對(duì)象,具體分為 原始類型 ...
摘要:多個(gè)窗口意味著多個(gè)全局環(huán)境,不同的全局環(huán)境擁有不同的全局對(duì)象,從而擁有不同的內(nèi)置類型構(gòu)造函數(shù)。比如,表達(dá)式會(huì)返回,因?yàn)閷傩缘玫降膬H僅是構(gòu)造函數(shù),而且是可以被手動(dòng)更改的,只是返回的構(gòu)造函數(shù)的名字,它并不返回類名。 原文:ES6時(shí)代,你真的會(huì)克隆對(duì)象嗎(二) 上一篇,我們從Symbol和是否可枚舉以及屬性描述符的角度分析了ES6下怎么淺拷貝一個(gè)對(duì)象,發(fā)表在掘金和segmentfault上(...
摘要:不合規(guī)的代碼示例合規(guī)解決方案參閱復(fù)制構(gòu)造函數(shù)與克隆也可以參閱應(yīng)該實(shí)現(xiàn)克隆覆蓋的類應(yīng)為并調(diào)用下面為引文翻譯談設(shè)計(jì)與作者的對(duì)話,作者首次在上發(fā)表,年月日復(fù)制構(gòu)造函數(shù)與克隆在你的書中,你建議使用復(fù)制構(gòu)造函數(shù)而不是實(shí)現(xiàn)和編寫。 今天在用 sonar 審核代碼, 偶然看到下面的提示:showImg(https://segmentfault.com/img/bVbqioZ?w=858&h=116)...
摘要:對(duì)象詳解對(duì)象深度剖析,深度理解對(duì)象這算是醞釀很久的一篇文章了。用空構(gòu)造函數(shù)設(shè)置類名每個(gè)對(duì)象都共享相同屬性每個(gè)對(duì)象共享一個(gè)方法版本,省內(nèi)存。 js對(duì)象詳解(JavaScript對(duì)象深度剖析,深度理解js對(duì)象) 這算是醞釀很久的一篇文章了。 JavaScript作為一個(gè)基于對(duì)象(沒有類的概念)的語言,從入門到精通到放棄一直會(huì)被對(duì)象這個(gè)問題圍繞。 平時(shí)發(fā)的文章基本都是開發(fā)中遇到的問題和對(duì)...
閱讀 2022·2023-04-25 23:30
閱讀 1452·2021-11-24 10:18
閱讀 3069·2021-10-09 09:54
閱讀 2017·2021-10-08 10:05
閱讀 3431·2021-09-23 11:21
閱讀 3161·2019-08-30 15:52
閱讀 1560·2019-08-30 13:05
閱讀 1056·2019-08-30 13:02