Skip to content

Instantly share code, notes, and snippets.

View ashley-figueira's full-sized avatar
🇵🇹

Ashley Figueira ashley-figueira

🇵🇹
View GitHub Profile
class FragmentTest {
protected lateinit var mockWebServer: MockWebServer
@Before
fun setUp() {
mockWebServer = MockWebServer()
mockWebServer.start(8080)
}
@UninstallModules(BaseUrlModule::class)
@HiltAndroidTest
@RunWith(AndroidJUnit4::class)
class NewsListFragmentTest {
@get:Rule val hiltRule = HiltAndroidRule(this)
@Module
@InstallIn(ApplicationComponent::class)
class FakeBaseUrlModule {
android {
defaultConfig {
...
testInstrumentationRunner "com.company.package.CustomTestRunner"
}
}
@ashley-figueira
ashley-figueira / CustomTestRunner.kt
Created November 30, 2020 16:29
Custom Test Runner
class CustomTestRunner : AndroidJUnitRunner() {
override fun newApplication(cl: ClassLoader?, appName: String?, context: Context?): Application {
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
}
@ashley-figueira
ashley-figueira / build.gradle
Last active November 30, 2020 16:18
Dependencies for Intrumentation Tests
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}"
@ashley-figueira
ashley-figueira / BaseViewModel.kt
Created August 12, 2020 13:58
A BaseViewModel includes a channel which receives intents and a stateFlow which handles the screen states.
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()