Skip to content

Instantly share code, notes, and snippets.

@jobinlawrance
Created December 13, 2020 20:13
Show Gist options
  • Save jobinlawrance/c086550173cea17dfc9a3f972ace7a43 to your computer and use it in GitHub Desktop.
Save jobinlawrance/c086550173cea17dfc9a3f972ace7a43 to your computer and use it in GitHub Desktop.
Testing using KoinTest
import ...
@RunWith(AndroidJUnit4ClassRunner::class)
class HomeActivityTest: KoinTest {
@get:Rule
var intentRule = IntentsTestRule(HomeActivity::class.java, true, false)
lateinit var mockModule: Module
lateinit var mockedInteractor: HomeInteractor
@Before
override fun setup() {
mockedInteractor = Mockito.mock(HomeInteractor::class.java)
mockedModule = module {
single(override = true) { mockedInteractor }
}
loadKoinModules(mockModule)
}
@After
override fun tearDown() {
unloadKoinModules(mockModule)
}
// Sample test case using the above mocks
@Test
fun testApiFailureDisplaysError() {
// Setting up the mocks
val testRxSubject = BehaviourSubject.create<Result<String>>()
`when`(mockedInteractor.getResultFromApi()).thenReturn(testRxSubject)
// Given: Progress is displayed on Home Page launch
intentRule.launchActivity(null)
onView(withId(R.id.progressBar)).check(matches(isDisplayed()))
// When: Api throws an error
testRxSubject.onError(Exception())
// Then: Progress bar should be removed and error text should be displayed.
onView(withId(R.id.progressBar)).check(matches(not(isDisplayed())))
onView(withId(R.id.failureTextView))
.check(matches(isCompletelyDisplayed()))
.check(matches(withText("Oops, there's some issue, try again later!")))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment