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

mg6maciej

02/17/2017, 1:29 PM
orogersilva: I haven't found it useful to mock classes in Kotlin. Can you show me a reference to some example code how and why do you use this feature?
o

orogersilva

02/17/2017, 1:44 PM
I just updated the post with a link for an open source project that implements unit tests with the mocks support.
👍 1
The app is in the initial phase. But, I will be implementing more tests over time.
m

mg6maciej

02/17/2017, 1:57 PM
@orogersilva So if you just change both params of
PubRepository::getInstance
to
PubDataSource
type everything will still compile you won't have a need for class mocking support.
o

orogersilva

02/17/2017, 2:08 PM
@mg6maciej Unit tests requires mocks for classes to be tested in isolation. That is, without the influence of dependencies. This can be done via dependency injection.
I agree by changing both params. But, as I said before, the project is in initial phase. Improvements will be made yet.
m

mg6maciej

02/17/2017, 2:11 PM
I usually have interfaces or function types as dependencies for things I unit test. In your example it's also possible to easily change the code to depend on interfaces instead of classes.
Not an example that would convince me to use final classes mocking feature.
👍 1
o

orogersilva

02/17/2017, 2:22 PM
@mg6maciej I agree with you (again). But, depending on context, it is not good practice to introduce interfaces exclusively to make unit testing possible. For the example context it is a good idea. However, there are contexts that do not.
What ends up making it necessary to mock final classes.
m

mg6maciej

02/17/2017, 2:27 PM
Well, let me know if you have an example that shows this.
2 Views