在spring-data

时间:2019-05-13 10:10:08

标签: jpa kotlin spring-data-jpa mockito

我有一个测试功能,应该执行以下任务

  1. 将数据插入数据库
  2. 查询数据库并验证数据是否符合预期

问题在于,在我的测试中,数据尚未提交到db,就像卡在某个transactional步骤中一样,在执行第二个查询之前,如何确定提交数据。

这是我测试功能的一部分,@Rollback(false)仅用于开发阶段。

@Test
@Rollback(false)
....
reportJobManager.saveOutput(savedDef, pipeline, results, null)
reportJobManager.retryRetention(savedDef, listOf(csvDeliverbale))

saveOutput函数。示例代码

@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功能代码

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)
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

如果您有带有saveOutput()批注的方法@Transactional,则需要在调用@Transactional的所有其他方法之上添加saveOutput(),以使事务实际提交。 / p>