kotlin 中的单元测试规范
Unit tests in kotlin with specifications
我需要获得符合规范的查询结果。为此,我使用了 JpaSpecificationExecutor 的 List<T> findAll(@Nullable Specification<T> spec)
方法。问题是我不能在测试中做同样的事情,因为它有几个参数相同的方法。
这是我的方法:
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)
}
我正在尝试的测试 运行:
@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)
}
我遇到错误:
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
为了 spring 理解,我应该在 findAll() 参数中写什么:
faqRepository.findAll(any())
问题是编译器无法决定 any()
应该是什么类型,因此无法决定选择哪种方法。
我不确定 any()
的来源,但如果它来自某个模拟库,您可能可以使用 any(Specification)
。否则,您可以将 any()
的签名更改为 return a Specification
或将其转换为 Specification
.
解决了
的问题
every { faqRepository.findAll(any<Specification<FaqEntity>>()) } returns faqList
我需要获得符合规范的查询结果。为此,我使用了 JpaSpecificationExecutor 的 List<T> findAll(@Nullable Specification<T> spec)
方法。问题是我不能在测试中做同样的事情,因为它有几个参数相同的方法。
这是我的方法:
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)
}
我正在尝试的测试 运行:
@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)
}
我遇到错误:
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
为了 spring 理解,我应该在 findAll() 参数中写什么:
faqRepository.findAll(any())
问题是编译器无法决定 any()
应该是什么类型,因此无法决定选择哪种方法。
我不确定 any()
的来源,但如果它来自某个模拟库,您可能可以使用 any(Specification)
。否则,您可以将 any()
的签名更改为 return a Specification
或将其转换为 Specification
.
解决了
的问题every { faqRepository.findAll(any<Specification<FaqEntity>>()) } returns faqList