Skip to content

Instantly share code, notes, and snippets.

View cdmunoz's full-sized avatar

Carlos Daniel cdmunoz

View GitHub Profile
@cdmunoz
cdmunoz / MockWebServerBaseTest.kt
Created June 15, 2020 20:25
Abstract class to control mock server lifecycle and to define all logic related to mock calls and test api service
abstract class MockWebServerBaseTest {
private lateinit var mockServer: MockWebServer
@Before
open fun setUp() {
this.configureMockServer()
}
@After
@ExperimentalCoroutinesApi
@RunWith(MockitoJUnitRunner::class)
class PhotosViewModelTest {
@get:Rule
val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule()
@get:Rule
val testCoroutineRule = TestCoroutineRule()
private lateinit var viewModel: PhotosViewModel
@Mock
@ExperimentalCoroutinesApi
class TestCoroutineRule : TestRule {
private val testCoroutineDispatcher = TestCoroutineDispatcher()
private val testCoroutineScope = TestCoroutineScope(testCoroutineDispatcher)
override fun apply(base: Statement?, description: Description?) = object : Statement() {
@Throws(Throwable::class)
override fun evaluate() {
Dispatchers.setMain(testCoroutineDispatcher)
@cdmunoz
cdmunoz / build.gradle
Created June 15, 2020 20:09
Adding testing dependencies
def mockitoCoreVersion = "3.3.3"
testImplementation "org.mockito:mockito-core:$mockitoCoreVersion"
def archCoreTest = "2.1.0"
testImplementation "androidx.arch.core:core-testing:$archCoreTest"
def coroutinesTest = "1.3.4"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesTest"
def mockWebserverVersion = "4.7.2"
testImplementation "com.squareup.okhttp3:mockwebserver:$mockWebserverVersion"
@cdmunoz
cdmunoz / HomeFragment.kt
Last active May 17, 2020 22:06
Calling EspressoIdlingResource increment and decrement to from prod code to let Espresso know while runnig UI tests when idling resources are available to continue its execution
//HomeFragment logic and methods
//Calls increment before the query to API
private fun initViewModels() {
if (null == photosViewModel) {
//creates viewModel
//.....
EspressoIdlingResource.increment()
photosViewModel?.loadData()
}
}
@cdmunoz
cdmunoz / EspressoIdlingResource.kt
Created May 17, 2020 22:00
Place this class within main package structure, not androidTest packages
package co.cdmunoz.nasaroverphotos.utils.test
import androidx.test.espresso.idling.CountingIdlingResource
object EspressoIdlingResource {
private const val RESOURCE = "GLOBAL"
@JvmField
val countingIdlingResource = CountingIdlingResource(RESOURCE)
@cdmunoz
cdmunoz / HomeFragmentUITest.kt
Created May 17, 2020 21:57
Espresso IdleResource UI Test
@LargeTest
@RunWith(AndroidJUnit4::class)
class HomeFragmentUITest {
@get:Rule
val activityTestRule = ActivityScenarioRule(MainActivity::class.java)
@Before
fun setUp() {
IdlingRegistry.getInstance().register(EspressoIdlingResource.countingIdlingResource)
@RunWith(AndroidJUnit4::class)
class OnBoardingActivityUITest {
@get:Rule
val activityScenarioRule = activityScenarioRule<OnBoardingActivity>()
@Test
fun initial_state_on_boarding_screen_UI_test() {
val titleToCheck = "On Boarding Title 1"
onView(withText(titleToCheck)).check(matches(isDisplayed()))
onView(withId(R.id.on_board_bottom_msg)).check(matches(isDisplayed()))
@cdmunoz
cdmunoz / build.gradle
Created April 23, 2020 20:15
ActivityScenarioRule to UI testing
def junitExtVersion = "1.1.1"
androidTestImplementation "androidx.test.ext:junit:$junitExtVersion"
androidTestImplementation "androidx.test.ext:junit-ktx:$junitExtVersion"
class OnBoardingActivity : AppCompatActivity() {
private var onBoardingPageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
updateCircleMarker(binding, position)
}
}
private lateinit var binding: ActivityOnBoardingBinding
override fun onCreate(savedInstanceState: Bundle?) {