Robolectric单元测试糖ORM

时间:2016-08-28 15:46:39

标签: android unit-testing robolectric sugarorm

我是单位测试的新手,甚至更新的Robolectric。 现在,我正在尝试使用Sugar ORM运行Robolectric测试,但不断收到此消息

objc[535]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
Connected to the target VM, address: '127.0.0.1:61500', transport: 'socket'
No such manifest file: /Users/virginia/Documents/Android%20Developer/ravnandroid_v5/app/build/intermediates/manifests/full/debug/AndroidManifest.xml
meta data -> {}
java.lang.NullPointerException: SugarContext has not been initialized properly. Call SugarContext.init(Context) in your Application.onCreate() method and SugarContext.terminate() in your Application.onTerminate() method.

    at com.orm.SugarContext.getSugarContext(SugarContext.java:24)
    at com.orm.SugarRecord.save(SugarRecord.java:360)
    at com.orm.TestSugarApp.insertUserTest(TestSugarApp.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [...]

Disconnected from the target VM, address: '127.0.0.1:61500', transport: 'socket'

Process finished with exit code 255

这是我的 graddle 依赖关系:

compile group: 'com.google.guava', name: 'guava', version: '19.0'
testCompile 'junit:junit:4.12'
testCompile "org.robolectric:robolectric:3.1.2"

测试类

package com.orm;

@RunWith(CustomRoboelectricGradleTestRunner.class)
@Config( constants = BuildConfig.class, sdk = 23)
public class TestSugarApp {

    @Test
    public void startEverTestSugarAppAsFirst() {
        assertEquals(4, 2 + 2);
    }

    @Test
    public void insertUserTest() {
        DBRavnUser user = new DBRavnUser("8099999999", "Sample User");
        long id = user.save();
        assertEquals(id > 0, id);
    }
}

这是我的RobolectricGradleTestRunner扩展名:

public class CustomRoboelectricGradleTestRunner extends RobolectricGradleTestRunner {
    public CustomRoboelectricGradleTestRunner(Class<?> testClass) throws InitializationError {
        super(testClass);
        String buildVariant = (BuildConfig.FLAVOR.isEmpty()
                ? "" : BuildConfig.FLAVOR+ "/") + BuildConfig.BUILD_TYPE;
        String intermediatesPath = BuildConfig.class.getResource("")
                .toString().replace("file:", "");
        intermediatesPath = intermediatesPath.substring(0, intermediatesPath.indexOf("/classes"));

        System.setProperty("android.package",
                BuildConfig.APPLICATION_ID);
        System.setProperty("android.manifest",
                intermediatesPath + "/manifests/full/" + buildVariant + "/AndroidManifest.xml");
        System.setProperty("android.resources", intermediatesPath + "/res/merged/" + buildVariant);
        System.setProperty("android.assets", intermediatesPath + "/assets/" + buildVariant);
    }

    @Override
    protected AndroidManifest getAppManifest(Config config) {

        String manifestProperty = System.getProperty("android.manifest");
        String resProperty = System.getProperty("android.resources");
        String assetsProperty = System.getProperty("android.assets");
        AndroidManifest manifest = new AndroidManifest(
                Fs.fileFromPath(manifestProperty),
                Fs.fileFromPath(resProperty),
                Fs.fileFromPath(assetsProperty)) {

            @Override
            public Map<String, Object> getApplicationMetaData() {
                Map<String, Object> metadata =  super.getApplicationMetaData();
                metadata.put("DATABASE", "rcn.db");
                metadata.put("VERSION", "16");
                metadata.put("QUERY_LOG", "false");
                metadata.put("DOMAIN_PACKAGE_NAME", BuildConfig.APPLICATION_ID);
                return metadata;
            }
        };
        manifest.setPackageName(BuildConfig.APPLICATION_ID);
        return manifest;
    }
}

如您所见,我正在覆盖getApplicationMetaData以设置SugarORM元数据信息。

对于正确方向的任何帮助将不胜感激。

问候。

3 个答案:

答案 0 :(得分:0)

  

在Application.onCreate()方法中调用SugarContext.init(Context),在Application.onTerminate()方法中调用SugarContext.terminate()

此错误消息已告诉您该怎么做。您可以在测试的开始和结束时添加这两个方法调用,而不是您的应用程序类。

答案 1 :(得分:0)

这种方法在任何真正的Android设备中都不会被调用。此方法仅用于模拟过程环境(如模拟器)

答案 2 :(得分:0)

我使用了您的RobolectricGradleTestRunner,并通过添加以下内容使其成功:

@Before
public void setupFromBase() {
    SugarOrm.init(Robolectric.buildActivity(Activity.class)
            .create()
            .resume()
            .get());
}

@After
public void cleanupFromBase() {
    SugarOrm.terminate();
}