Skip to content

Instantly share code, notes, and snippets.

@gigiperih
Last active December 28, 2021 06:23
Show Gist options
  • Save gigiperih/60f87a8b939e2e9ac198aa67920ae289 to your computer and use it in GitHub Desktop.
Save gigiperih/60f87a8b939e2e9ac198aa67920ae289 to your computer and use it in GitHub Desktop.
(Android Instrumentation Test) Kotlin extensions for launching hilt fragment
android {
...
defaultConfig {
...
testInstrumentationRunner "com.pkg.HiltAppTestRunner"
}
...
}
dependencies {
...
testImplementation testDeps.values()
androidTestImplementation androidTestDeps.hilt
androidTestImplementation androidTestDeps.androidx.values()
androidTestImplementation androidTestDeps.server
androidTestImplementation androidTestDeps.coroutines
kaptAndroidTest kaptAndroidTestDeps.hilt
debugImplementation debugDeps.fragment
}
ext {
deps = [
androidx : [
core : "androidx.core:core-ktx:1.7.0",
constraint: "androidx.constraintlayout:constraintlayout:2.1.2",
rv : "androidx.recyclerview:recyclerview:1.2.1",
lifecycle : "androidx.lifecycle:lifecycle-runtime-ktx:2.4.0",
appcompat : "androidx.appcompat:appcompat:1.4.0",
hilt : "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
],
material : "com.google.android.material:material:1.4.0",
fragment : "androidx.fragment:fragment-ktx:1.4.0",
hilt : "com.google.dagger:hilt-android:2.38.1",
coroutines : [
core : "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0",
android: "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0"
],
networking : [
retrofit : "com.squareup.retrofit2:retrofit:2.9.0",
gson : "com.squareup.retrofit2:converter-gson:2.9.0",
interceptor: "com.squareup.okhttp3:logging-interceptor:4.9.1"
],
nav : [
fragment: "androidx.navigation:navigation-fragment-ktx:2.3.5",
ui : "androidx.navigation:navigation-ui-ktx:2.3.5"
],
swipeRefresh: "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0",
glide : "com.github.bumptech.glide:glide:4.12.0",
shimmer : "com.facebook.shimmer:shimmer:0.5.0"
]
compiler = [
hilt : [
hilt : "androidx.hilt:hilt-compiler:1.0.0",
dagger: "com.google.dagger:hilt-android-compiler:2.38.1"
],
glide: "com.github.bumptech.glide:compiler:4.12.0"
]
testDeps = [
junit : "junit:junit:4.13.2",
mockk : "io.mockk:mockk:1.12.1",
mockkJvm : "io.mockk:mockk-agent-jvm:1.12.1",
coroutines: "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0",
core : "androidx.arch.core:core-testing:2.1.0"
]
androidTestDeps = [
hilt : "com.google.dagger:hilt-android-testing:2.38.1",
androidx : [
junit : "androidx.test.ext:junit:1.1.3",
espresso: "androidx.test.espresso:espresso-core:3.4.0",
intents : "androidx.test.espresso:espresso-intents:3.4.0",
contrib : "androidx.test.espresso:espresso-contrib:3.4.0",
nav : "androidx.navigation:navigation-testing:2.3.5"
],
server : "com.squareup.okhttp3:mockwebserver:4.9.3",
coroutines: "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0"
]
kaptAndroidTestDeps = [
hilt: "com.google.dagger:hilt-android-compiler:2.38.1"
]
debugDeps = [
fragment: "androidx.fragment:fragment-testing:1.4.0"
]
}
inline fun <reified T : Fragment> launchFragmentInHiltContainer(
fragmentArgs: Bundle? = null,
@StyleRes themeResId: Int = R.style.Theme_GithubRepo,
fragmentFactory: FragmentFactory? = null,
crossinline action: Fragment.() -> Unit = {}
) {
val startActivityIntent = Intent.makeMainActivity(
ComponentName(
ApplicationProvider.getApplicationContext(),
HiltTestActivity::class.java
)
).putExtra(
"androidx.fragment.app.testing.FragmentScenario.EmptyFragmentActivity.THEME_EXTRAS_BUNDLE_KEY",
themeResId
)
ActivityScenario.launch<HiltTestActivity>(startActivityIntent).onActivity { activity ->
fragmentFactory?.let {
activity.supportFragmentManager.fragmentFactory = it
}
val fragment: Fragment = activity.supportFragmentManager.fragmentFactory.instantiate(
Preconditions.checkNotNull(T::class.java.classLoader),
T::class.java.name
)
fragment.arguments = fragmentArgs
activity.supportFragmentManager
.beginTransaction()
.add(android.R.id.content, fragment, "")
.commitNow()
fragment.action()
}
}
class HiltAppTestRunner : AndroidJUnitRunner() {
override fun newApplication(
cl: ClassLoader?,
className: String?,
context: Context?
): Application {
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
override fun onCreate(arguments: Bundle?) {
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().permitAll().build())
super.onCreate(arguments)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment