Last active
September 5, 2023 06:24
-
-
Save warting/2faac778cc125f5bedf49cda413dd0fe to your computer and use it in GitHub Desktop.
Conditional Hilt application test runner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package se.warting.android | |
import android.app.Application | |
import android.content.Context | |
import androidx.test.runner.AndroidJUnitRunner | |
import dagger.hilt.android.testing.HiltAndroidTest | |
import dagger.hilt.android.testing.HiltTestApplication | |
class ConditionalHiltApplicationTestRunner : AndroidJUnitRunner() { | |
override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application { | |
val isHiltTest = name?.let { testName -> | |
try { | |
val testClass = Class.forName(testName) | |
testClass.isAnnotationPresent(HiltAndroidTest::class.java) | |
} catch (e: ClassNotFoundException) { | |
false | |
} | |
} ?: false | |
return if (isHiltTest) { | |
super.newApplication( | |
cl, | |
HiltTestApplication::class.java.name, // HiltTestApplication_Application::class.java.name, | |
context | |
) | |
} else { | |
super.newApplication(cl, name, context) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment