Day49 of #100DaysOfCode

Kushagra Kesav
2 min readMar 27, 2022

--

#CodeTogether Day 49/100

Hii folks 🙌

Today I will be continuing the same pathway in which I will write the test cases to test the lists and Adapters.

Unit 2: Layouts

Pathway 3: Display a scrollable list

Source: https://developer.android.com/courses/android-basics-kotlin/course

Test Lists and Adapters

We will start by downloading the repo from the given link in the course and setting it up in the Android Studio.

After then we will move forward and create a test/java directory and will create a new package for each new directory called com.example.affirmations

After then will create a new class in androidTest -> com.example.affirmations called AffirmationsListTests.kt.

  • Now we will add a test runner to the newly created class.
@RunWith(AndroidJUnit4::class)
  • And will make an activity scenario rule for the main activity.
@get:Rule
val activity = ActivityScenarioRule(MainActivity::class.java)
  • Now we will add a dependency to allow interaction with the RecyclerView in instrumentation tests. The dependencies look something like this:
dependencies {
...
androidTestImplementation
'com.android.support.test.espresso:espresso-contrib:3.0.2'
}
  • After that, we will test the RecyclerView
onView(withId(R.id.recycler_view)).perform(
RecyclerViewActions
.scrollToPosition<RecyclerView.ViewHolder>(9))
)
  • Now we create a new class in test -> com.example.affirmations called AffirmationsAdapterTests.kt. And will add a local test dependency:
dependencies {
...
testImplementation 'org.mockito:mockito-core:3.12.4'
}
  • Now we will test the adapter by creating a function called adapter_size() and annotate it as a test. The goal of this test is to make sure that the size of the adapter is the size of the list that was passed to the adapter.
val data = listOf(
Affirmation(R.string.affirmation1, R.drawable.image1),
Affirmation(R.string.affirmation2, R.drawable.image2)
)
  • Now we will create an instance of the ItemAdapter , passing in the context and data variables created in the previous steps.
val adapter = ItemAdapter(context, data)
  • Recycler view adapters have a method that returns the size of the adapter called getItemCount(). For this app the method looks like this:
/**
* Return the size of our dataset (invoked by the layout manager)
*/

override fun getItemCount() = dataset.size
  • This is the method that should be tested. Make sure that the returned value from this method matches the size of the list we created in step 2. Use the assertEquals() method and compare the values of the list size and the adapter size.
assertEquals("ItemAdapter is not the correct size", data.size, adapter.itemCount)

Today I Learned:

  • How to add testing-specific dependencies.
  • Learned how to interact with a RecyclerView with instrumentation tests.
  • Learned some fundamental best practices for testing.

That is all for Day49 ✅

Thanks for reading, See you tomorrow!

--

--

Kushagra Kesav
Kushagra Kesav

Written by Kushagra Kesav

Developer Relations | Communities | Software Engineering | https://www.linkedin.com/in/kushagrakesav

No responses yet