-
-
Save rafaeltoledo/7637b24ce95dfc3631746bb7c372e62e to your computer and use it in GitHub Desktop.
Social App (koin)
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
// 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', | |
] | |
... | |
} |
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
// 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" | |
} |
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
package net.rafaeltoledo.social | |
import android.arch.lifecycle.ViewModel | |
class MainViewModel(private val string: String) : ViewModel() { | |
fun getString() = string | |
} |
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
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() | |
}) | |
} | |
} |
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
// FirstModule.kt | |
package net.rafaeltoledo.social.di | |
import org.koin.dsl.module.applicationContext | |
val firstModule = applicationContext { | |
bean { "Social App" } | |
} |
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
// 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()) } | |
} |
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
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 | |
)) | |
} | |
} |
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
// 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 } } | |
)) | |
} | |
} |
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
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