Hey guys, is there any equivalent of `@TestOnly` o...
# multiplatform
j
Hey guys, is there any equivalent of
@TestOnly
or
@VisibleForTesting
annotations for K/Mp out there?
a
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
Yeah, for sure it's an anti-pattern but my problem is not something I nicely solve with public API
a
Then maybe extract to a separate dependency. I completely stopped using this in all my projects.
j
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
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
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
@russhwolf That sounds like a decent workaround, thanks!
304 Views