How to test Android Intent?

62 views
Skip to first unread message

Andriy Matkivskiy

unread,
Aug 10, 2016, 9:26:03 AM8/10/16
to Robolectric
Enter code here..Hi guys,

On my project I have function which creates the Intent depending on the content provided to it:

public Intent createIntent(Context context, String contentType, String contentId) {
        Intent intent;
        if ("live".equals(contentType)) {
            intent = new Intent(context, MainActivity.class);
        } else {
            intent = new Intent(context, ContentItemActivity.class);
        }

        intent.putExtra("id", contentId);
        return intent;
}

Now I want to test the intent created by this function. How I can do this without starting activity?
Do I need to use Android instrumentation testing to do it or I can accomplish it by using Robolectric?

Jonathan Gerrish

unread,
Aug 10, 2016, 10:26:45 AM8/10/16
to robol...@googlegroups.com
It is easy to do in Robolectric

public void testLiveIntent() {
  Intent actual createIntent(RuntimeEnvironment.application, "live", "contentId);
  assertThat(actual.getComponent().getClassname
()).isEqualTo(MainActivity.class.getName());
}

public void testNonLiveIntent() {
  Intent actual createIntent(RuntimeEnvironment.application, "non-live", "contentId);

  assertThat(actual.getComponent().getClassname
()).isEqualTo(ContentItemActivity.class.getName());
}

--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andriy Matkivskiy

unread,
Aug 10, 2016, 10:31:27 AM8/10/16
to Robolectric
Great! 
Thanks Jonathan, but what about testing that correct "contentId" is set to the intent?
Like:

assertThat(actual.getString("id")).isEqualTo("123")
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.

Jonathan Gerrish

unread,
Aug 10, 2016, 10:35:39 AM8/10/16
to robol...@googlegroups.com
These are standard Android APIs so you can just call them yourself from the test. getExtras()... Or getStringExtra() I believe (just left my desk so not 100%)

To unsubscribe from this group and stop receiving emails from it, send an email to robolectric+unsubscribe@googlegroups.com.

Andriy Matkivskiy

unread,
Aug 10, 2016, 11:00:54 AM8/10/16
to Robolectric
I tried your suggestion but I got such exception:

 java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap
	at org.robolectric.internal.bytecode.InstrumentationConfiguration.<init>(InstrumentationConfiguration.java:173)
	at org.robolectric.internal.bytecode.InstrumentationConfiguration.<init>(InstrumentationConfiguration.java:32)
	at org.robolectric.internal.bytecode.InstrumentationConfiguration$Builder.build(InstrumentationConfiguration.java:155)
	at org.robolectric.RobolectricTestRunner.createClassLoaderConfig(RobolectricTestRunner.java:120)
	at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:174)
	at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:49)
	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.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:142)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:105)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:56)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:64)
	at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:49)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
	at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
	at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
	at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:106)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
	at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
	at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
	at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

Jonathan Gerrish

unread,
Aug 10, 2016, 11:02:40 AM8/10/16
to robol...@googlegroups.com
I guess you are missing Guava on your class path then.

To unsubscribe from this group and stop receiving emails from it, send an email to robolectric+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages