https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
j

Jan Stoltman

09/14/2019, 3:21 PM
Hey guys, is there any equivalent of
@TestOnly
or
@VisibleForTesting
annotations for K/Mp out there?
a

Arkadii Ivanov

09/14/2019, 3:25 PM
I believe these annotations are anti-patterns. Drive your class to a required state using public API. As it will work in production.
👍 1
j

Jan Stoltman

09/14/2019, 3:29 PM
Yeah, for sure it's an anti-pattern but my problem is not something I nicely solve with public API
a

Arkadii Ivanov

09/14/2019, 3:30 PM
Then maybe extract to a separate dependency. I completely stopped using this in all my projects.
j

Jan Stoltman

09/14/2019, 3:32 PM
I could explain my use case to you if you've got time, but trust me so far annotations are the the best solution. So from what you're saying I recon that there is no alternative for K/Mp?
s

saket

09/14/2019, 4:03 PM
The thing about anti-patterns are that they exist in all fairly large codebases and only increase over time, often marked as
TODO: fix once we've time
. The same can be said for powermocks, spies, etc. They're evil, but are incredibly useful in cases like untested legacy projects where you'd want to incrementally add tests without requiring to refactor the entire graph of classes in one go.
r

russhwolf

09/14/2019, 4:19 PM
I think you can get close by using an experimental annotation. Try defining something like
Copy code
@Experimental
annotation class VisibleForTesting
Now if you have a declaration like
Copy code
@VisibleForTesting
public fun foo() { ... }
then you’ll get warnings/errors when using
foo()
. You can then annotate your test classes with
@UseExperimental(VisibleForTesting::class)
to suppress only in your tests.
👍 1
j

Jan Stoltman

09/14/2019, 4:28 PM
@russhwolf That sounds like a decent workaround, thanks!
68 Views