Skip to content

Instantly share code, notes, and snippets.

@BenHenning
Created September 13, 2019 07:49
Show Gist options
  • Save BenHenning/4bed29efd4eaae3d7445c899798aaa37 to your computer and use it in GitHub Desktop.
Save BenHenning/4bed29efd4eaae3d7445c899798aaa37 to your computer and use it in GitHub Desktop.
class InMemoryBlockingCacheTest {
@ExperimentalCoroutinesApi
@Inject
@field:TestDispatcher
lateinit var testDispatcher: TestCoroutineDispatcher
@ExperimentalCoroutinesApi
private val backgroundTestCoroutineScope by lazy {
CoroutineScope(backgroundTestCoroutineDispatcher)
}
@ExperimentalCoroutinesApi
private val backgroundTestCoroutineDispatcher by lazy {
TestCoroutineDispatcher()
}
@Before
@ExperimentalCoroutinesApi
fun setUp() {
setUpTestApplicationComponent()
}
@Test
@ExperimentalCoroutinesApi
fun testReadCache_withoutInitialValue_providesNull() = runBlockingTest(testDispatcher) {
// Test with testDispatcher since it's connected to the blocking dispatcher.
}
private fun setUpTestApplicationComponent() {
DaggerInMemoryBlockingCacheTest_TestApplicationComponent.builder()
.setApplication(ApplicationProvider.getApplicationContext())
.build()
.inject(this)
}
@Qualifier annotation class TestDispatcher
@Module
class TestModule {
@ExperimentalCoroutinesApi
@Singleton
@Provides
@TestDispatcher
fun provideTestDispatcher(): TestCoroutineDispatcher {
return TestCoroutineDispatcher()
}
@ExperimentalCoroutinesApi
@Singleton
@Provides
@BlockingDispatcher
fun provideBlockingDispatcher(@TestDispatcher testDispatcher: TestCoroutineDispatcher): CoroutineDispatcher {
return testDispatcher
}
}
@Singleton
@Component(modules = [TestModule::class])
interface TestApplicationComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun setApplication(application: Application): Builder
fun build(): TestApplicationComponent
}
fun inject(inMemoryBlockingCacheTest: InMemoryBlockingCacheTest)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment