does anyone have any working samples of testing co...
# compose
p
does anyone have any working samples of testing composable functions? im getting
E/TestRunner: java.lang.RuntimeException: Could not launch activity
when I attempt to use
createComposeRule()
. or am I trying to use something that isn’t ready?
Copy code
@RunWith(JUnit4::class)
class SampleTests {

    @get:Rule
    val composeTestRule = createComposeRule(disableTransitions = true)

    @Test
    fun twoComponents_areChecked() {
        composeTestRule.setContent {
            MaterialTheme {
                Surface {
                    Column {
                        Checkbox(checked = true, onCheckedChange = null)
                        Checkbox(checked = true, onCheckedChange = null)
                    }
                }
            }
        }

        findAll(isOn())
            .assertCountEquals(2)
            .forEach {
                it.assertIsOn()
            }
    }
}
r
rest of the stack trace?
p
Copy code
java.lang.RuntimeException: Could not launch activity
at androidx.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:495)
at androidx.test.rule.ActivityTestRule.launchActivity(ActivityTestRule.java:358)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:529)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2189)
Caused by: java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=soffrito.libraries.design.test/android.app.Activity }
at android.app.Instrumentation.startActivitySync(Instrumentation.java:501)
at android.app.Instrumentation.startActivitySync(Instrumentation.java:464)
at androidx.test.runner.MonitoringInstrumentation.access$201(MonitoringInstrumentation.java:99)
at androidx.test.runner.MonitoringInstrumentation$4.call(MonitoringInstrumentation.java:471)
at androidx.test.runner.MonitoringInstrumentation$4.call(MonitoringInstrumentation.java:468)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
r
Declare the Activity in your manifest:
Copy code
<activity android:name="android.app.Activity" android:theme="@style/some_theme_goes_here"/>
from:
Caused by: java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=soffrito.libraries.design.test/android.app.Activity }
p
🤦
👏🏻 1
thank you!
r
no problem!
c
are there any official tests anywhere? or plans for them?
c
thanks, interesting (if ugly)…
p
s
For the question regarding official tests, I guess these here are kinda “official”, but they're not plentiful: https://github.com/android/compose-samples/tree/master/JetNews/app/src/androidTest/java/com/example/jetnews
👍🏻 1