• Getting Started


    Robolectric works best with Gradle or Maven. If you are starting a new project, we would reccomend Gradle first (since it is the build system of choice in Android Studio) and Maven second. Regardless, Robolectric should integrate well into whatever build tool you are using.

    Building with Gradle

    Add the following to your build.gradle:

    testCompile "org.robolectric:robolectric:3.1.1"
    

    Annotate your test with the Gradle test runner:   //使用注解注释每个测试类

    @RunWith(RobolectricGradleTestRunner.class)
    @Config(constants = BuildConfig.class)
    public class SandwichTest {
    }
    

    Note that you must specify the constants field which points to the BuildConfig.class generated by the build system. Robolectric uses the constants in the class to compute the output paths used by Gradle when building your project. Without these values, Robolectric will not be able to find your merged manifest, resources, or assets.

    //注意你必须指定常量域指向BuildConfig.class。robolectric使用这个常量来计算构建项目时的输出路径,没有这些变量,robolectric将不会找到你的manifest,resource和assets

    Building with Maven

    Add the following to your pom.xml:

    <dependency>
       <groupId>org.robolectric</groupId>
       <artifactId>robolectric</artifactId>
       <version>3.1.1</version>
       <scope>test</scope>
    </dependency>
    

    Annotate your test with the base test runner:

    @RunWith(RobolectricTestRunner.class)
    public class SandwichTest {
    }
    

    If you reference resources that are outside of your project (i.e. in a aar dependency), you will need to provide Robolectric with a pointer to the exploded aar in your build system. See the section titled "Using Library Resources" for more information.

    Building with Android Studio

    Robolectric works with Android Studio 1.1.0 or newer. Simply follow the instructions above for working with Gradle. For versions of Android Studio older than 2.0.0, enable the unit test support in the "Build Variants" tab and run your test. As of Android Studio 2.0.0, the test artifacts feature is enabled by default, and the setting to enable/disable test artifacts can be found in File Menu -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle -> Experimental.

    Android Enable Unit Tests

    Note for Linux and Mac Users

    If you are on Linux or on a Mac, you will probably need to configure the default JUnit test runner configuration in order to work around a bug where Android Studio does not set the working directory to the module being tested. This can be accomplished by editing the run configurations, Defaults -> JUnit and changing the working directory value to $MODULE_DIR$.

    Android Studio Configure Defaults

    Building with Eclipse

    Install the m2e-android plugin for Eclipse, and import the project as a Maven project. After importing into Eclipse, you have to mark the consume-aar goal as ignored, since AAR consumption is not yet supported by m2e-android. To do this, simply apply the Quick fix on the "Plugin execution not covered by lifecycle configuration" error. To run your tests, just right click on the project -> Run as ->JUnit Test, then choose the Eclipse JUnit Launcher.

    Sample Projects

    Look at the Robolectric samples to see how fast and easy it can be to test drive the development of Android applications. In addition, check out the Gradle or Maven starter projects.

    ----------- Do not start just casually, and do not end just casually. -----------
  • 相关阅读:
    centos7使用yum时报错”Error: rpmdb open failed“
    shell脚本监控httpd进程,如果进程异常自动重启服务
    DNS域名服务系统(二)--基本dns域名解析/主从dns解析
    DNS域名服务系统(一)
    ** server can't find mail.extmail.org: SERVFAIL
    搭建邮件服务器postfix向mysql导入extmail.sql文件时报错 ERROR 1064 (42000) at line 50: You have an error in your SQL syntax; 'TYPE=MyISAM COMMENT='Ext/Webman
    Nginx实现https和跳转功能
    Nginx的反向代理和负载均衡
    Nginx基于域名的虚拟主机
    jquery select chosen禁用某一项option
  • 原文地址:https://www.cnblogs.com/yexiant/p/5691937.html
Copyright © 2020-2023  润新知