Hafiz-Waleed-Hussain / android-test-kit

Automatically exported from code.google.com/p/android-test-kit
0 stars 0 forks source link

AndroidJUnitRunner cannot create ActivityUnitTestCase's target activity #121

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

1. Create a new project with Intellij 14 CE and make some changes to 
build.gradle.
android{
    defaultConfig {
        applicationId "com.littledot.testing"
        minSdkVersion 9
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.+'

    androidTestCompile('org.mockito:mockito-core:1.9.5')
    androidTestCompile('com.google.dexmaker:dexmaker:1.2')
    androidTestCompile('com.google.dexmaker:dexmaker-mockito:1.2')

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.0')
    androidTestCompile('com.android.support.test:testing-support-lib:0.1')
}

2. Write a JUnit4-style unit test.
@RunWith(AndroidJUnit4.class)
public class MainActivityJUnit4Test extends ActivityUnitTestCase<MainActivity> {
    public MainActivityJUnit4Test() {
        super(MainActivity.class);
    }

    MainActivity activity;

    @Before
    public void setup() throws Exception {
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        super.setUp();
        ContextThemeWrapper context = new ContextThemeWrapper(getInstrumentation().getTargetContext(), R.style.AppTheme);
        setActivityContext(context);
        activity = startActivity(new Intent(Intent.ACTION_MAIN), null, null);
    }

    @Test
    public void baseCase() {
        TextView tv = (TextView) activity.findViewById(R.id.tv);
        Assert.assertEquals("Hello World", tv.getText());

    }
}

3.Run the test, getting a stack trace.
junit.framework.AssertionFailedError
at junit.framework.Assert.fail(Assert.java:48)
at junit.framework.Assert.assertTrue(Assert.java:20)
at junit.framework.Assert.assertNotNull(Assert.java:218)
at junit.framework.Assert.assertNotNull(Assert.java:211)
at 
android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:147)
at 
com.sdchang.testing.MainActivityJUnit4Test.setup(MainActivityJUnit4Test.java:32)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at 
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java
:45)
at 
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:
15)
at 
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:4
2)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68
)
at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47
)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
at 
android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:2
70)
at 
android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)

The crash happens when startActivity() tries to initialize mMockParent = new 
MockParent(). That line throws: Can't create handler inside thread that has not 
called Looper.prepare().

Am I missing anything else to get AndroidJUnitRunner working with 
ActivityUnitTestCase? Any help would be greatly appreciated.

Original issue reported on code.google.com by littledo...@gmail.com on 16 Jan 2015 at 7:31

GoogleCodeExporter commented 9 years ago
Please see 
https://code.google.com/p/android-test-kit/wiki/AndroidJUnitRunnerUserGuide#Inst
rumentation_Thread_Handlers

Basically the new AndroidJUnitRunner prevents any Handler from being created on 
the Instrumentation worker thread. ActivityUnitTestCase.startActivity() needs 
to be called on the main thread. There are several ways to do this:

1. Use Instrumentation's runOnMainSync()
getInstrumentation().runOnMainSync(new Runnable() {
    @Override
    public void run() {
        activity = startActivity(new Intent(Intent.ACTION_MAIN), null, null);
    }
});

2. Create your own base class that extends ActivityUnitTestCase<T extends 
Activity> and override startActivity

@Override
protected T startActivity(final Intent intent, final Bundle savedInstanceState,
        final Object lastNonConfigurationInstance) {
    return startActivityOnMainThread(intent, savedInstanceState, lastNonConfigurationInstance);
}

private T startActivityOnMainThread(final Intent intent, final Bundle 
savedInstanceState,
        final Object lastNonConfigurationInstance) {
    final AtomicReference<T> activityRef = new AtomicReference<>();
    final Runnable activityRunnable = new Runnable() {
        @Override
        public void run() {
            activityRef.set(YourBaseActivityUnitTestCase.super.startActivity(
                    intent, savedInstanceState, lastNonConfigurationInstance));
        }
    };

    if (Looper.myLooper() != Looper.getMainLooper()) {
        getInstrumentation().runOnMainSync(activityRunnable);
    } else {
        activityRunnable.run();
    }

    return activityRef.get();
}

You would extend your base test class instead of ActivityUnitTestCase directly

3. If you call startActivity() in a test method and your entire test can run on 
the main thread, you can simply annotate your test method with @UiThreadTest.

Hope this helps.

Original comment by mhernan...@gmail.com on 28 Jan 2015 at 7:24

GoogleCodeExporter commented 9 years ago
Thank you very much for your response! You saved my day!
I tried all 3 techniques, from 3 > 1 > 2. At first, I was a bit bummed out 
because 3 did not work. But luckily 1 & 2 was a success!

Thank you mhernan...!

Original comment by littledo...@gmail.com on 6 Feb 2015 at 10:35

GoogleCodeExporter commented 9 years ago
Anyone have success doing this with launchActivity as well?

Original comment by bi...@jana.com on 4 Mar 2015 at 4:12

GoogleCodeExporter commented 9 years ago
We are in the process of deprecating ActivityUnitTestCase. We recommend to move 
business logic to a separate class and unit test it with gradle unit test 
support (mockable android.jar).

Original comment by vale...@google.com on 18 Mar 2015 at 5:22

GoogleCodeExporter commented 9 years ago

Original comment by vale...@google.com on 18 Mar 2015 at 5:23