Robolectric 配置

费了些工夫,已配好,按记录留记录

按官网操作http://robolectric.org/getting-started/

1引包

testCompile "org.robolectric:robolectric:3.0"

示例
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.loopj.android:android-async-http:1.4.7'
compile 'com.jakewharton:butterknife:6.1.0'
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:3.0'
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:support-v4:22.2.0'
compile 'com.squareup.okhttp:okhttp:2.5.0'
compile files('libs/jsoup-1.8.3.jar')
compile 'junit:junit:4.12'
}


2edit configurations

 editing the run configurations, Defaults -> JUnit and changing the working directory value to $MODULE_DIR$.

3添加JUnit(这一点官方没有说明)

editing the run configurations

左上角 按+号 添加选中JUnit

设置fock mode 为method

设置user pclasspath of module 选中要测试的module

设置Class(需要加注解,才能识别)

4在方法上右键goto test 新建测试,编写方法,加注解

示例

@RunWith(RobolectricGradleTestRunner.class)

public class netTest {
    @Test
    @Config(constants = BuildConfig.class)
    public void testHttpCookieGet() throws Exception {
        System.out.println("ssout3");
            net n=new net();
            n.HttpCookieGet();
            System.out.println("ssout");
            Log.w("wbody  hello", "workd");
            assertEquals("1","1");
    }
}

注意引用

import com.mac.cdp.baidusign.BuildConfig;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

5执行报错

项目api版本为22

Robolectric does not support API level 22

改为 

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

执行通过

6

目前Log 和system.out.print()都还打不出日志。

 傻了,日志还是在 android monitor里,我还以为用这种测试不会启动 android

posted @ 2015-09-14 22:19  cclient  阅读(667)  评论(0编辑  收藏  举报