p3q*_*uod 2 kotlin junit5 mockk
在某些情况下,需要清理或重置测试用例之间的模拟。
将 Kotling 与 JUnit5 和 Mockk 一起使用,第一种方法应该是这样的:
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>())
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是不是在每个测试用例上声明模拟,并初始化 SUT,也许使用更清晰(也许不是更快)@BeforeEach,像这样:
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)
}
...
Run Code Online (Sandbox Code Playgroud)
有没有更好(更快)的方法来声明模拟并 sut 一次并在每次测试中重置或清除它们?
小智 9
您可以初始化模拟并 sut 一次,并在每次测试之前重置模拟,但您只需要生成测试类的一次实例。看起来是这样的:
@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)
}
...
Run Code Online (Sandbox Code Playgroud)
小智 5
你应该试试:
@AfterEach
internal fun tearDown() {
clearAllMocks()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3907 次 |
| 最近记录: |