Hey there. I have some data classes that don't hav...
# announcements
d
Hey there. I have some data classes that don't have default values (because it doesn't make sense in the production enviroment) but I would like to have an easy way to create instances of those classes with default values in my unit / integration test. Right now I'm doing sth. like this
Copy code
fun createAddCartItemDto(
        partnerId: Long = 29L,
        productId: Long = 40L,
        productVariantLabel: String = "A1",
        testName: String? = null,
        bucketGroup: String? = null
) = AddCartItemDto(
        partnerId = partnerId,
        productId = productId,
        productVariantLabel = productVariantLabel,
        bucketGroup = bucketGroup,
        testName = testName
)
but i'm not the biggest fan of the duplicated parameter list .. better ideas?
m
You don't need the parameter names in the constructor:
Copy code
fun createAddCartItemDto(
        partnerId: Long = 29L,
        productId: Long = 40L,
        productVariantLabel: String = "A1",
        testName: String? = null,
        bucketGroup: String? = null
) = AddCartItemDto(partnerId, productId, productVariantLabel, bucketGroup, testName)
d
not sure if that actually improves it or makes it worse. adding a new parameter now means figuring out the "correct position"
g
If you use Android Studio “change signature” refactoring (cmd+f6) it will create the spot for you
m
Why is the
createAddCartItemDto
function required? It seems like the constructor would be just as useful, and save some code.
d
because i have hundreds of tests and almost all of the tests will use the same product id, the same partner id etc and it makes the tests way cleaner if i only provide the parameters that are "relevant" for the test
🆗 1
Copy code
fun `adding a product with variant a1 to the cart should remove shipping fees`()
{
    val emptyCart = UUID.randomUuid();
    val dto = AddCartItemDto(productVariantLabel = 'A1')
    val cart = addItemToCart(emptyCart, dto)

    assertTrue(cart.charges.none { it.type == 'shipping_fee' })
}
vs.
Copy code
fun `adding a product with variant a1 to the cart should remove shipping fees`()
{
    val emptyCart = UUID.randomUuid();
    val dto = AddCartItemDto(
        partnerId = 29L,
        productId = 40L,
        productVariantLabel = "A1",
        testName = null,
        bucketGroup = null
    )
    val cart = addItemToCart(emptyCart, dto)

    assertTrue(cart.charges.none { it.type == 'shipping_fee' })
}