Saving Data on Android


com.raywenderlich.droidquiz



Download 19,28 Mb.
Pdf ko'rish
bet152/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   148   149   150   151   152   153   154   155   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

com.raywenderlich.droidquiz
 package with 
(androidTest)
 next to it.
Under that package, there is a 
QuizDaoTest.kt
 file. Open it by double-clicking and 
replace the contents inside with the following code:
@RunWith(AndroidJUnit4::class)
 
class
 
QuizDaoTest
 { 
// 1
 
  
@Rule
 
  
@JvmField
 
  
val
 rule: TestRule = InstantTaskExecutorRule() 
// 2
 
 
  
private
 
lateinit
 
var
 database: QuizDatabase 
// 3
 
  
private
 
lateinit
 
var
 quizDao: QuizDao 
// 4
 
}
Briefly, the above:
1. Creates a test class for your 
QuizDao
 called 
QuizDaoTest
.
2. Specifies that all tasks executed using Google's Architecture Components should 
be executed synchronously on the 
main thread
. This is very important since unit 
tests should be executed sequentially and synchronously.
3. Creates a 
lateinit var
 that will hold a reference to your Room database.
4. Creates a 
lateinit var
 that will hold a reference to your 
QuizDao
.
Saving Data on Android
Chapter 8: The DAO Pattern
raywenderlich.com
141


Next, add the following code:
@Before
 
fun
 
setUp
()
 { 
  
val
 context: Context = 
InstrumentationRegistry.getInstrumentation().context 
// 1
 
  
try
 { 
    database = Room.inMemoryDatabaseBuilder(context, 
QuizDatabase::
class
.
java
) //2 
        .allowMainThreadQueries() 
//3
 
        .build() 
  } 
catch
 (e: Exception) { 
    Log.i(
this
.javaClass.simpleName, e.message) 
//4
 
  } 
  quizDao = database.quizDao() 
//5
 
}
@Before
 is used to specify a method that should be executed before any test is run. 
Here is what's happening above:
1. Gets the context for this test and assigns it to 
context
.
2. Creates an in-memory version of your database. This means that all data will 
safely be deleted at the end of your test.
3.
allowMainThreadQueries()
 allows you to execute queries on the main thread. 
You need to call this method on your database builder, or Room will throw an 
error.
4. Logs an exception if the database can't be built.
5. Initializes your 
QuizDao
.
Next, add the following code:
@Test
 
fun
 
testInsertQuestion
()
 { 
  
// 1
 
  
val
 previousNumberOfQuestions = quizDao.getAllQuestions().size 
  
//2
 
  
val
 question = Question(
1

"What is your name?"

  quizDao.insert(question) 
  
//3
 
  
val
 numberOfQuestions = quizDao.getAllQuestions().size 
  
// 4
 
  
val
 numberOfNewQuestions =  
      numberOfQuestions - previousNumberOfQuestions 
  
// 5
 
  Assert.assertEquals(
1
, numberOfNewQuestions) 
  
// 6
Saving Data on Android
Chapter 8: The DAO Pattern
raywenderlich.com
142


  quizDao.clearQuestions() 
  
// 7
 
  Assert.assertEquals(
0
, quizDao.getAllQuestions().size) 
}
The 
@Test
 annotation specifies that this method is a test that you want to execute. 
Here's what is going on:
1. Calls the 
getAllQuestions()
 method of your DAO and store the size of currently 
available questions.
2. Creates a 
Question
 and inserts it in your database.
3. Gets the new amount of questions from the database.
4. Calculates the delta from new and previous amount of questions in the database.
5. Uses 
assert()
 to let the test know you're expecting only one new question in the 
database. If the assertion fails, it means the 
Question
 you created was not stored 
in the database, or it was stored more than once.
6. Clears all the questions from the database after the first assertion.
7. Verifies that there are no questions in the database, after you've cleared it.
Add the following method:
@Test
 
fun
 
testClearQuestions
()
 { 
  
for
 (question 
in
 QuestionInfoProvider.questionList) { 
    quizDao.insert(question) 
  } 
  Assert.assertTrue(quizDao.getAllQuestions().isNotEmpty()) 
  Log.d(
"testData"
, quizDao.getAllQuestions().toString()) 
  quizDao.clearQuestions() 
  Assert.assertTrue(quizDao.getAllQuestions().isEmpty()) 
}
This test simply verifies the correct functionality of your 
clearQuestions()
 method 
by inserting and deleting all the question records in your 
QuestionInfoProvider
. It 
also logs the data saved, so you can see the data does exist within the database.
Finally, add the following method:
@After
 
fun
 
tearDown
()
 { 
  database.close() 
}
Saving Data on Android
Chapter 8: The DAO Pattern
raywenderlich.com
143


Contrary to the 
@Before
 annotation that specifies methods that should be executed 
before any test is run, 
@After
 specifies which methods should be executed after each 
of the tests has concluded. Generally speaking, methods annotated with 
@After
 are 
used to release resources allocated with 
@Before
. In this case, you are closing the 
connection to your database.
When you run an Espresso test, it will install your app on a device or emulator, then 
execute all the code in your tests. Under the 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   148   149   150   151   152   153   154   155   ...   292




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish