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
class UserListPresenterTest { | |
private val immediateScheduler = object : Scheduler() { | |
override fun createWorker(): Worker { | |
return ExecutorScheduler.ExecutorWorker(Executor { it.run() }) | |
} | |
} | |
... | |
@Before |
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
class ImmediateSchedulerRule : TestRule { | |
private val immediate = object : Scheduler() { | |
override fun createWorker(): Worker { | |
return ExecutorScheduler.ExecutorWorker(Executor { it.run() }) | |
} | |
} | |
override fun apply(base: Statement, d: Description): Statement { | |
return object : Statement() { | |
@Throws(Throwable::class) |
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
@Test | |
fun testOnScrollChanged_offsetReachedAndLoading_dontRequestNextPage() { | |
// Given | |
val users = listOf(UserViewModel(1, "Name", 1000, "")) | |
val single: Single<List<UserViewModel>> = Single.create { | |
emitter -> | |
emitter.onSuccess(users) | |
} | |
val delayedSingle = single.delay(2, TimeUnit.SECONDS, testScheduler) |
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
interface SchedulerProvider { | |
fun uiScheduler() : Scheduler | |
fun ioScheduler() : Scheduler | |
} | |
class AppSchedulerProvider : SchedulerProvider { | |
override fun ioScheduler() = Schedulers.io() | |
override fun uiScheduler(): Scheduler = AndroidSchedulers.mainThread() | |
} |
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
class UserListPresenterTest { | |
@Mock | |
lateinit var mockGetUsers: GetUsers | |
@Mock | |
lateinit var mockView: UserListView | |
lateinit var userListPresenter: UserListPresenter |
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
class FakeUserRepository : UserRepository { | |
override fun getUsers(page: Int, forced: Boolean): Single<UserListModel> { | |
val users = (1..10L).map { | |
val number = (page - 1) * 10 + it | |
User(it, "User $number", number * 100, "") | |
} | |
return Single.create<UserListModel> { emitter: SingleEmitter<UserListModel> -> | |
val userListModel = UserListModel(users) |
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
class CustomApplication : Application() { | |
lateinit var component: ApplicationComponent | |
override fun onCreate() { | |
super.onCreate() | |
initAppComponent() | |
Stetho.initializeWithDefaults(this); |
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
@Module | |
class FakeApplicationModule { | |
@Provides | |
@Singleton | |
fun provideUserRepository() : UserRepository { | |
return FakeUserRepository() | |
} | |
@Provides |
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
@Singleton | |
@Component(modules = arrayOf(FakeApplicationModule::class)) | |
interface FakeApplicationComponent : ApplicationComponent |
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 { | |
@Rule @JvmField | |
var activityRule = ActivityTestRule(MainActivity::class.java, true, false) | |
@Before | |
fun setUp() { | |
val instrumentation = InstrumentationRegistry.getInstrumentation() | |
val app = instrumentation.targetContext.applicationContext as CustomApplication |