我有一个测试功能,该功能应执行以下任务
- 将数据插入DB
- 查询数据库和验证数据是预期的
问题在于,在我的测试中,数据尚未投入到数据库中,就像它陷入了某些transactional
步骤一样,我一定能在执行第二个查询之前提交数据。
这是我的测试功能的一部分,@Rollback(false)
仅用于开发阶段。
@Test
@Rollback(false)
....
reportJobManager.saveOutput(savedDef, pipeline, results, null)
reportJobManager.retryRetention(savedDef, listOf(csvDeliverbale))
保存输出函数。示例代码
@Transactional
fun saveOutput() {
if (deliverable.type.name == "DATA_RETENTION_RESULT") {
finishedPipeline.postProcessors.forEach {
//it(definition, dbDeliverable)
val dbRetention = ReportRetention(
deliverable = dbDeliverable,
definition = definition,
retryCount = 1L
)
val retentionUploadSaved = retentionRepository.save(dbRetention)
if (retentionUploadSaved.id == null) {
throw IllegalStateException("Retention upload was not saved!")
}
}
}
}
retryRetention
func代码
fun retryRetention(definition: ReportDefinition, listOfDeliverables: List<Deliverable>) {
retentionRepository.findAll().forEach {
if (it.state.name == "NOT_UPLOADED" && it.retryCount!!.toInt() < 5) {
if (it.deliverable?.success == true) {
it.state = RetentionUploadStatus.UPLOADED
println("RetentionUploadStatus->UPLOADED")
} else {
val schemaService = SchemaServiceImpl()
val schemas = schemaService.initializeSchemas(definition, emptyMap())
val parameters = definition.parameterPolicy.policy(schemas.parametersSchema)
val delivery = deliveryPolicyService.policy<Deliverable>(ValidDeliveryPolicy.RETENTION_ONLY, schemas.deliverySchema)
val deliveryFunction = delivery.createDeliveryStep()
deliveryFunction(parameters, listOfDeliverables)
it.retryCount = it.retryCount!!.plus(1L)
}
retentionRepository.save(it)
}
}
}
如果您具有带有@Transactional
注释的方法saveOutput()
,则需要将@Transactional
添加到其他正在调用saveOutput()
以进行交易的其他方法上以实际提交。