在尝试进行 spek 测试(对我们来说是新事物)时,我们在尝试验证模拟上的调用时遇到错误。错误是:
在此上下文中无法访问“m3CustomerService”。java.lang.AssertionError:在此上下文中无法访问“m3CustomerService”。
我一直无法弄清楚为什么这个错误不断发生。有人有什么主意吗。
package com.crv.decustomeradapter.m3
import com.crv.decustomeradapter.m3.client.M3Client
import org.assertj.core.api.Assertions.assertThat
import org.mockito.BDDMockito.then
import org.mockito.Mockito.mock
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
object M3CustomerServiceSpekTest : Spek({
val m3ClientMock by memoized { mock(M3Client::class.java) }
val m3LocationEligibilityCheckServiceMock by memoized { mock(M3LocationEligibilityCheckService::class.java) }
val m3CustomerEligibilityCheckServiceMock by memoized { mock(M3CustomerEligibilityCheckService::class.java) }
describe("Calling the mock") {
it("makes it return empty list") {
assertThat(m3ClientMock.unfilteredCustomersFromM3).isEmpty();
}
}
describe("Initiating the service ") {
val m3CustomerService by memoized {
M3CustomerService(m3CustomerEligibilityCheckServiceMock, m3LocationEligibilityCheckServiceMock,
m3ClientMock)
}
describe("and calling it with no return values for the mocks") {
val m3Customers = m3CustomerService.processM3CustomersFromM3()
it("makes it return an empty list") {
assertThat(m3Customers).isEmpty()
}
it("calls the m3ClientMock") {
//This test gives the error
then(m3ClientMock).should().unfilteredCustomersFromM3
}
}
}
})
更新:当使用 memoized 获取列表时,对 m3CustomerService 的调用从未完成,因此测试失败。