2016-02-29 107 views
4

此测试原本运行良好。几天后检查出一个新的分支(来自许多其他开发者的提交),它不再有效。将在MyLibrary库模块中Robolectric找不到AndroidManifest.xml

测试类:

import com.company.mylibrary.BuildConfig; 

@RunWith(RobolectricGradleTestRunner.class) 
@Config(constants = BuildConfig.class, manifest = "src/main/AndroidManifest.xml", sdk = 21) 
public class MyTest { 

我也曾尝试:

@Config(constants = BuildConfig.class, sdk = 21) 
@Config(constants = BuildConfig.class, manifest = Config.NONE, sdk = 21) 

在库模块的build.gradle

dependencies { 
    . 
    . 
    testCompile 'org.robolectric:robolectric:3.0' 

错误消息中运行时AS为: java.lang.RuntimeException: build/intermediates/manifests/full/debug/AndroidManifest.xml not found or not a file; it should point to your project's AndroidManifest.xml

从命令行运行时0

错误信息是: com.company.mylibrary.framework1.feature1.MyTest > testMethod STANDARD_ERROR java.lang.RuntimeException: build/intermediates/manifests/full/debug/AndroidManifest.xml not found or not a file; it should point to your project's AndroidManifest.xml

A) Don't know why it is looking there for the manifest B) That file/directory does not exist C) src/main/AndroidManifest.xml does exist

事情我曾尝试: - deleted the build directory in that library module - restarted Android Studio - Build/Clean - Build/Rebuild Project - run the test (both inside AS and from command line) - and tried different versions of the @Config notation

似乎是在一个靠不住的状态,我不能清楚。

我正在使用MacBook Pro。 Android Studio 2.0 beta5

回答

0

嗯,我已经解决了你现在面临的问题几次,并找到适合自己的解决方案。

通常,如果您的测试逻辑不需要访问应用程序的资源,那么使用通常的RobolectricTestRunner是值得的,因为测试执行时间相对于RobolectricGradleTestRunner下的测试执行时间相对较短。

如果出于某种原因需要访问特定的AndroidManifest.xml文件,IMO最好提供测试文件,而不是在项目的文件上进行操作。

通过说'测试文件'我的意思是: 让我们从定义什么是可以帮助我们获取资源文件路径的方法开始。我们的目标是能够在Android Studio中执行测试和,更重要的是相关的,通过CLI(gradle :project:testBuildTypeUnitTest

  1. Java的系统类:System.getProperty('user.dir')回报用户的当前工作目录。获取当前目录我们可以帮助我们获得路径,以获得运行我们测试所需的资源。
  2. 覆盖RobolectricGradleTestRunner。为了创建我们定制的测试运行,我们需要AndroidManifest.xmlres目录和assets目录路径:

    public class CompassApplicationRobolectricTestRunner extends RobolectricGradleTestRunner { 
    
    private static final int TARGET_SDK_VERSION = Build.VERSION_CODES.LOLLIPOP; 
    private static final int MIN_SDK_VERSION = Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1; 
    
    public CompassApplicationRobolectricTestRunner(Class<?> klass) throws InitializationError { 
        super(klass); 
    } 
    
    @Override 
    protected AndroidManifest getAppManifest(Config config) { 
    
        final String manifestPath = PathResolver.resolveAndroidManifestPath(); 
        final String resourcesPath = PathResolver.resolveResPath(); 
        final String assetsPath = PathResolver.resolveAssetsPath(); 
    
        AndroidManifest manifest = new AndroidManifest(
          Fs.fileFromPath(manifestPath), 
          Fs.fileFromPath(resourcesPath), 
          Fs.fileFromPath(assetsPath)) { 
         @Override 
         public int getTargetSdkVersion() { 
          return TARGET_SDK_VERSION; 
         } 
    
         @Override 
         public int getMinSdkVersion() { 
          return MIN_SDK_VERSION; 
         } 
        }; 
    
        return manifest; 
    } 
    } 
    

下面,是链接到为我工作的例子。但是,它是在一段时间之前开发出来的,从时间角度来看,我发现它可以做得更加优雅,所以如果您决定将此解决方案应用到您的项目中,请将您的路径常量组织为静态和不可变的:

https://github.com/dawidgdanski/android-compass-api/blob/master/app-tests/src/test/java/pl/dawidgdanski/compass/PathResolver.java

值得记住File.separator返回系统的默认目录分隔符。当提供与默认分隔符分隔开的系统无关路径时,它非常有用。

最后,如果上述解决方案是不是你想跟着一个,读体面文章关于建立测试环境可以在这里找到:

http://artemzin.com/blog/how-to-mock-dependencies-in-unit-integration-and-functional-tests-dagger-robolectric-instrumentation/

希望能解决你的问题。

6

您需要将测试运行配置中的工作目录设置为模块目录。 enter image description here

+1

为我工作 - 谢谢截图! –

0

就我而言,我从Android Studio手动运行单个测试(右键单击并运行),Roboelectric希望获得RELEASE版本。上面的问题是关于调试,但我的测试运行出于某种原因想要一个发布版本。

java.lang.RuntimeException: build/intermediates/manifests/release/AndroidManifest.xml not found or not a file; it should point to your project's AndroidManifest.xml

我让build目录从未创建从未在这个项目做了生产版本。

经过一段时间没有成功的摔跤后(在配置中设置路径,试图在我的CustomRoboelectric文件中获取路径),我刚刚生成了一个生产版本,这样我就可以使用清单创建版本路径并且一切正常。

所以我的解决方案是运行构建创建Roboelectric想要的。