https://kotlinlang.org logo
#mockk
Title
# mockk
m

Michael de Kaste

08/11/2022, 8:28 AM
what do you all consider the best practice when it comes to typing/naming mockks and why? (not really mockk specific question but its something we keep coming across in our team) 1. val namingFactory: NamingFactory = mockk() 2. val namingFactory = mockk<NamingFactory>()
1️⃣ 1
m

Mattia Tommasone

08/11/2022, 8:31 AM
i don’t think it makes a real difference, the IDE will recognize your
namingFactory
to be a
NamingFactory
in either case. I couldn’t find anything about it in the kotlin style guide, so i really think that’s up to you and your team
k

Klitos Kyriacou

08/11/2022, 8:36 AM
I would tend to call it
mockNamingFactory
though.
t

thanksforallthefish

08/11/2022, 8:42 AM
I like option two, and I think kotlin prefers two too just try to extract a mockk and idea does two, eg
Copy code
val helper = Helper(mockk())
now try to extract
mockk()
to a variable, using idea refactoring and it becomes
Copy code
val whatever = mockk<Whatever>()
I would do the same manually if not to keep style consistent and not having to “fix” the style manually
k

Klitos Kyriacou

08/11/2022, 8:44 AM
But that's option 2.
t

thanksforallthefish

08/11/2022, 8:44 AM
indeed, I am consistent also with typos 😄
m

Michael de Kaste

08/11/2022, 10:18 AM
@thanksforallthefish I tend to agree, but generating implementations of interfaces always makes variables like so:
Copy code
override val someValue: Int
    get() = TODO()
so trusting how intellij does it might not be a good argument (for me against my colleagues :P)
o

Ondřej Šimon

08/15/2022, 5:10 PM
Personally I use the first approach, also suffixing the mocked instance with
Mock
, e.g.
someServiceMock
.
t

Tom Yuval

08/20/2022, 5:32 PM
For what it’s worth, note the difference between a member property and a local variable: • For a member property, it seems to be customary (though not obligatory, and not always the case, even in the standard library) to specify the type explicitly (which explains the example of implementing an interface above). • For a local variable, it seems to be customary not to specify the type explicitly lacking a good reason to do so. Personally, I would go for 1️⃣ if it’s a member property and for 2️⃣ if it’s a local variable.
(And I think
namingFactory
is perfectly fine, no need to specify it’s a mock explicitly in the name unless the context requires disambiguating between two naming factories one of which is a mock and one isn’t.)
i

igor.wojda

08/30/2022, 3:05 PM
2nd is cool, but I find 1st is easier to write, so I go with 1st
3 Views