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
val mockServer = MockWebServer() | |
mockServer.start() | |
val mockedResponse = MockResponse() | |
mockedResponse.setResponseCode(200) | |
mockedResponse.setBody("{}") // sample JSON | |
mockServer.enqueue(mockedReponse) |
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
@RunWith(JUnit4::class) | |
class BlogRepositoryUTest { | |
lateinit var blogRepository : BlogRepository | |
lateinit var mockServer : MockWebServer | |
lateinit var blogService : BlogService | |
@Before @Throws fun setUp() { | |
// Initialize mock webserver | |
mockServer = MockWebServer() |
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
dependencies { | |
testImplementation 'junit:junit:4.12' | |
testImplementation 'org.mockito:mockito-core:2.23.4' | |
testImplementation 'org.mockito:mockito-android:2.22.0' | |
} |
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
@RunWith(AndroidJUnit4::class) | |
class MainActivityTest { | |
@get:Rule | |
val rule = ActivityTestRule(MainActivity::class.java) | |
@Test | |
fun testAssertHelloText() { | |
onView(withId(R.id.hello)).check(matches(withText("Hello World!"))) | |
} | |
} |
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
android | |
{ | |
testOptions { | |
unitTests { | |
includeAndroidResources = true | |
} | |
} | |
} | |
dependency |
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
inline fun <T> dependantLiveData( | |
vararg dependencies: LiveData<*>, | |
crossinline mapper: () -> T? | |
) = MediatorLiveData<T>().also { mediatorLiveData -> | |
val observer = Observer<Any> { mediatorLiveData.value = mapper() } | |
dependencies.forEach { dependencyLiveData -> | |
mediatorLiveData.addSource(dependencyLiveData, observer) | |
} | |
} |
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
open class RxAwareViewModel : ViewModel() { | |
var disposables = CompositeDisposable() | |
override fun onCleared() { | |
super.onCleared() | |
if (!disposables.isDisposed) { | |
disposables.dispose() | |
} | |
} |
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
fun <T : ViewDataBinding> Activity.bind(layoutId: Int): T { | |
return DataBindingUtil.setContentView(this, layoutId) | |
} |
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
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
loginViewModel = ViewModelProviders.of(this@LoginActivity, viewModelFactory) | |
.get(LoginViewModel::class.java) | |
val binding = bind<ActivityLoginLayoutBinding>(R.layout.activity_login_layout) | |
binding.lifecycleOwner = this | |
binding.viewmodel = loginViewModel | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<layout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto"> | |
<data> | |
<import type="android.text.TextUtils" /> | |
<variable | |
name="viewmodel" |