This file contains 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
abstract class BaseViewModel<screenState: BaseScreenState, action: BaseAction>(initialState: screenState) : ViewModel() { | |
val intent = Channel<action>(Channel.UNLIMITED) | |
protected val _state = MutableStateFlow(initialState) | |
val state: StateFlow<screenState> get() = _state | |
init { | |
viewModelScope.launch { | |
handleIntents() |
This file contains 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
dependencies { | |
... | |
// Instrumentation tests | |
androidTestImplementation "junit:junit:${Versions.junit}" | |
androidTestImplementation "com.google.truth:truth:${Versions.truth}" | |
androidTestImplementation "org.mockito:mockito-core:${Versions.mockito}" | |
androidTestImplementation "org.mockito:mockito-android:${Versions.mockito}" | |
androidTestImplementation "androidx.test:core:${Versions.androidx_test_core}" | |
androidTestImplementation "androidx.test:runner:${Versions.androidx_test_runner}" | |
androidTestImplementation "androidx.test:rules:${Versions.androidx_test_rules}" |
This file contains 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
class CustomTestRunner : AndroidJUnitRunner() { | |
override fun newApplication(cl: ClassLoader?, appName: String?, context: Context?): Application { | |
return super.newApplication(cl, HiltTestApplication::class.java.name, context) | |
} | |
} |
This file contains 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
android { | |
defaultConfig { | |
... | |
testInstrumentationRunner "com.company.package.CustomTestRunner" | |
} | |
} |
This file contains 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
@UninstallModules(BaseUrlModule::class) | |
@HiltAndroidTest | |
@RunWith(AndroidJUnit4::class) | |
class NewsListFragmentTest { | |
@get:Rule val hiltRule = HiltAndroidRule(this) | |
@Module | |
@InstallIn(ApplicationComponent::class) | |
class FakeBaseUrlModule { |
This file contains 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
class FragmentTest { | |
protected lateinit var mockWebServer: MockWebServer | |
@Before | |
fun setUp() { | |
mockWebServer = MockWebServer() | |
mockWebServer.start(8080) | |
} |
This file contains 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
private fun getJsonContent(fileName: String): String { | |
return InputStreamReader(this.javaClass.classLoader!!.getResourceAsStream(fileName)).use { it.readText() } | |
} |
This file contains 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
class MockServerDispatcher { | |
/** | |
* Return ok response from mock server | |
*/ | |
internal inner class RequestDispatcher : Dispatcher() { | |
override fun dispatch(request: RecordedRequest): MockResponse { | |
return when (request.path) { | |
"/fixtures" -> MockResponse().setResponseCode(200).setBody(getJsonContent("fixtures_feed.json")) | |
"/news" -> MockResponse().setResponseCode(200).setBody(getJsonContent("news_response.json")) | |
else -> MockResponse().setResponseCode(400) |
This file contains 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
inline fun <reified T : Fragment> launchFragmentInHiltContainer( | |
fragmentArgs: Bundle? = null, | |
@StyleRes themeResId: Int = R.style.SomeTheme, | |
fragmentFactory: FragmentFactory? = null, | |
crossinline action: Fragment.() -> Unit = {} | |
) { | |
val startActivityIntent = Intent.makeMainActivity( | |
ComponentName( | |
ApplicationProvider.getApplicationContext(), | |
HiltTestActivity::class.java |
This file contains 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
@AndroidEntryPoint | |
class HiltTestActivity : AppCompatActivity() |
OlderNewer