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...

КОМЕНТАРІ • 162

  • @ИванЕмелин-ш9ш
    @ИванЕмелин-ш9ш 2 роки тому +3

    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?

    • @PhilippLackner
      @PhilippLackner  2 роки тому +1

      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

    • @igormorozov6311
      @igormorozov6311 Рік тому

      I had a same question, and I am very surprising that only we two ask it ☝

  • @matt-g-recovers
    @matt-g-recovers 3 роки тому +13

    In all seriousness, I really think this is the best Android playlist on UA-cam.

  • @yunusemrearslan3696
    @yunusemrearslan3696 3 роки тому +19

    Not all heroes wear capes...
    You are a true legend dude.
    You saved my life thank you so much.

  • @eczema59
    @eczema59 Рік тому +1

    Hello, i have problems with testing. In ShoppingDaoTest Android studio cannot resolve ShoppingItemDatabase and ShoppingDao. How can i fix this?

  • @hoonjung2143
    @hoonjung2143 3 роки тому +7

    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!

  • @mcgaps6205
    @mcgaps6205 3 роки тому +4

    12:30 LiveDataUtilAndroidTest
    17:42 InstantTaskExecutorRule

  • @androidinsight8146
    @androidinsight8146 8 місяців тому +1

    Excellent playlist ever seen for Testing 🎉🎉🎉🎉

  • @ampo2004
    @ampo2004 4 роки тому +7

    Incredible as always Philipp, I've learned a lot of things in these testing videos

  • @matt-g-recovers
    @matt-g-recovers 3 роки тому +1

    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 ;)

  • @daniboy943
    @daniboy943 2 роки тому +2

    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?

    • @abhimanyu.n14
      @abhimanyu.n14 2 роки тому +3

      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.

    • @daniboy943
      @daniboy943 2 роки тому

      @@abhimanyu.n14 Nice, thanks for the answer!

  • @pradeepkumarreddykondreddy7048
    @pradeepkumarreddykondreddy7048 3 роки тому +1

    What is the difference between @RunWith(AndroidJUnit4::class) and @RunWith(AndroidJUnit4ClassRunner::class) ?

  • @SpaceTimeBeing_
    @SpaceTimeBeing_ 4 роки тому +3

    Can you show tests for one to many and many to many relation entities

  • @m.irtizakhursheed3040
    @m.irtizakhursheed3040 3 роки тому +2

    How we can generate code coverage of instrumented test in the AndroidTest folder.

  • @GrahamFirst
    @GrahamFirst Рік тому +1

    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).

  • @dennisthegamer2376
    @dennisthegamer2376 Рік тому

    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.

  • @codingWithAj
    @codingWithAj Рік тому

    runBlockingTest{ } is now deprecated in favour of runTest{ }

  • @sergeytarasov5041
    @sergeytarasov5041 2 роки тому +1

    One of the best explaination of room db testing. Thank U, good work.

  • @user-sankarsana
    @user-sankarsana 3 роки тому +1

    I enjoyed it very much! Thanks.
    But I suppose using liveData is not very good in this case.

  • @safionweb
    @safionweb 3 роки тому +2

    The comparison table shows that @Small tests can't access the database. Then how are we accessing it?

  • @harishodzic3274
    @harishodzic3274 Рік тому

    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.?

  • @judekwashie8177
    @judekwashie8177 3 роки тому +1

    Great video but I have one question. What exactly does this line
    \ "@get:Rule"
    var instantantTaskExecutorRule = InstantTaskExecutorRule()\ do?

    • @innocentkyalo7688
      @innocentkyalo7688 3 роки тому +2

      A JUnit Test Rule that swaps the background executor used by the Architecture Components with a different one which executes each task synchronously.

  • @whitebl4ckk
    @whitebl4ckk 4 роки тому +1

    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

  • @michaguspiel5522
    @michaguspiel5522 3 роки тому +1

    Loved the video! I ran into a problems with my database and was searching for a way to test it, luckily found this video!

  • @chi94
    @chi94 Рік тому

    this got complicated real quickly lol

  • @dmytromarchuk3023
    @dmytromarchuk3023 3 роки тому +1

    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

    • @PhilippLackner
      @PhilippLackner  3 роки тому +1

      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

    • @dmytromarchuk3023
      @dmytromarchuk3023 3 роки тому +2

      @@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!

  • @swapnilkadam1391
    @swapnilkadam1391 2 роки тому +1

    hey Philipp, thanks for making this. How should we handle flow?

  • @ritamdey6262
    @ritamdey6262 4 роки тому +1

    Hey! Is turning on Main Thread Query execution really necessary?

    • @PhilippLackner
      @PhilippLackner  4 роки тому

      Well, we want to execute everything in the main thread, so yes

  • @AwesomeDevNotes
    @AwesomeDevNotes 4 роки тому +1

    𓌤 𓌥 𓌦 𓌧 𓌨

  • @studyonline3236
    @studyonline3236 11 місяців тому

    This is an android-testing bible.

  • @mehdiparsaei1867
    @mehdiparsaei1867 3 роки тому +1

    Thank for your amazing course.

  • @liveonyoutube5487
    @liveonyoutube5487 4 роки тому +1

    Woow, fantastic bro. Finally I found what I am looking for 😁👏

  • @Aspiret620
    @Aspiret620 Рік тому

    23:15
    Lol Philipp Lackner unrated

  • @ahmedaboshady9733
    @ahmedaboshady9733 2 роки тому

    thank you very much😍

  • @atulbirthwal3931
    @atulbirthwal3931 2 роки тому

    Hey Philipp,
    I am just curious about that LiveData works asynchronously? can you please explain

  • @travelogue.austria
    @travelogue.austria 2 роки тому

    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?

  • @eldercampanhabaltaza
    @eldercampanhabaltaza Рік тому

    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

  • @aceoftheplays4664
    @aceoftheplays4664 3 роки тому

    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

  • @nayeramohamed436
    @nayeramohamed436 3 роки тому

    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?

  • @leilasedighi1396
    @leilasedighi1396 2 роки тому

    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

  • @ifeakachukwuosakwe
    @ifeakachukwuosakwe 3 роки тому

    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

  • @saadbiniqbal9024
    @saadbiniqbal9024 3 роки тому

    👏👏👏👏👏

  • @arielapp9469
    @arielapp9469 3 роки тому

    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.

  • @VeronikaDoma-f5y
    @VeronikaDoma-f5y Рік тому

    Thank you! 👏🙏

  • @prkofc
    @prkofc 3 роки тому

    I am getting this "Test running failed: Process crashed."

  • @Axelfromthehudjelud
    @Axelfromthehudjelud 2 роки тому

    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. ?

  • @AlgeriaHere
    @AlgeriaHere 4 роки тому

    Very helpful video , thank you .

  • @vishnusimha8264
    @vishnusimha8264 3 роки тому

    Hi @philip Lackner I don't see any code in the git hub
    can you or someone please share the source code?

  • @mehranbehbahani3050
    @mehranbehbahani3050 3 роки тому

    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.

  • @jeylaiskender8500
    @jeylaiskender8500 3 роки тому

    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 :)

  • @marlonlom
    @marlonlom 3 роки тому

    if the room database usage is in an android library, which setup can i follow for perform the testing?

  • @kannang2759
    @kannang2759 2 роки тому

    Good tutorial

  • @aviksutar
    @aviksutar 2 роки тому

    Love your course.great doing my favourite coach. keep going man..

  • @mahmoudelgamal7320
    @mahmoudelgamal7320 3 роки тому

    very very good

  • @youtubecomment1150
    @youtubecomment1150 3 роки тому

    Gold mine!

  • @JamesBond-mq7pd
    @JamesBond-mq7pd 3 роки тому

    10:08 tried to press Control + Space but nothing happened.

  • @akshaychande9698
    @akshaychande9698 3 роки тому

    How to see code coverage for written test cases?n how to exclude file or packges from that coverage

  • @SheikhNasratSharif
    @SheikhNasratSharif 2 роки тому

    Awesome ❤

  • @jeebanbagdi3396
    @jeebanbagdi3396 3 роки тому

    Nothing to say... just amazing. Now after seeing your videos I have fall in love with Unit Testing.

  • @danju132
    @danju132 2 роки тому

    Thanks

  • @amineayachi335
    @amineayachi335 2 роки тому

    thanks

  • @SerhiiOr
    @SerhiiOr 4 роки тому +1

    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?

    • @PhilippLackner
      @PhilippLackner  4 роки тому +2

      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

    • @SerhiiOr
      @SerhiiOr 4 роки тому

      @@PhilippLackner and theoretically we can do all this stuff in test directory?

    • @PhilippLackner
      @PhilippLackner  4 роки тому +2

      @@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

    • @SerhiiOr
      @SerhiiOr 4 роки тому

      @@PhilippLackner thanks!

  •  Рік тому

    The simplest and most understandable video. Thank you so much. 💯💜🙏

  • @JamesBond-mq7pd
    @JamesBond-mq7pd 3 роки тому

    13:20 can you provide original link of that code?

  • @erickramones2167
    @erickramones2167 2 роки тому

    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?

  • @Berk45632
    @Berk45632 3 роки тому

    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"

  • @pejmanazad5852
    @pejmanazad5852 3 роки тому

    one question happen, why we need test room functionality like(CRUD)?

    • @PhilippLackner
      @PhilippLackner  3 роки тому

      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

  • @enzoteles2037
    @enzoteles2037 2 роки тому

    very nice, you are the best of the world!!

  • @mado6066
    @mado6066 2 роки тому

    Hi, Philipp how i test dao with Bitmab parameter?

  • @jorgevidal2543
    @jorgevidal2543 3 роки тому

    Gracias hombre simplemente buen contenido de calidad..!

  • @JuanDanielOrnella
    @JuanDanielOrnella 4 роки тому +1

    Amazing Bro!

  • @nicoqueijo
    @nicoqueijo 3 роки тому

    How come you are not using JUnit 5?

  • @gajendrapandeya5081
    @gajendrapandeya5081 4 роки тому

    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??

    • @PhilippLackner
      @PhilippLackner  4 роки тому

      In the end you just need SQL for that and UA-cam is full of that

  • @s.j.6096
    @s.j.6096 2 роки тому

    Why you don't write in the title if your work with kotlin or Java?

  • @marlonlom
    @marlonlom 3 роки тому

    which dependencies are needed for room database testing?

  • @animekingitachi
    @animekingitachi 2 роки тому

    It's really helpful tutorial

  • @rajkumarsingh8664
    @rajkumarsingh8664 2 роки тому

    Superb Dear you are amazing.

  • @rushidesai2836
    @rushidesai2836 3 роки тому

    Incredible. Thanks a lot!!

  • @GrahamFirst
    @GrahamFirst Рік тому

    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.

    • @PhilippLackner
      @PhilippLackner  Рік тому +2

      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

    • @GrahamFirst
      @GrahamFirst Рік тому

      @@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?

  • @codingwithsam4992
    @codingwithsam4992 2 роки тому

    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?

    • @PhilippLackner
      @PhilippLackner  2 роки тому

      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

  • @mehulbisht9708
    @mehulbisht9708 4 роки тому

    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? )

    • @PhilippLackner
      @PhilippLackner  4 роки тому +1

      I have 16gb of ram and you can use a physical device as well

    • @mehulbisht9708
      @mehulbisht9708 4 роки тому

      @@PhilippLackner cool

  • @thearpansircar
    @thearpansircar 4 роки тому

    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.

  • @Berk45632
    @Berk45632 3 роки тому

    23:14 Put a dollar in the swear jar 😂

  • @federicocreti4699
    @federicocreti4699 3 роки тому

    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

    • @PhilippLackner
      @PhilippLackner  3 роки тому +2

      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.

    • @federicocreti4699
      @federicocreti4699 3 роки тому

      @@PhilippLackner oh ok this is what I supposed... Ty bro ;)

  • @hosseinamini2443
    @hosseinamini2443 3 роки тому

    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

    • @PhilippLackner
      @PhilippLackner  3 роки тому

      No, but writing tests how your code interacts with them

  • @devetips
    @devetips 3 роки тому

    nicely nice thank you Sir

  • @ZaidZakir
    @ZaidZakir 3 роки тому

    you the best man!!!!!!!!!!!!!!

  • @puszkinowski
    @puszkinowski 3 роки тому

    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?

    • @akashdeepwadhwa5828
      @akashdeepwadhwa5828 3 роки тому +1

      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

    • @puszkinowski
      @puszkinowski 3 роки тому

      @@akashdeepwadhwa5828 Thank you :)

  • @arash1684
    @arash1684 3 роки тому

    Nice tutorial

  • @ivankvych8182
    @ivankvych8182 3 роки тому

    Very exciting!!

  • @juancarlosnino379
    @juancarlosnino379 3 роки тому

    thx a lot!

  • @rautermann
    @rautermann 3 роки тому

    The part with runBlockingTest seems to be Kotlin specific - how would I do that with Java?

    • @PhilippLackner
      @PhilippLackner  3 роки тому +1

      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

    • @rautermann
      @rautermann 3 роки тому

      @@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)

    • @PhilippLackner
      @PhilippLackner  3 роки тому

      @@rautermann german spotted :D

    • @rautermann
      @rautermann 3 роки тому

      @@PhilippLackner Liebe Grüße aus Wien ;P

    • @PhilippLackner
      @PhilippLackner  3 роки тому +1

      @@rautermann Grüße aus Hannover zurück 💪

  • @markiianmykush9124
    @markiianmykush9124 4 роки тому

    Awesome! Waiting next video!

  • @pratikchauhan989
    @pratikchauhan989 4 роки тому

    Thanks for so easy tutorial on testing

  • @saumilpatel2480
    @saumilpatel2480 4 роки тому

    Thank you Philip..

  • @chiragprajapati3989
    @chiragprajapati3989 4 роки тому

    nice explanation

  • @antoniokomangyudistira4150
    @antoniokomangyudistira4150 4 роки тому

    Thank you sir

  • @haissayf
    @haissayf 4 роки тому

    Thank you.

  • @frankleth9380
    @frankleth9380 4 роки тому

    You are the best. I'm always waiting for your videos