Fake code
Fake code
Generic code
Assert 2
Assert 1
Assert 2
Assert 1
Assert 1
Exemple de test simple et succinct :
@Test
fun `given products, when get for a category, then obtain the products of this category`() {
givenProducts(
ProductEntity(id = 1, name = "Envelope", category = "Office", description = "An Envelope", stockAmount = 1),
ProductEntity(id = 2, name = "Pen", category = "Office", description = "An Pen", stockAmount = 1),
ProductEntity(id = 3, name = "Notebook", category = "Hardware", description = "A Notebook", stockAmount = 2)
)
val actualProducts = whenGetProductsForCategory("Office")
assertProductIdsAre(listOf(1, 2), actualProducts)
}
Après extraction de méthode pour la création des products :
@Test
fun `given products, when get for a category, then obtain the products of this category`() {
givenProducts(
createProductWithCategory(1, "Office"),
createProductWithCategory(2, "Office"),
createProductWithCategory(3, "Hardware")
)
val actualProducts = whenGetProductsForCategory("Office")
assertProductIdsAre(listOf(1, 2), actualProducts)
}
fun createProductWithCategory(id: Int, category: String) =
ProductEntity(id,
"Something",
category,
"This is something",
5)
fun createProductWithCategory(id: Int, category: String) =
ProductEntity(id,
aName,
category,
aDescription,
anAmount)
val aName = "a name"
val aDescription = "a description"
val anAmount = 5
fun createProductWithCategory(id: Int, category: String) =
ProductEntity(id,
aName(),
category,
aDescription(),
anAmount())
fun aName() = aString(maxSize = 10)
fun aDescription() = aString(maxSize = 32)
fun anAmount() = anInt(min = 1)
https://phauer.com/2019/modern-best-practices-testing-java/ https://phauer.com/2018/best-practices-unit-testing-kotlin/ https://kotest.io/docs/proptest/property-test-generators.html