简体   繁体   English

kotlin 中的单元测试与规范

[英]Unit tests in kotlin with specifications

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.为此,我使用了 JpaSpecificationExecutor 的List<T> findAll(@Nullable Specification<T> spec)方法。 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:为了让 spring 理解,我应该在 findAll() 参数中写什么:

faqRepository.findAll(any())

Solved the problem with解决了这个问题

every { faqRepository.findAll(any<Specification<FaqEntity>>()) } returns faqList

The problem is that the compiler can't decide what type any() is supposed to be and therefore which method to pick.问题是编译器无法决定any()应该是什么类型,因此无法决定选择哪种方法。

I'm not sure where any() is coming from, but if it is from some mocking library you can probably use any(Specification) .我不确定any()来自哪里,但如果它来自某些 mocking 库,您可能可以使用any(Specification) Otherwise you might be able to change the signature of any() to return a Specification or cast it to Specification .否则,您可能能够更改any()的签名以返回Specification或将其强制转换为Specification

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM