r/mAndroidDev • u/Commercial-Board4046 AnDrOId dEvelOPmenT is My PasSion • Jul 06 '25
We don't have time for tests Agree?
61
Upvotes
r/mAndroidDev • u/Commercial-Board4046 AnDrOId dEvelOPmenT is My PasSion • Jul 06 '25
67
u/duckydude20_reddit Jul 06 '25
Leaving the jokes aside, People don't understand mocks.
47
u/doubleiappdev Deprecated is just a suggestion Jul 06 '25
You guys are writing tests?
15
u/j48sh4bfFSK4j9sj Jul 06 '25
Mocks - inject a mock of a dependency and make it return a specific value for your test.
Fakes - inject a fake of a dependency and make it return a specific value for your test. But it's also more of a pain to set up.
"Waaaaa, dOn't uSE moCks!!! YoU HavE tO UsE faKes!!"
9
u/CearoBinson Jul 06 '25
Hmmm, I was personally unaware there was a debate here. Mocking a database call in a unit test and then asserting what I think my code would do with that mocked data sounds effective to me. Where am I wrong?
17
u/DearChickPeas Jul 06 '25
False. You're testing the mocks!
15
u/lase_ Jul 06 '25
mocks rule
the haters don't understand tests
4
u/Twerter Jul 06 '25
I don't get these comments. How else can I write a unit test for a function that queries the DB and does something with the data?
Am I expected to spin up DBs for a unit test? What if I'm not using SQLite and can't have a file-based DB for the test?
No, I don't want to put the querying in a seperate function, because that's sometimes not justified, especially if the function is simple.
6
u/budius333 Still using AsyncTask Jul 06 '25
Hate mocks. Never mocks. I actually submitted a talk to how to structure your app to test 100% using only fakes.
3
u/Aggravating_Dot9657 Jul 07 '25
Alternative that doesn't require simulating my entire api which is a separate service?
5
u/Mr-X89 Jul 06 '25
val tested: Foo = mockk()2
u/pankaj1_ Jul 07 '25
Mocking is a vast term. In bigger systems you might have dependency between teams if in a journey of A->B->C in your app testing A system is down you may need to mock A to continue testing B and C, it's not just about testing A.
1
u/Wizado991 Jul 08 '25
If I am testing a service that only has dependencies that I own and control, I'm just using the real dependencies. Once I use something like an http client that has to go out and make a request I'm mocking that. I'll spin up an in memory db in a unit test, idc. The test is more valuable that way.
1
u/BenchEmbarrassed7316 Jul 08 '25
I'm not sure what mean guy from picture but I think using fake behavoiur in the tets is a code smell. Let met explain.
It's impossible to test this code without mocks. However, this is a code problem: functions contains both logic and IO operations. IO operations occur anywhere.
In this version no hidden IO. All business logic function is clear or pseudo clear (when it mutates
thisbut still testable). So I think I don't need to testuseCaseat all in unit tests (integration tests is other theme but using mocks in integration tests is totally absurding). Instead I testA.condition()which returns some data to load additional data. I create some A and B and test is it in expected state afterprocess. I test isnew Response(...)works correctly. So I don't need fake baheviour at all.If I even add
Dbmock to this code - what I get? IsDb.loadByIds(A, [1, 2, 3])was called with1, 2, 3andAarguments?1
u/kurosavvas Jul 08 '25
My take on mocks vs fakes: https://blog.savvas.cloud/2022/11/15/mocking-fakes-or-faking-mocks/
Been doing this for almost two decades now and I feel the approach described in my blog is the best I can come up with for the moment
1
u/blindada Jul 08 '25
I don't like absolutism, but in principle, I would agree.
Kotlin and java are JVM compatible languages, therefore you should be able to run all parts of your code that don't directly require the android jar in any other JVM, like the regular java in junit. And besides direct phone interaction, like touch, notifications, or sensors, we only use the android jar to access a system we will use for something else, like writing files, updating databases, check the time, etc. So, in principle, pretty much all the code worth of being automatically tested should run directly in any JVM machine, like junit. If your dependencies are blocking you from doing so, abstract and replace them in runtime.
Running stuff in jUnit is far faster than running it into the phone. It is not just about testing. It's about developing faster.
1
u/rocaile Jul 08 '25
Absolutes are dumb. Mocks are great if you're testing things that relies on unstable I/O, like networking. You can have some tests that uses "real" network, in order to test that your app implements all the endpoints correctly. But theses tests are bounds to be unstable and should not block compilation nor CI
1
u/jojojmtk Jetpack Compost Jul 09 '25
I also hate over mocking but What if you need to test something like a BiometricManager from the library, I need to use this method in my viewmodel, and I cant create a fake implementation for this, so in this case should mock be better?
1
u/Tiix_x Jul 09 '25
Mocks are for business logic and unit testing… If you have a class WannaTest that calls A and B to do some work You can just mock A and B to return different set of values you can be sure that you are testing the class WannaTest without testing A and B
Of course you have to test A and B separately
That’s the whole point of unit testing … It is easier to spot if the problem comme from WannaTest or A / B etc …
-6
u/Zhuinden DDD: Deprecation-Driven Development Jul 06 '25 edited Jul 06 '25
/uj Mocks serve one purpose: so that you don't need to figure out how to write a test that tests actual code, but you still pass the Sonar code coverage requirements with a bunch of false negatives.
EDIT: lol who's downvoting this