Kotlin:如何使用JUnit5和Mockk清理或重置Mock
在某些情况下,需要在测试用例之间清理或重置模拟 将Koling与JUnit5和Mockk一起使用,第一种方法应该如下所示:Kotlin:如何使用JUnit5和Mockk清理或重置Mock,kotlin,junit5,mockk,Kotlin,Junit5,Mockk,在某些情况下,需要在测试用例之间清理或重置模拟 将Koling与JUnit5和Mockk一起使用,第一种方法应该如下所示: class CreateProductsTests { @Test fun `run() with an existing product should throw a CrudException`() { val productRepository = mockk<ProductRepository>() v
class CreateProductsTests {
@Test
fun `run() with an existing product should throw a CrudException`() {
val productRepository = mockk<ProductRepository>()
val editorService = mockk<EditorService>()
val sut = CreateProductServiceImpl(productRepository, editorService)
// Given an editor that return a JSON
val product = completeTestProduct()
every { editorService.edit("Create new Product", product) } returns product
// And the product does exist in the database
every { productRepository.findById(product.id) } returns Optional.of(product)
// When we call createProduct()"
// Then should fail
val exception = assertFailsWith<CrudException> { sut.createProduct() }
exception.message shouldBe "The product 'TST' already exists in database"
}
@Test
fun `createProduct() with an invalid product should fail`() {
val productRepository = mockk<ProductRepository>()
val editorService = mockk<EditorService>()
val sut = CreateProductServiceImpl(productRepository, editorService)
// Given an editor that return a JSON
val product = completeTestProduct()
every { editorService.edit("Create new Product", product) } returns product
// And the product does exist in the database
every { productRepository.findById(product.id) } returns Optional.of(product)
// And a repository saves the product
every { productRepository.save(product) } returns product
// When we call createProduct()"
val actual = sut.createProduct()
// Then it should return the product
actual shouldBe product
// And should call once these dependencies
verify(exactly = 1) {
editorService.edit(any<String>(), any<Product>())
productRepository.findById(any<String>())
productRepository.save(any<Product>())
}
}
}
class CreateProductsTests {
var productRepository = mockk<ProductRepository>()
var editorService = mockk<EditorService>()
var sut = CreateProductServiceImpl(productRepository, editorService)
@BeforeEach
fun clear() {
productRepository = mockk<ProductRepository>()
editorService = mockk<EditorService>()
sut = CreateProductServiceImpl(productRepository, editorService)
}
...
是否有更好(更快)的方法一次性声明模拟和sut,并在每次测试中重置或清除所有模拟和sut?您应该尝试:
@AfterEach
internal fun tearDown() {
clearAllMocks()
}
您可以在每次测试之前初始化mock和sut一次并重置mock,但是您只需要生成测试类的一个实例。这就是它的样子:
@TestInstance(Lifecycle.PER_CLASS)
class CreateProductsTests {
var productRepository = mockk<ProductRepository>()
var editorService = mockk<EditorService>()
var sut = CreateProductServiceImpl(productRepository, editorService)
@BeforeAll
fun setup() {
MockKAnnotations.init(this)
sut = CreateProductServiceImpl(productRepository, editorService)
}
@BeforeEach
fun clear() {
clearMocks(productRepository, editorService)
}
...
@TestInstance(生命周期每类)
类CreateProductsTests{
var productRepository=mockk()
var editorService=mockk()
var sut=CreateProductServiceImpl(productRepository,editorService)
@以前
趣味设置(){
MockKAnnotations.init(this)
sut=CreateProductServiceImpl(productRepository,editorService)
}
@之前
趣味十足{
clearMocks(产品存储库、编辑器服务)
}
...