Android/Gradle Espresso测试无法启动活动

10

我很难说服新的Android构建系统运行测试。运行测试时,出现“无法解析活动:意图”错误,这已经在其他问题中讨论过了,但其中没有任何解决我的问题的内容。

我已将其简化,使得我的测试包完全不依赖于我的主包(com.wealdtech.app),但仍然无法启动活动。

我的测试活动:

package com.wealdtech.test;

import android.app.Activity;
import android.os.Bundle;

public class TileLayoutTestActivity extends Activity
{
  @Override
  public void onCreate(final Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
  }
}

我的测试类:

package com.wealdtech.test;

import android.test.ActivityInstrumentationTestCase2;

public class TileLayoutTest extends ActivityInstrumentationTestCase2<TileLayoutTestActivity>
{
  public TileLayoutTest()
  {
    super(TileLayoutTestActivity.class);
  }

  @Override
  protected void setUp() throws Exception
  {
    super.setUp();
    setActivityInitialTouchMode(false);
  }

  public void testNull()
  {
    final TileLayoutTestActivity activity = getActivity();
    activity.finish();
  }

build.gradle的相关部分:

apply plugin: 'android-library'

android {
  compileSdkVersion 19
  buildToolsVersion "19.0.3"

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
  }

  defaultConfig {
    minSdkVersion 11
    targetSdkVersion 19

    testPackageName "com.wealdtech.test"
    testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
  }
}

我得到的完整堆栈跟踪如下:

java.lang.RuntimeException: Could not launch activity
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentation.startActivitySync(GoogleInstrumentation.java:286)
at android.test.InstrumentationTestCase.launchActivityWithIntent(InstrumentationTestCase.java:119)
at android.test.InstrumentationTestCase.launchActivity(InstrumentationTestCase.java:97)
at android.test.ActivityInstrumentationTestCase2.getActivity(ActivityInstrumentationTestCase2.java:104)
at com.wealdtech.test.TileLayoutTest.testNull(TileLayoutTest.java:21)
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.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
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 com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onStart(GoogleInstrumentationTestRunner.java:167)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Caused by: java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=com.wealdtech.test/.TileLayoutTestActivity }
at android.app.Instrumentation.startActivitySync(Instrumentation.java:379)
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentation.access$101(GoogleInstrumentation.java:52)
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentation$2.call(GoogleInstrumentation.java:268)
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentation$2.call(GoogleInstrumentation.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)

我没有包含我的AndroidManifest.xml,因为我读到的所有内容都表明我不需要为TileLayoutTestActivity添加意图,但是我仍然尝试这样做,结果却相同。

我还尝试将Gradle插件从android-library更改为android,以防止出现问题,但结果仍然相同。

我无法找到任何关于Espresso测试或使用Gradle构建系统进行测试的先决条件的文档,这些都是我已经涵盖过的。有什么想法可以让我在测试中启动该活动吗?


你能找到解决方法吗?我在我的项目中遇到了同样的问题。 - C Nick
有人有解决方案吗? - Febin Mathew
4个回答

4

对于使用 android-library 插件的项目来说,可以说 AndroidManifest.xml 并不是真正需要使用的。实际上,编译库项目所需的仅仅是以下内容:

<manifest package="com.package.yours"/>

任何您尝试放入其中的权限或意图都将在创建AAR文件时被忽略。它是一个库,据我所知,库项目清单中的任何内容都不会出现在AAR中(如果您也制作JAR,则也适用)。
但是!这就是构建推送到设备的测试项目时要使用的清单。您可以在src/androidTest/AndroidManifest.xml中输入无意义的内容,gradle不会关心,但您必须将测试活动添加到src/main/AndroidManifest.xml中,否则./gradlew connectedCheck将引发运行时异常。
我的项目看起来像这样(确实如此,我只更改了名称):
src/
  androidTest/
    java/
      com.package.mine/
        TestActivity.java
        VariousTests.java
  main/
    java/
      com.package.mine/
        FancyLibrary.java
    AndroidManifest.xml

以下是我的 AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.uie.uieanalytics">

    <uses-permission android:name="android.permission.PERM_I_NEED"/>

    <application>
        <activity android:name=".TestActivity" />
    </application>
</manifest>

否则,我使用与您相同的测试运行程序,并且我的build.gradle与您的相似。

令人烦恼的是,AS似乎没有意识到这些类的存在,它们显示为红色。不过还是谢谢你的答案! - Daniel Wilson

4

以下是其他人可能会来到这篇文章的参考,以便他们不浪费时间。

  1. 从传统的、废弃的、丑陋的ActivityInstrumentationTestCase2转换到被AndroidStudio、Gradle和Espresso 2支持的注释。这将由Google进一步开发。

  2. 永远忘记ActivityInstrumentationTestCase2!

  3. 开始使用@RunWith@LargeTest@Test@Rule等注释...


3
在进行仪器测试时,Android会构建两个APK文件 - 一个包含应用程序,另一个包含测试。如果您将活动放置在androidTest版本中,则该活动属于测试APK。如果您稍后使用仪器(直接或使用ActivityTestRule)启动活动,则Android会在您的应用程序APK中搜索它并失败,因为应用程序APK中没有这样的活动。
为了解决这个问题,您可以在应用程序的debug版本中定义测试活动(类和清单)。然后它将与您的应用程序APK一起打包,测试将正常工作。 更新:或者 - 如Austyn Mahoney所建议的那样 - 您应该使用InstrumentationRegistry.getInstrumentation().getTargetContex‌​t()来访问应用程序上下文,而不是仪器上下文。

你不需要在测试包中定义Activity,只需要在创建Intent时使用正确的“Context”。使用InstrumentationRegistry.getInstrumentation().getTargetContext()。这将返回应用程序的“Context”,而不是仪器apk的“Context”。 - Austyn Mahoney

1

请在规则内更改活动名称,以便您可以运行

ActivityTestRule mActivityRule = new ActivityTestRule<>( 更改活动名称)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接