在Spock单元测试中,模拟方法没有被调用

我试图使用Spock框架来实现单元测试,这是我的测试看起来像:

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方法正在被调用。 我不明白可能是什么原因。 LocalDateDays来自Joda时间 ,最后是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) } 

请试试这个:

 open fun findUnconfirmedTransactions(start: LocalDate, days: Days): ScrollableResults 

嘲笑需要扩展功能,Spock将无法这样做,除非功能在Kotlin中打开。