Som*_*ent 12 junit android unit-testing activityunittestcase android-espresso
我正在尝试将Espresso 2.0的AndoridJUnitRunner与ActivityUnitTestCase 集成.但是,当startActivity()尝试初始化mMockParent = new MockParent()时,我的测试会崩溃.
这是我做的:
使用Intellij 14 CE创建一个新项目并对build.gradle进行一些更改.
android{
defaultConfig {
applicationId "com.noob.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')
}
Run Code Online (Sandbox Code Playgroud)
编写JUnit4风格的单元测试.
@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());
}
}
Run Code Online (Sandbox Code Playgroud)
运行测试,获得堆栈跟踪.
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:42)
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:270)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Run Code Online (Sandbox Code Playgroud)
这个堆栈跟踪实际上是一个重新抛出
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Run Code Online (Sandbox Code Playgroud)
当startActivity()尝试初始化mMockParent = new MockParent()时发生:
ComponentName cn = new ComponentName(mActivityClass.getPackage().getName(),
mActivityClass.getName());
intent.setComponent(cn);
ActivityInfo info = new ActivityInfo();
CharSequence title = mActivityClass.getName();
mMockParent = new MockParent();
String id = null;
Run Code Online (Sandbox Code Playgroud)
我错过了其他任何东西让AndroidJUnitRunner与ActivityUnitTestCase一起工作吗?任何帮助将不胜感激.
Som*_*ent 21
在我的头靠在墙上,窗户和各种家具上之后,我终于让我的测试工作了!
从它的问题跟踪器的响应,我才知道,没有新的处理程序可以从仪器线程创建.这意味着必须在UI线程中调用startActivity().这导致3个解决方案.
1.使用仪器 runOnMainSync()
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
activity = startActivity(new Intent(Intent.ACTION_MAIN), null, null);
}
});
Run Code Online (Sandbox Code Playgroud)
2.创建自己的扩展ActivityUnitTestCase的基类并覆盖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();
}
Run Code Online (Sandbox Code Playgroud)
3.如果在测试方法中调用startActivity()并且整个测试可以在主线程上运行,则可以使用@UiThreadTest简单地注释测试方法.
在我的测试中,我尝试了所有3种解决方案,但只有1和2可以使用.
| 归档时间: |
|
| 查看次数: |
5415 次 |
| 最近记录: |