dimanche 28 août 2016

Robolectric Unit Testing Sugar ORM

I'm new with unit testing, even newer with Robolectric. Now, I'm trying to run Robolectric testing with Sugar ORM, but keep getting this message

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

This is my graddle dependency:

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

Test Class

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);
    }
}

This, my RobolectricGradleTestRunner extension:

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;
    }
}

As you see here, I'm overriding getApplicationMetaData to setup SugarORM metadata info.

Any help in the correct direction will be appreciated.

Regards.

Aucun commentaire:

Enregistrer un commentaire