Skip to content

Instantly share code, notes, and snippets.

@Sal7one
Created December 6, 2024 00:33
Show Gist options
  • Save Sal7one/aefa6d28e561e907668f231b3be74da1 to your computer and use it in GitHub Desktop.
Save Sal7one/aefa6d28e561e907668f231b3be74da1 to your computer and use it in GitHub Desktop.
Roboelctric compose example
package com.sal7one.testingrobo
import android.app.Application
import android.content.pm.ActivityInfo
import androidx.activity.ComponentActivity
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.core.app.ApplicationProvider
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestWatcher
import org.junit.runner.Description
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(manifest = Config.NONE)
class MainActivityTest {
@get:Rule(order = 1)
val addActivityToRobolectricRule = object : TestWatcher() {
override fun starting(description: Description?) {
super.starting(description)
val appContext: Application = ApplicationProvider.getApplicationContext()
val activityInfo = ActivityInfo().apply {
name = ComponentActivity::class.java.name
packageName = appContext.packageName
}
shadowOf(appContext.packageManager).addOrUpdateActivity(activityInfo)
}
}
@get:Rule(order = 2)
val composeTestRule = createComposeRule()
@Test
fun testComposeWithRobolectric() {
composeTestRule.setContent {
Greeting("")
}
composeTestRule.onNodeWithTag("ClickableButton").performClick()
composeTestRule.onNodeWithTag("ClickableButton").performClick()
composeTestRule.onNodeWithText("Clicked 2 times").assertExists()
composeTestRule.onNodeWithTag("ClickableButton").performClick()
composeTestRule.onNodeWithText("Clicked 3 times").assertExists()
composeTestRule.onNodeWithTag("ClickableButton").performClick()
composeTestRule.onNodeWithText("Clicked 3 times").assertDoesNotExist()
}
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
var count by remember { mutableStateOf(0) }
Button(onClick = { count++ }, modifier=Modifier.testTag("ClickableButton")) {
Text("Clicked $count times", modifier = Modifier.testTag("TextCounter"))
}
}
[versions]
agp = "8.7.1"
kotlin = "2.0.0"
coreKtx = "1.15.0"
junit = "4.13.2"
robolectric = "4.14"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
lifecycleRuntimeKtx = "2.8.7"
activityCompose = "1.9.3"
composeBom = "2024.04.01"
uiTestJunit4Android = "1.7.5"
[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
roboelctric = { group = "org.robolectric", name = "robolectric", version.ref = "robolectric" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-ui-test-junit4-android = { group = "androidx.compose.ui", name = "ui-test-junit4-android", version.ref = "uiTestJunit4Android" }
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.androidx.ui.test.junit4.android)
testImplementation(libs.junit)
testImplementation("androidx.compose.ui:ui-test-junit4:1.7.5")
debugImplementation("androidx.compose.ui:ui-test-manifest:1.7.5")
testImplementation(libs.roboelctric)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)
// Gradle properties
testOptions.unitTests.isIncludeAndroidResources = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment