Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Prokash Sarkar prokash-sarkar

🎯
Focusing
View GitHub Profile
View run_blocking_test.kt
class MainViewModel : ViewModel() {
private var isSessionExpired = false
suspend fun checkSessionExpiry(): Boolean {
withContext(Dispatchers.IO) {
delay(5_000) // to simulate a heavy weight operations
isSessionExpired = true
}
return isSessionExpired
View run_blocking.kt
class MainViewModel : ViewModel() {
private var isSessionExpired = false
suspend fun checkSessionExpiry(): Boolean {
withContext(Dispatchers.IO) {
delay(5_000) // to simulate a heavy weight operations
isSessionExpired = true
}
return isSessionExpired
View build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlinVersion = "1.4.21"
ext.jacoco_version = '0.8.6'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.2'
View DefaultDataRepositoryTest.kt
class DefaultDataRepositoryTest {
@Inject
lateinit var dataRepository: DefaultDataRepository
@Inject
lateinit var remoteDataSource: DataSource
@Before
fun setup() {
val component: TestAppComponent = DaggerTestAppComponent.factory()
.create(TestBaseApplication())
View build.gradle
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.example.dagger"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
multiDexEnabled true
View CustomTestRunner.kt
class CustomTestRunner : AndroidJUnitRunner() {
override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
return super.newApplication(cl, TestBaseApplication::class.java.name, context)
}
}
View TestBaseApplication.kt
class TestBaseApplication : BaseApplication() {
override fun initializeDaggerComponent(): AppComponent {
val component: TestAppComponent = DaggerTestAppComponent.factory()
.create(this)
component.inject(this)
return component
}
}
View TestAppComponent.kt
@Singleton
@Component(
modules = [
AndroidInjectionModule::class, NetworkModule::class,
TestAppModule::class, AppModuleBinds::class,
ViewModelBuilderModule::class, ActivityBuildersModule::class
]
)
interface TestAppComponent : AppComponent {
@Component.Factory
View TestAppModule.kt
@Module
class TestAppModule {
@Singleton
@Provides
fun provideTasksRemoteDataSource(
apiService: ApiService
): DataSource {
return mockk()
}
}
View DefaultDataRepositoryTest.kt
class DefaultDataRepositoryTest {
private val remoteDataSource: DataSource = mockk()
private lateinit var dataRepository: DefaultDataRepository
@Before
fun setup() {
dataRepository = DefaultDataRepository(remoteDataSource)
}
@ExperimentalCoroutinesApi