Skip to content

Instantly share code, notes, and snippets.

Avatar
💪
Attitute beats talent

Fernando Prieto Moyano ferPrieto

💪
Attitute beats talent
View GitHub Profile
View LaunchesScreenRobot.kt
internal fun launchesScreenRobot(
composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<EntryPointActivity>, EntryPointActivity>,
func: LaunchesScreenRobot.() -> Unit
) = LaunchesScreenRobot(composeTestRule).also { func }
internal open class LaunchesScreenRobot constructor(
private val composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<EntryPointActivity>, EntryPointActivity>
) {
// Robot definitions and functions
}
@ferPrieto
ferPrieto / LaunchesScreenKtTest.kt
Created Jan 13, 2022
For this demonstration, the dispatcher initialisation and content setup (composeTestRule.setContent) was omitted for simplicity
View LaunchesScreenKtTest.kt
@Test
fun elementsVisibilityAfterOpeningTheScreen() {
launchesScreenRobot(composeTestRule) {
clickOnLaunchesTab()
initialElementsShowed()
}
}
@Test
fun elementsVisibilityAfterOpeningTheScreen() {
@ferPrieto
ferPrieto / LaunchesScreenKtTest.kt
Last active Jan 5, 2022
This is the simplified test using MockWebServer
View LaunchesScreenKtTest.kt
@ExperimentalMaterialApi
@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class LaunchesScreenKtTest : BaseScreenTest() {
@Test
@InternalCoroutinesApi
fun visibleItemsCountAfterOpeningTheScreen() {
mockWebServer.dispatcher = SuccessDispatcher()
setMainContent()
@ferPrieto
ferPrieto / BaseTest.kt
Created Jan 4, 2022
These are the minimum initialisation needed in order to use MockWebServer in your tests
View BaseTest.kt
val mockWebServer by lazy { MockWebServer() }
@Before
fun setUp() {
mockWebServer.start(BuildConfig.PORT)
}
@After
fun teardown() {
mockWebServer.shutdown()
@ferPrieto
ferPrieto / NetworkModule.kt
Created Jan 4, 2022
This is the simplified version of the NetworkModule, which will be replaced by the FakeNetworkModule in the Tests
View NetworkModule.kt
@InstallIn(SingletonComponent::class)
@Module
open class NetworkModule {
open fun getBaseUrl () ="https://api.spacexdata.com/v3/"
@Provides
@BaseUrl
fun provideBaseUrl() = getBaseUrl ()
@ferPrieto
ferPrieto / FakeNetworkModule.kt
Created Jan 4, 2022
This is the FakeNetworkModule class used for testing purpose that replaces the original one (NetworkModule) defined in production
View FakeNetworkModule.kt
@Module
@TestInstallIn(
components = [SingletonComponent::class],
replaces = [NetworkModule::class]
)
class FakeNetworkModule : NetworkModule() {
override fun getBaseUrl() = "http://127.0.0.1:${BuildConfig.PORT}"
}
@ferPrieto
ferPrieto / LaunchesScreenTest.kt
Last active Jan 5, 2022
This is a simplified demonstration of a UI Test in Isolation where only one of the tabs is initialised with the desired use case
View LaunchesScreenTest.kt
@Test
@InternalCoroutinesApi
fun elementsVisibilityAfterTwoItemsRetrieved() {
composeTestRule.apply {
setContent {
SpaceXTheme {
LaunchesScreen(
state = LaunchesContract.State(
listOf(
LaunchUiModel(
View MockTestRunner.kt
class MockTestRunner : AndroidJUnitRunner() {
override fun onCreate(arguments: Bundle?) {
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().permitAll().build())
super.onCreate(arguments)
}
override fun newApplication(
cl: ClassLoader?,
className: String?,
context: Context?
View build.gradle.kts
android {
defaultConfig {
applicationId = "prieto.fernando.sample"
testInstrumentationRunner = "prieto.fernando.sample.webmock.MockTestRunner"
}
buildFeatures {
compose = true
viewBinding = true
}
// rest of configuration
@ferPrieto
ferPrieto / MainScreenTest.kt
Created Jan 4, 2022
This is an example of how to identify a node by Semantics: ContentDescription
View MainScreenTest.kt
private val dialogFilterButton by lazy {
composeTestRule.onNodeWithContentDescription("Filter Button")
}
@Test
fun elementsVisibilityAfterOpeningTheMainScreen() {
setMainContent()
dialogFilterButton.assertIsDisplayed()
}