我尝试使用Spock framework实施单元测试,这就是我的测试结果:
def setup() {
scrollableResultsMock = Mock(ScrollableResults)
paymentRepositoryMock = Mock(PaymentRepository)
paymentRegistryService = new PaymentRegistryService(paymentRepositoryMock)
}
@Unroll
def 'should correctly process'() {
given:
paymentRepositoryMock.findUnconfirmedTransactions(_ as LocalDate, _ as Days) >> scrollableResultsMock
...
}
这是我尝试注入模拟对象的类:
@Service
open class PaymentRegistryService
@Autowired
constructor(
val paymentRepository: PaymentRepository
) {
@Transactional
open fun parseRegistryFileStream(input: InputStream): LinkedList<Pair<Long, String>> {
...
val registry = paymentRepository.findUnconfirmedTransactions(start, PERIOD)
...
}
}
在运行测试而不是调用我的方法时,正在调用真正的paymentRepository
方法。我不明白可能是什么原因。 LocalDate
和Days
来自Joda time,最后是paymentRepository
类:
@Repository
@Transactional
open class PaymentRepository : AbstractRepository<Payment, Long>(Payment::class.java) {
fun findUnconfirmedTransactions(start: LocalDate, days: Days): ScrollableResults = criteria().add(
and(
eq("isConfirmed", false),
ge("transactionDateTime", start),
lt("transactionDateTime", start.plus(days))
)).setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY)
}
答案 0 :(得分:2)
请试试这个:
open fun findUnconfirmedTransactions(start: LocalDate, days: Days): ScrollableResults
模拟需要扩展功能,除非在Kotlin中打开函数,否则Spock不会这样做。