Skip to content

UI testing for BreedListFragment #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .idea/navEditor.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 34 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ apply plugin: 'androidx.navigation.safeargs'
apply plugin: 'kotlin-kapt'

android {
useLibrary 'android.test.runner'

useLibrary 'android.test.base'
useLibrary 'android.test.mock'

compileSdkVersion versions.compileSdkVersion
dataBinding {
enabled = true
Expand Down Expand Up @@ -45,6 +50,34 @@ dependencies {
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:' + versions.coroutines
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:' + versions.coroutinesTest


// Dependencies for local unit tests

testImplementation 'androidx.test:core-ktx:' + versions.androidxCore
testImplementation 'org.robolectric:robolectric:' + versions.roboelectric
testImplementation 'androidx.test.ext:junit-ktx:' + versions. androidXTestExtKotlinRunner


// AndroidX Test - Instrumented testing
androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:' + versions.coroutinesTest
androidTestImplementation 'androidx.test.ext:junit:' + versions. androidXTestExtKotlinRunner
androidTestImplementation 'androidx.test.espresso:espresso-core:' + versions.espressoCore
androidTestImplementation 'androidx.test.espresso:espresso-contrib:' + versions.espressoCore

androidTestImplementation 'junit:junit:' + versions.junit
androidTestImplementation 'org.mockito:mockito-core:' + versions.mockito
androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito:' + versions.dexMaker


// Dependencies for Android instrumented unit tests
// androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:' + versions.coroutines

// Testing code should not be included in the main code.
// Once https://issuetracker.google.com/128612536 is fixed this can be fixed.

implementation ("androidx.fragment:fragment-testing:1.1.0-beta01", {exclude group: 'androidx.test', module: 'core'} )
implementation 'androidx.test:core:' + versions.androidxCore

androidTestImplementation 'androidx.test:runner:' + versions.testRunner
androidTestImplementation 'androidx.test.espresso:espresso-core:' + versions.espressoCore
androidTestImplementation 'androidx.arch.core:core-testing:' + versions.coreTestingVersion
Expand All @@ -69,7 +102,7 @@ dependencies {


// Android X
implementation 'androidx.core:core-ktx:' + versions.androidxCore
implementation 'androidx.core:core-ktx:' + versions.androidXCore
implementation 'androidx.recyclerview:recyclerview:' + versions.androidxRecyclerView
implementation 'androidx.browser:browser:' + versions.androidxBrowser

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.cottacush.android.androidbaseprojectkt.sample.catlist

import android.os.Bundle
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.recyclerview.widget.RecyclerView
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.filters.MediumTest
import com.cottacush.android.androidbaseprojectkt.R
import com.cottacush.android.androidbaseprojectkt.sample.breeddetails.BreedDetailsFragment
import com.cottacush.android.androidbaseprojectkt.sample.models.BreedDatabase
import com.cottacush.android.androidbaseprojectkt.sample.models.DatabaseBreedModel
import com.cottacush.android.androidbaseprojectkt.sample.models.asDomainModel
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import org.junit.After
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.InjectMocks
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify


@MediumTest
@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class BreedListFragmentTest {

private val database = Room
.inMemoryDatabaseBuilder(
ApplicationProvider.getApplicationContext(),
BreedDatabase::class.java
).build()
private val dataBaseBreed = mock(DatabaseBreedModel::class.java)
private val list = listOf(dataBaseBreed)
private val model = list.asDomainModel()[0]

@Test
fun clickBreed_navigateToDetailFragment() {
runBlockingTest {
database.breedDao.insertAllBreed(dataBaseBreed)
}
val navController = mock(NavController::class.java)

val scenerio = launchFragmentInContainer<BreedDetailsFragment>(Bundle(), R.style.AppTheme)

scenerio.onFragment {
Navigation.setViewNavController(it.view!!, navController)
}
onView(withId(R.id.breedsRecyclerView))
.perform(
RecyclerViewActions.actionOnItem<RecyclerView.ViewHolder>(
hasDescendant(
withText(model.description)
), click()
)
)

verify(navController).navigate(
BreedListFragmentDirections.actionCatsListFragmentToBreedDetailsFragment(
model
)
)
}

@After
fun tearDown() {
database?.run {
clearAllTables()
close()
}
}
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.INTERNET"/>

<application

android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -21,6 +22,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="android.test.runner" />
</application>

</manifest>
13 changes: 9 additions & 4 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ ext.versions = [
//tests
junit : '4.12',
testRunner : '1.2.0',
espressoCore : '3.2.0',
espressoCore : '3.2.0-beta01',
coreTestingVersion : '2.0.0',
coroutinesCore : '1.2.1',
coroutinesCore : '1.3.2',
coroutinesTest : '1.3.2',
dexMaker : '2.12.1',
fragment : '1.1.0-alpha07',
androidxCore : '1.2.0',
roboelectric : '4.3.1',
androidXTestExtKotlinRunner :'1.1.1',

//mockito
mockitoVersion : "2.25.0",
Expand All @@ -24,11 +29,11 @@ ext.versions = [
spotlessPlugin : '3.23.1',

// Kotlin
kotlin : '1.3.40',
kotlin : '1.3.50',
coroutines : '1.2.1',

// AndroidX
androidxCore : '1.0.1',
androidXCore : '1.0.1',
androidxRecyclerView: '1.0.0',
androidxMaterial : '1.0.0',
androidxBrowser : '1.0.0',
Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ android.useAndroidX=true
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official

kapt.incremental.apt=true