Testing Room Databases - Testing on Android - Part 6
Вставка
- Опубліковано 30 вер 2024
- In this video we will test the functions we have defined in the ShoppingDao in the last video.
⭐ Get certificates for your future job
⭐ Save countless hours of time
⭐ 100% money back guarantee for 30 days
⭐ Become a professional Android developer now:
pl-coding.com/...
💻 Let me personally review your code and provide individual feedback, so it won't backfire and cost you a fortune in future:
elopage.com/s/...
Get the source code for this video here:
github.com/phi...
Regular Android tutorials on my Instagram: / philipplackner_official
Checkout my GitHub: github.com/phi...
Good day, please tell me at 3:13 you said that we add the @RunWith(AndroidJUnit4::class) annotation to indicate to Junit that these tests are instrumented. And then at 3:50 you said that the @SmallTest annotation is needed to show that unit tests are inside. Don't these two annotations contradict each other?
No, a unit test can be instrumented or local. Unit test just means it tests a single instance (class/function) in your code. Local and instrumented just mean it either runs on the jvm or an android device
I had a same question, and I am very surprising that only we two ask it ☝
In all seriousness, I really think this is the best Android playlist on UA-cam.
Not all heroes wear capes...
You are a true legend dude.
You saved my life thank you so much.
kerennn
Hello, i have problems with testing. In ShoppingDaoTest Android studio cannot resolve ShoppingItemDatabase and ShoppingDao. How can i fix this?
Hey Phil!
Loving you content, especially the testing series as a lot of job offers qant some experience in TDD and I didn't (or couldn't? haha) find any comprehensive, extensive testing tutorials for Android. Thank you!
I just wanted to mention that we can speed up the tests by running them as local unit tests via a simulated device (not emulated) by using Robolectric. ^^
Keep up the good stuff!
12:30 LiveDataUtilAndroidTest
17:42 InstantTaskExecutorRule
Excellent playlist ever seen for Testing 🎉🎉🎉🎉
Incredible as always Philipp, I've learned a lot of things in these testing videos
Thanks man, glad you like it ✌️
Now and then you learn something so good it just makes your hair stand on end, tingles of numbly in my flesh as I feel the power flow through my fingers... but it is so simple and kinda seemingly stupid, as in no big deal...Don't judge me.
CTL + to see our available functions.
YES!!! hahahaha
Oh the testing is good too ;)
Hi, Philipp! I understand that @SmallTest is for Unit tests, but at the same time we are testing our database (DAO is part of that, right?). In the table at 4:23 it shows Database tests should use @MediumTest, so this got me a bit confused. Could you explain why @SmallTest for DAO interface testing?
We actually are not testing the database, but the Dao code. Database is used to refer to the persistent data storage. In tests we are using "in memory database" - which stores the data only in memory. (RAM). The data is not persisted once the test completes. Integration tests can use actual database if required.
@@abhimanyu.n14 Nice, thanks for the answer!
What is the difference between @RunWith(AndroidJUnit4::class) and @RunWith(AndroidJUnit4ClassRunner::class) ?
Can you show tests for one to many and many to many relation entities
How we can generate code coverage of instrumented test in the AndroidTest folder.
I believe with suspend functions in your DAO, you don't need "allowMainThreadQueires", since Room will actually do the work on another thread, even if the coroutine from which you're calling it is currently executing on the main thread. Of course, you must be in a coroutine in order to call a suspend function, which is why it's recommended for Room DAO functions that don't return something observable (e.g. LiveData, as you pointed out).
Thanks for the example. But wouldn't testing a repository make more sense? Testing a DAO doesn't really seem necessary to me, it mainly tests room, and maybe your skill to write basic SQL queries xD.
runBlockingTest{ } is now deprecated in favour of runTest{ }
One of the best explaination of room db testing. Thank U, good work.
I enjoyed it very much! Thanks.
But I suppose using liveData is not very good in this case.
The comparison table shows that @Small tests can't access the database. Then how are we accessing it?
Yeah i'm wondering the same thing
This is unit test right? Why are logic and testClasses added to and "androidTest" folder. Shouldn't unit tests be in "android" folder and UI tests in "androidTest" folder.?
Great video but I have one question. What exactly does this line
\ "@get:Rule"
var instantantTaskExecutorRule = InstantTaskExecutorRule()\ do?
A JUnit Test Rule that swaps the background executor used by the Architecture Components with a different one which executes each task synchronously.
Well done brother, awesome video, I have just one question, what about foreign keys, when one entity requiers others entities to be populated.
Should I put all code in one test class
Loved the video! I ran into a problems with my database and was searching for a way to test it, luckily found this video!
this got complicated real quickly lol
Would you mind telling me the purpose of testing the databases? Room has been developed by Android team so they are responsible for testing it
In this simple example it probably doesn't make that much sense, but imagine you have complex sql queries and you want to test if your queries are correct and also if they still work as expected after changing them
@@PhilippLackner that makes much more sense. If you have custom queries - they have to be tested, because once you have to update the SQL - you can break something. Thanks!
hey Philipp, thanks for making this. How should we handle flow?
did u find a solution for flow?
Hey! Is turning on Main Thread Query execution really necessary?
Well, we want to execute everything in the main thread, so yes
𓌤 𓌥 𓌦 𓌧 𓌨
This is an android-testing bible.
Thank for your amazing course.
Woow, fantastic bro. Finally I found what I am looking for 😁👏
Glad to hear that! :)
23:15
Lol Philipp Lackner unrated
thank you very much😍
Hey Philipp,
I am just curious about that LiveData works asynchronously? can you please explain
Thank you so much for this wonderful tutorial
I have a problem with this episode
I wrote the code and got this Error:
java.lang.RuntimeException: cannot find implementation for com.application.unittest.data.local.ShoppingItemDatabase. ShoppingItemDatabase_Impl does not exist
at androidx.room.Room.getGeneratedImplementation(Room.java:100)
at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:1486)
at com.application.unittest.data.local.ShoppingDaoTest.setup(ShoppingDaoTest.kt:38)
I checked stackoverflow but those solutions didn't work for, so what should I do?
Thanks so much for the content you are sharing. I am moving from React Native Development back to Android Development and your videos have been a great source for me to catch up with the latest technologies available. Also, as I was running the project (that I've downloaded from the GitHub link available on this video description), I faced an issue when trying to run the first test on my Mac M1. There is an incompatibility between the room version on your GitHub project and M1 Macs. This issues has been resolved on later version of room starting at 2.4.0
Hi, does anyone have the LiveDataUtil class but in java ? I am kind of struggling with converting it. Yes I do java it is a requirement dont bite my head off for it :D
Can you please clarify why we didn't include a test case for observeAllShoppingItems while we did include a test case for observeTotalPrice, both of them returns live data?
Hello. Thanks for this grate tutorial.
Just there is one question for me: We shouldn't test every single code indivigualy? for find out easier witch part of code causing the error?
Thanks in advance
I just googled - how to test Android room database in Kotlin - and this channel came up ... Thank you so much for this tutorial 😊 ... You saved me
👏👏👏👏👏
I'm having an error while following the tutorial in my project.
the difference I have is that I have an autoGenerated primary key field in my Entity.
the error I'm getting is that the database does not contain the object that I put in it.
the reason is that it generates the primary key than put it in the database.
is there a way to make sure the objects are the same?
thank you in advance.
Thank you! 👏🙏
I am getting this "Test running failed: Process crashed."
I have a question, are those tests independent? We are testing our Insert function but we are calling observeAll function in the test, We are testing our Delete function, but we are calling insert and delete functions inside the test, etc. ?
Very helpful video , thank you .
You are welcome!
Hi @philip Lackner I don't see any code in the git hub
can you or someone please share the source code?
When testing the insert function, what if the query function doesn't work as expected and so the test case fails. is there a way not to depend the test case of a function (insert function) on another function that we have written ourselves (query function)? This part seemed a bit strange to me.
Thank you very much! I am currently enrolled to Udacity Nanodegree and I find you videos more detailed for beginners it is very important! Subscribed :)
if the room database usage is in an android library, which setup can i follow for perform the testing?
Good tutorial
Love your course.great doing my favourite coach. keep going man..
very very good
Gold mine!
10:08 tried to press Control + Space but nothing happened.
How to see code coverage for written test cases?n how to exclude file or packges from that coverage
Awesome ❤
Nothing to say... just amazing. Now after seeing your videos I have fall in love with Unit Testing.
Thanks
thanks
Bro, why we use @SmallTest in androidTest?Shouldn't we use @MediumTest? Or it doesn't matter where to use, only has sence what we have inside test?
Small test means Unit test. And unit test means we only test one class. And here we only test our Dao, so all of these are unit tests
@@PhilippLackner and theoretically we can do all this stuff in test directory?
@@SerhiiOr yes, but then you need sqlite installed on your pc. You shouldn't do that as I said because then you're using another sqlite version than on your android device
@@PhilippLackner thanks!
The simplest and most understandable video. Thank you so much. 💯💜🙏
Glad it was helpful!
13:20 can you provide original link of that code?
the run annotation indicates it is an integrated test and small test annotation indicates that it is a unit test. What kind of test is it? is it both at the same time?
Tests are design to check if our code has bugs.
* Philipp Testing *
Philipp : "Oh. We get an error here... Let's take a look in our test case; something must be wrong here"
😂😂😂
one question happen, why we need test room functionality like(CRUD)?
In this simple case for demonstration it doesn't make much sense, but if you have complex queries, it makes sense to test if these are working as expected
very nice, you are the best of the world!!
Hi, Philipp how i test dao with Bitmab parameter?
Gracias hombre simplemente buen contenido de calidad..!
Amazing Bro!
How come you are not using JUnit 5?
bro just loving the series. But i have some queries. There's not even a single video in entire youtube about working with multiple table with room. Please can you create one very simple tutorial about that??
In the end you just need SQL for that and UA-cam is full of that
Why you don't write in the title if your work with kotlin or Java?
To trigger Java developers
@@PhilippLackner OK, I see.
which dependencies are needed for room database testing?
It's really helpful tutorial
Superb Dear you are amazing.
Incredible. Thanks a lot!!
Also, in my experience, a new instance of your test class (e.g. "ShoppingDaoTest" in your example) is created to run each test case (function/method), and so I've had success just using "by lazy" to initialize things such as the DB, which is a bit simpler than "lateinit var" and an "@Before" method.
Lazy will keep the same reference throughout tests, while @Before creates a new one for each test. You don't want that a test case fails be aus a previous test manipulated a field lime the DB
@@PhilippLackner Thanks for your quick reply. I'm really curious now and would love to debug this with you if you're willing.
Lazy will keep the DB around as long as the instance of the test class exists, and if the same instance of the test class were used to run more the one individual test, of course that would be bad because it would break isolation between the test cases.
In pratice, however, I always find that Android Studio creates a new instance of the test class for each individual test case, even if I run them all together in one "batch". I've confirmed this by debugging. It's because of this behaviour that Lazy has worked just fine for me in practice.
I'm really curious whether you see the same thing, or is it a quirk of my setup?
Perhaps this behaviour isn't guaranteed, and perhaps that is the reason behind the best practice of assuming that the test class instance might be reused?
I have a question. Why do we need to test database as it would be much simpler to open app inspection and see whats going on in the data base as we add or remote entity from mobile?
It doesn't make sense in terms of simple insertions/deletions. However if you have complex queries, you can make sure those work the way they should
16:51 how much is the RAM on your System ? my laptop has 4 GB and i don't think it will work for me since running an android emulator slows it down a lot. Can a physical device also work for these test cases ? ( My laptop does have Intel I5 - 10th gen with 1 TB HDD and 256 GB of SSD, still i think the 4 GB ram is costing me the emulator, your opinion on this please? )
I have 16gb of ram and you can use a physical device as well
@@PhilippLackner cool
Lmao, I saw this channel name and was about to inform you that someone was using your videos. Then I remembered how you made an entire video about changing your channel name.
Lol :D
23:14 Put a dollar in the swear jar 😂
Hi Philip, thanks for this course. But I have a question: what is the purpose of these tests you are writing in this specific video? I mean, what are you really testing? The functions you are testing are not even our functions, they are abstract and are implemented by Room, so what is exactly testing? The only thing I can suppose is that you are testing the query you wrote as annotations on top of dao functions, because maybe sometimes we can have complex queries, am I right?
EDIT: just to be more clear, for example you are testing that insert a product insert really a product, but how is possible this cannot happens? In the end, this is done by Room, so of course it will be inserted...I hope you understand where is my doubt :D
Sure, this example here is super easy and wouldn't make sense in an actual more complex app. The point is just to show how it would work. For more complex queries and database flows it definitely makes sense to test that. It's not about testing if Room behaves right, but if you actually wrote your queries correctly.
@@PhilippLackner oh ok this is what I supposed... Ty bro ;)
In a real project, is it a good idea to write tests for third-party libraries? Because most of these libraries are tested by their creators
No, but writing tests how your code interacts with them
nicely nice thank you Sir
you the best man!!!!!!!!!!!!!!
great vid. What exactly does 'get' before Rule annotation do ? Ok, i supposes its getting the rule :) but why is has to be there and not in others annotations?
i think to declare that instead of executing functions that performs asynchroniously ..use that rule object(to store the details of tasks who are running asynchroniously )to specify that functions should run in sequence because we are testing it in mainthread ..like write/update data then other methods like read and all that
@@akashdeepwadhwa5828 Thank you :)
Nice tutorial
Very exciting!!
thx a lot!
The part with runBlockingTest seems to be Kotlin specific - how would I do that with Java?
With Java, you couldn't use coroutines at all. You would probably use threads or so. There is no reason not to use kotlin for Android nowadays
@@PhilippLackner I see, thanks! And thank you for the great content!
(The reason to use Java is that I'm a beginner and have already tried to learn way too many languages this year... and btw, I think your pronunciation of kuh-routine is hilarious xD)
@@rautermann german spotted :D
@@PhilippLackner Liebe Grüße aus Wien ;P
@@rautermann Grüße aus Hannover zurück 💪
Awesome! Waiting next video!
Coming soon!
Thanks for so easy tutorial on testing
Most welcome 😊
Thank you Philip..
You're welcome :)
nice explanation
Thanks and welcome
Thank you sir
Welcome
Thank you.
You're welcome!
You are the best. I'm always waiting for your videos
Thanks 👌