如何创建Android JUnit测试用例来测试Activity中生成的Intent的内容?
我有一个包含EditText窗口的Activity,当用户输入完所需的数据后,Activity会向IntentService启动一个Intent,它会记录数据并继续执行应用程序.这是我想要测试的类,OnEditorActionListener/PasscodeEditorListener是作为一个单独的类创建的:
public class PasscodeActivity extends BaseActivity {
EditText m_textEntry = null;
PasscodeEditorListener m_passcodeEditorListener = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.passcode_activity);
m_passcodeEditorListener = new PasscodeEditorListener();
m_textEntry = (EditText) findViewById(R.id.passcode_activity_edit_text);
m_textEntry.setTag(this);
m_textEntry.setOnEditorActionListener(m_passcodeEditorListener);
}
@Override
protected void onPause() {
super.onPause();
/*
* If we're covered for any reason during the passcode entry,
* exit the activity AND the application...
*/
Intent finishApp = new Intent(this, CoreService.class);
finishApp.setAction(AppConstants.INTENT_ACTION_ACTIVITY_REQUESTS_SERVICE_STOP);
startService(finishApp);
finish();
}
}
class PasscodeEditorListener implements OnEditorActionListener{
@Override
public boolean …Run Code Online (Sandbox Code Playgroud) 我目前正在构建一个Android应用程序,我正在进行最好的测试设置,以使我的应用程序防弹:)如果有任何小/大缺失请随时推荐!:)
所以我的测试有点像这样:(请记住我的应用程序有一个后端服务器)
所有这些测试都将在TeamCity CI服务器上运行.我们试图在尽可能多的测试中使用roboletric,因为它比在设备上加载测试更快.
我的问题是,我的设置中是否有任何大/小的缺失,或者是否有任何框架可以使我们更容易运行除了使用robolectric/android maven插件/ teamcity之外的所有这些测试?
我有一个Android项目,它使用:
不涉及任何活动的测试(例如断言1 == 1),工作正常.但是,当我使用activity时会产生以下错误:
java.lang.RuntimeException: huh? can't find parent for StyleData{name='Theme_Sanactbar', parent='@style/Theme_AppCompat_Light_DarkActionBar'}
at org.robolectric.shadows.ShadowAssetManager$StyleResolver.getParent(ShadowAssetManager.java:365)
at org.robolectric.shadows.ShadowAssetManager$StyleResolver.getAttrValue(ShadowAssetManager.java:350)
Run Code Online (Sandbox Code Playgroud)
我尝试了https://github.com/robolectric/robolectric/issues/979,但它创造了相同的结果.
供参考:
这是我的PROJECT/build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
// For Robolectric
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url 'https://github.com/rockerhieu/mvn-repo/raw/master/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.11.+'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2+'
classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.1-SNAPSHOT'
}
}
allprojects {
repositories {
mavenCentral()
// For Robolectric
maven { …Run Code Online (Sandbox Code Playgroud) 我正在尝试为现有应用程序编写一些测试.我想为测试提供一个测试应用程序类,我按照这里的例子,因为我也使用Dagger进行DI.
但是,如果测试中的活动是ActionBarActivity,我会收到以下异常:
java.lang.IllegalArgumentException: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{mypackage.view.activity/mypackage.view.activity.MyActivity}
at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:282)
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:116)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
at mypackage.view.activity.MyActivity.onCreate(MyActivity.java:68)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:158)
at mypackage.MyActivityTest.test(MyActivityTest.java:89)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Caused by: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{mypackage.view.activity/mypackage.view.activity.MyActivity}
at android.app.ApplicationPackageManager.getActivityInfo(ApplicationPackageManager.java:242)
at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:298)
at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:279)
... 21 more
Run Code Online (Sandbox Code Playgroud)
我的测试类如下所示:
public class MyActivityTest extends ActivityUnitTestCase<MyActivity> {
...
public MyActivityTest() {
super(MyActivity.class);
}
private Context context;
private TestBaseApplication application;
@Override
protected void setUp() throws Exception {
super.setUp(); …Run Code Online (Sandbox Code Playgroud) android unit-testing android-testing android-actionbaractivity
我在我的Android(4.0+)应用程序片段(在Activity中)使用Tab栏.
我想创建Espresso测试但是如果我创建主要Activity并打开片段.我得到这个例外:
java.lang.IllegalStateException: ViewPager does not have adapter instance.
at com.astuetz.PagerSlidingTabStrip.setViewPager(PagerSlidingTabStrip.java:177)
at cz.villamemories.detoxme.staticcontent.StaticContentFragment.onCreateView(StaticContentFragment.java:197)
Run Code Online (Sandbox Code Playgroud)
片段中的代码:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewPagerAdapter = new StaticContentPagerAdapter(
this.getChildFragmentManager(), mItemsList, categories);
mPager.setAdapter(mViewPagerAdapter);
mTabs.setViewPager(mPager); //line 197
Run Code Online (Sandbox Code Playgroud)
你有什么提示可能有问题吗?怎么了?
我正在尝试使用MVP来增强单元测试并更快地运行测试(因为我测试的逻辑不是android代码所以我避免使用像RobotElectric这样的东西).
但I'm使用RXAndroid,它需要尺蠖得到Schedulers.io()和AndroidSchedulers.mainThread(),当我尝试运行某个像
class Phone {
public Observable<> sendSms(String number){
//...
}
}
Phone.getInstance().sendSms(phoneNumber)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(phone -> {
mView.dismissProgress();
mView.startCodeView(phone);
}, error -> {
mView.dismissProgress();
mView.showError(error);
});
Run Code Online (Sandbox Code Playgroud)
我明白了:
Caused by: java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked. See http://g.co/androidstudio/not-mocked for details.
at android.os.Looper.getMainLooper(Looper.java)
at rx.android.schedulers.AndroidSchedulers.<clinit>(AndroidSchedulers.java:27)
... 28 more
Run Code Online (Sandbox Code Playgroud)
我试过了:
android {
// ...
testOptions {
unitTests.returnDefaultValues = true
}
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,因为我想运行完整的JUnit测试,而不是Roboelectric或Espresso的东西.
我怎么能完成它?是否有任何调度程序不会因此而崩溃?
我想将数据放在androidTest/res/values/string.xml文件夹的xml文件中.我创建了文件说test.xml,内容如下.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="test">hello</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
当我试图通过R.string.test访问现场测试时.它无法访问,并说"无法解决符号测试".有人可以在这里建议我.
testing android android-resources android-testing android-espresso
假设我的build.gradle文件为在以下位置定义的同一变量定义了不同的值BuildConfig:
android {
def INTEGER= "integer"
def VARIABLE = "variable"
buildTypes {
release {
buildConfigField BOOLEAN, VARIABLE, "1"
}
debug {
buildConfigField BOOLEAN, VARIABLE, "2"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想BuildConfig为这个变量定义值androidTest(创建的那个app/build/generated/source/buildConfig/androidTest/debug/{app_id}/test/BuildConfig.java)
现在,该值与debug闭包中的值相同.
是否有可能改变它?
我写过单元测试,仪器测试和浓缩咖啡测试.我还使用Android Test Orchestrator运行它们以获得清晰的应用状态(对于Espresso测试非常重要).当我从Android Studio运行这些测试时,一切正常.但是当我尝试使用命令行时,我收到错误,我无法理解.
当我尝试:
./gradlew connectedAndroidTest or connectedDebugAndroidTest
Run Code Online (Sandbox Code Playgroud)
我收到:
Instrumentation run failed due to 'java.lang.IllegalStateException'
com.android.builder.testing.ConnectedDevice > No tests found.[SM-J106H -
6.0.1] FAILED
No tests found. This usually means that your test classes are not in the
form that your test runner expects (e.g. don't inherit from TestCase or lack
@Test annotations).
Run Code Online (Sandbox Code Playgroud)
当然我所有的测试都是用@Test注释的.
当我尝试
adb shell am instrument -w my.package/android.test.InstrumentationTestRunner
Run Code Online (Sandbox Code Playgroud)
我收到
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for:
ComponentInfo{mypackage/myCustomRunner}
INSTRUMENTATION_STATUS_CODE: -1
Run Code Online (Sandbox Code Playgroud)
我使用CustomTestRunner,但错误保持不变.
当我尝试
adb shell 'CLASSPATH=$(pm path android.support.test.services) app_process …Run Code Online (Sandbox Code Playgroud) 我正在使用目前处于alpha状态的新导航控制器.它工作正常,但我找不到任何文档或示例应用程序,以了解如何进行测试.谷歌也发布了android.arch.navigation:navigation-testing用于测试导航的库,但同样没有文档.
任何帮助或建议将不胜感激.
android android-testing android-jetpack android-architecture-navigation
android-testing ×10
android ×8
robolectric ×2
unit-testing ×2
android-architecture-navigation ×1
gradle ×1
java ×1
junit ×1
rx-android ×1
rx-java ×1
testing ×1