-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #287 from wealthfront/more-sample-tests
Basic unit tests for all sample-migration screens
- Loading branch information
Showing
18 changed files
with
252 additions
and
23 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
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
33 changes: 33 additions & 0 deletions
33
...ample-migration/src/test/java/com/wealthfront/magellan/sample/migration/TestExtensions.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
@file:Suppress("UNCHECKED_CAST") | ||
|
||
package com.wealthfront.magellan.sample.migration | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.runBlocking | ||
import org.mockito.Mockito.verify | ||
import org.mockito.Mockito.`when` | ||
import org.mockito.stubbing.LenientStubber | ||
import org.mockito.stubbing.OngoingStubbing | ||
import org.mockito.verification.VerificationMode | ||
|
||
fun <T> LenientStubber.coWhen(block: suspend CoroutineScope.() -> T): OngoingStubbing<T> = | ||
runBlocking { | ||
this@coWhen.`when`(block()) | ||
} | ||
|
||
fun <T> coWhen(block: suspend CoroutineScope.() -> T): OngoingStubbing<T> = | ||
runBlocking { | ||
`when`(block()) | ||
} | ||
|
||
fun <T> coVerify(mock: T, block: suspend CoroutineScope.(T) -> Unit) { | ||
runBlocking { | ||
block(verify(mock)) | ||
} | ||
} | ||
|
||
fun <T> coVerify(mock: T, mode: VerificationMode, block: suspend CoroutineScope.(T) -> Unit) { | ||
runBlocking { | ||
block(verify(mock, mode)) | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...gration/src/test/java/com/wealthfront/magellan/sample/migration/tide/DogBreedsStepTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.wealthfront.magellan.sample.migration.tide | ||
|
||
import android.app.Activity | ||
import android.app.Application | ||
import android.os.Looper | ||
import androidx.test.core.app.ApplicationProvider.getApplicationContext | ||
import com.google.common.truth.Truth.assertThat | ||
import com.wealthfront.magellan.lifecycle.LifecycleState | ||
import com.wealthfront.magellan.lifecycle.transitionToState | ||
import com.wealthfront.magellan.sample.migration.AppComponentContainer | ||
import com.wealthfront.magellan.sample.migration.TestAppComponent | ||
import com.wealthfront.magellan.sample.migration.api.DogApi | ||
import com.wealthfront.magellan.sample.migration.api.DogBreeds | ||
import com.wealthfront.magellan.sample.migration.coWhen | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.junit.MockitoJUnit | ||
import org.mockito.junit.MockitoRule | ||
import org.mockito.quality.Strictness | ||
import org.robolectric.Robolectric | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.Shadows.shadowOf | ||
import javax.inject.Inject | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class DogBreedsStepTest { | ||
|
||
private val dogBreedsStep = DogBreedsStep() | ||
private val activity = Robolectric.buildActivity(Activity::class.java).get() | ||
|
||
@Inject lateinit var api: DogApi | ||
|
||
@Rule @JvmField | ||
val mockitoRule: MockitoRule = MockitoJUnit.rule().strictness(Strictness.WARN) | ||
|
||
@Before | ||
fun setup() { | ||
val context = getApplicationContext<Application>() | ||
((context as AppComponentContainer).injector() as TestAppComponent).inject(this) | ||
|
||
val breedData = DogBreeds( | ||
message = listOf("chesapeake", "curly", "flatcoated", "golden"), | ||
status = "success" | ||
) | ||
coWhen { api.getListOfAllBreedsOfRetriever() }.thenReturn(breedData) | ||
} | ||
|
||
@Test | ||
fun fetchesDogBreedsOnShow() { | ||
dogBreedsStep.transitionToState(LifecycleState.Shown(activity)) | ||
shadowOf(Looper.getMainLooper()).idle() | ||
assertThat(dogBreedsStep.viewBinding!!.dogBreeds.adapter).isNotNull() | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...tion/src/test/java/com/wealthfront/magellan/sample/migration/tide/DogDetailsScreenTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.wealthfront.magellan.sample.migration.tide | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import android.os.Looper.getMainLooper | ||
import androidx.activity.ComponentActivity | ||
import androidx.test.core.app.ApplicationProvider | ||
import com.wealthfront.magellan.lifecycle.LifecycleState | ||
import com.wealthfront.magellan.lifecycle.transitionToState | ||
import com.wealthfront.magellan.sample.migration.AppComponentContainer | ||
import com.wealthfront.magellan.sample.migration.TestAppComponent | ||
import com.wealthfront.magellan.sample.migration.api.DogApi | ||
import com.wealthfront.magellan.sample.migration.api.DogMessage | ||
import io.reactivex.Observable | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mock | ||
import org.mockito.Mockito.verify | ||
import org.mockito.Mockito.`when` | ||
import org.mockito.junit.MockitoJUnit | ||
import org.mockito.junit.MockitoRule | ||
import org.mockito.quality.Strictness | ||
import org.robolectric.Robolectric.buildActivity | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.Shadows.shadowOf | ||
import javax.inject.Inject | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class DogDetailsScreenTest { | ||
private lateinit var screen: DogDetailsScreen | ||
private val activity = buildActivity(ComponentActivity::class.java).get() | ||
private val breedData = DogMessage( | ||
message = "image-url", | ||
status = "success" | ||
) | ||
|
||
@Inject lateinit var api: DogApi | ||
@Mock lateinit var dogDetailsView: DogDetailsView | ||
|
||
@Rule @JvmField | ||
val mockitoRule: MockitoRule = MockitoJUnit.rule().strictness(Strictness.WARN) | ||
|
||
@Before | ||
fun setup() { | ||
val context = ApplicationProvider.getApplicationContext<Application>() | ||
((context as AppComponentContainer).injector() as TestAppComponent).inject(this) | ||
|
||
screen = object : DogDetailsScreen("robotic") { | ||
override fun createView(context: Context): DogDetailsView { | ||
super.createView(context) | ||
return dogDetailsView | ||
} | ||
} | ||
|
||
`when`(api.getRandomImageForBreed("robotic")).thenReturn(Observable.just(breedData)) | ||
} | ||
|
||
@Test | ||
fun fetchesDogBreedOnShow() { | ||
screen.transitionToState(LifecycleState.Shown(activity)) | ||
shadowOf(getMainLooper()).idle() | ||
verify(dogDetailsView).setDogPic("image-url") | ||
} | ||
} |
Oops, something went wrong.