For testing purposes and building history I need to mock the date
What is the best approach?
is unit testing - How to mock new Date() in java using Mockito - Stack Overflow
a feasible approach?
look forward to your suggestions,
Regards,
Hans
Most of the OOTB code uses ec.user.nowTimestamp() for the current date/time, and this is partly to make it easier to test with different dates/times. This approach is used in the OOTB service level tests in mantle-usl, such as this call to ec.user.setEffectiveTime():
@Shared Map setInfoOut, shipResult
@Shared String b2bPaymentId, b2bShipmentId, b2bCredMemoId
@Shared long effectiveTime = System.currentTimeMillis()
// no longer needed: @Shared boolean kieEnabled = false
@Shared long totalFieldsChecked = 0
def setupSpec() {
// init the framework, get the ec
ec = Moqui.getExecutionContext()
// set an effective date so data check works, etc
ec.user.setEffectiveTime(new Timestamp(effectiveTime))
// no longer needed: kieEnabled = ec.factory.getToolFactory("KIE") != null
ec.entity.tempSetSequencedIdPrimary("mantle.account.method.PaymentGatewayResponse", 55500, 10)
ec.entity.tempSetSequencedIdPrimary("mantle.ledger.transaction.AcctgTrans", 55500, 50)
ec.entity.tempSetSequencedIdPrimary("mantle.shipment.Shipment", 55500, 10)
ec.entity.tempSetSequencedIdPrimary("mantle.shipment.ShipmentItemSource", 55500, 10)
ec.entity.tempSetSequencedIdPrimary("mantle.product.asset.Asset", 55500, 10)
ec.entity.tempSetSequencedIdPrimary("mantle.product.asset.AssetDetail", 55500, 50)
ec.entity.tempSetSequencedIdPrimary("mantle.product.asset.PhysicalInventory", 55500, 10)
ec.entity.tempSetSequencedIdPrimary("mantle.product.issuance.AssetReservation", 55500, 10)
1 Like
excellent, works very well, thanks David!
1 Like