I need to get the results of the query with specifications. In order to do that I'm using JpaSpecificationExecutor's List<T> findAll(@Nullable Specification<T> spec)
method. The problem is that I can't do the same in tests, cause it has several methods with the same parameters.
Here is my method:
fun getFaqList(category: FaqCategory?, subcategory: FaqCategory?, searchText: String?): List<FaqEntity> {
val spec = Specification.where(FaqSpecification.categoryEquals(category))
?.and(FaqSpecification.subcategoryEquals(subcategory))
?.and(
stringFieldContains("title", searchText)
?.or(stringFieldContains("description", searchText))
)
return faqRepository.findAll(spec)
}
And the test that I'm trying to run:
@MockK
private lateinit var faqRepository: FaqRepository
@InjectMockKs
private lateinit var faqService: FaqService
companion object {
val FAQ_CATEGORY_ENTITY = FaqCategoryEntity(
id = AGRICULTURE
)
val FAQ_SUBCATEGORY_ENTITY = FaqCategoryEntity(
id = AGRICULTURE_GENERAL
)
val FAQ_ENTITY = FaqEntity(
id = FAQ_ID,
title = "title",
description = "description",
category = FAQ_CATEGORY_ENTITY,
subcategory = FAQ_SUBCATEGORY_ENTITY
)
}
@Test
fun `getFaqList - should return faq list`() {
val faqList = listOf(FAQ_ENTITY)
every { faqRepository.findAll(any()) } returns faqList
val response = faqService.getFaqList(AGRICULTURE, AGRICULTURE_GENERAL, FAQ_SEARCH_TEXT)
assertThat(response).isEqualTo(faqList)
}
I'm getting error:
Overload resolution ambiguity. All these functions match.
public abstract fun <S : FaqEntity!> findAll(example: Example<TypeVariable(S)!>):
(Mutable)List<TypeVariable(S)!> defined in kz.btsd.backkotlin.faq.FaqRepository
public abstract fun findAll(pageable: Pageable): Page<FaqEntity!> defined in
kz.btsd.backkotlin.faq.FaqRepository
public abstract fun findAll(sort: Sort): (Mutable)List<FaqEntity!> defined in
kz.btsd.backkotlin.faq.FaqRepository
public abstract fun findAll(spec: Specification<FaqEntity!>?): (Mutable)List<FaqEntity!>
defined in kz.btsd.backkotlin.faq.FaqRepository
What should I write in findAll() params in order for spring to understand :
faqRepository.findAll(any())
Solved the problem with
every { faqRepository.findAll(any<Specification<FaqEntity>>()) } returns faqList