GROX13
GROX13

Reputation: 4765

In Spock unit test mocked method is not being called

I'm trying to implement unit test using Spock framework this is what my test looks like:

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

Here is class in which I'm trying to inject mocked object:

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

While running test instead of calling my method real paymentRepository method is being called. I don't understand what could be the reason. LocalDate and Days are from Joda time and finally this is paymentRepository class:

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

}

Upvotes: 1

Views: 1640

Answers (1)

Dusu
Dusu

Reputation: 66

Please try this:

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

Mocking needs to extend function and Spock won't be able to do so unless function is open in Kotlin.

Upvotes: 5

Related Questions