无法在测试中保存实体

时间:2019-10-17 19:32:16

标签: spring hibernate unit-testing jpa

我想从JobService中测试create()方法。当我启动应用程序并由邮递员提出要求时,它可以工作,但是当我启动测试方法时,它不起作用。为什么?

@RunWith(SpringRunner::class)
internal class JobServiceTest {
    @Mock
    lateinit var jobRepository: JobRepository
    @Mock
    lateinit var jobStateMachineFactory: StateMachineFactory<JobState, JobEvent>
    @InjectMocks
    lateinit var jobService: JobService

    @Test
    fun create() {
        val jobDescription = "Job description"
        val jobPoint = Point(1.0, 1.0)
        val createdJob = jobService.create(jobDescription, jobPoint)
        assertThat(jobRepository.count()).isEqualTo(1)
        assertThat(createdJob).isNotNull
        assertThat(createdJob!!.currentState).isEqualTo(JobState.DRAFT)
        assertThat(createdJob.description).isEqualTo(jobDescription)
        assertThat(createdJob.point).isEqualTo(jobPoint)
        assertThat(createdJob.serviceTime).isNull()
        assertThat(createdJob.callOutCharge).isEqualTo(0.0F)
    }
}

服务功能:

fun create(description: String, point: Point): Job? {
    return jobRepository.save(Job(description, point))
}

0 个答案:

没有答案