Skip to content

Instantly share code, notes, and snippets.

@rafaeltoledo
Last active November 12, 2019 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaeltoledo/7637b24ce95dfc3631746bb7c372e62e to your computer and use it in GitHub Desktop.
Save rafaeltoledo/7637b24ce95dfc3631746bb7c372e62e to your computer and use it in GitHub Desktop.
Social App (koin)
// build.gradle
buildscript {
ext.versions = [
'kotlin': '1.2.41',
'supportLibrary': '27.1.1',
'jacoco': '0.8.1',
'archComponents': '1.1.1',
'koin': '0.9.3',
]
...
}
// app/build.gradle
apply plugin: 'com.android.application'
apply plugin: 'org.jetbrains.kotlin.android'
apply plugin: 'org.jetbrains.kotlin.kapt'
...
dependencies {
...
implementation "android.arch.lifecycle:extensions:$versions.archComponents"
kapt "android.arch.lifecycle:compiler:$versions.archComponents"
implementation "org.koin:koin-android:$versions.koin"
implementation "org.koin:koin-android-architecture:$versions.koin"
}
package net.rafaeltoledo.social
import android.arch.lifecycle.ViewModel
class MainViewModel(private val string: String) : ViewModel() {
fun getString() = string
}
package net.rafaeltoledo.social
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.TextView
import org.koin.android.architecture.ext.viewModel
class MainActivity : AppCompatActivity() {
private val mainViewModel: MainViewModel by viewModel()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(TextView(this).apply {
id = R.id.content
text = mainViewModel.getString()
})
}
}
// FirstModule.kt
package net.rafaeltoledo.social.di
import org.koin.dsl.module.applicationContext
val firstModule = applicationContext {
bean { "Social App" }
}
// ViewModelModule.kt
package net.rafaeltoledo.social.di
import net.rafaeltoledo.social.MainViewModel
import org.koin.android.architecture.ext.viewModel
import org.koin.dsl.module.applicationContext
val viewModelModule = applicationContext {
viewModel { MainViewModel(get()) }
}
package net.rafaeltoledo.social
import android.app.Application
import net.rafaeltoledo.social.di.firstModule
import net.rafaeltoledo.social.di.viewModelModule
import org.koin.android.ext.android.startKoin
class SocialApp : Application() {
override fun onCreate() {
super.onCreate()
startKoin(listOf(
viewModelModule,
firstModule
))
}
}
// TestSetup.kt
package net.rafaeltoledo.social
import android.app.Application
import net.rafaeltoledo.social.di.viewModelModule
import org.koin.android.ext.android.startKoin
import org.koin.dsl.module.applicationContext
class TestSocialApp : SocialApp() {
fun overrideStringValue(newValue: String) {
// Override value
// This behavior should be explicit in a future version of Koin
// See: https://github.com/Ekito/koin/pull/123
loadKoinModules(listOf(
applicationContext { bean { newValue } }
))
}
}
package net.rafaeltoledo.social
import android.widget.TextView
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
@RunWith(RobolectricTestRunner::class)
class MainActivityTest {
@Test
fun checkIfActivityIsSuccessfullyCreated() {
// Arrange
val newValue = "Test Social App"
val app = RuntimeEnvironment.application as TestSocialApp
app.overrideStringValue(newValue)
val activity = Robolectric.setupActivity(MainActivity::class.java)
// Act - nothing to do
// Assert
val text = activity.findViewById(R.id.content)
assertThat(text.text).isEqualTo(newValue)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment