https://kotlinlang.org logo
#feed
Title
# feed
s

Stanislav Kozlovski

05/14/2020, 3:00 PM
Just had my latest piece on Kotlin’s lack of
package-private
published: https://levelup.gitconnected.com/kotlin-unit-testing-classes-without-leaking-public-api-871468695447 I’m sure you’re all aware of it, but I thought I’d share 🙂
t

Thomas

05/14/2020, 4:15 PM
There is a rather good discussion in the comments at https://henrikwarne.com/2014/02/09/unit-testing-private-methods/ that talks some about this topic.
c

Cody Engel

05/14/2020, 8:23 PM
I've found that classes being internal works out the best. You can provide instances of those classes to your public classes as either mocks or concrete instances depending your liking. Open class is a code smell to me since you are allowing for a class to be extended for the sake of tests. Making functions protected when they could be private is another smell IMO, if they shouldn't be extended then they shouldn't be extended.
k

karandeep singh

05/15/2020, 12:19 PM
how about using `@VisibleForTesting`Annotation?
c

Cody Engel

05/15/2020, 2:45 PM
I can see how it could be useful. Personally I've never had a need for that though. More or less following SOLID leads to fairly easy to test code.
k

karandeep singh

05/15/2020, 5:26 PM
sometimes when you need to aim that 70%+ coverage you probably need to do these kind of things. 😛
c

Cody Engel

05/15/2020, 5:28 PM
I've hit 70%+ coverage without it which is usually testing everything minus Views (but including viewmodels). If you find yourself having to test private functions more often than not it's a code smell.
👍 1
2 Views