Android - Testing a String Resource with Robolectric

I just want to check that getting the String resource is equal to what I think should be equal. My problem is that I have Realm in my project. I know that Robolectric doesn't support Realm (he mentions it in the documentation), but I don't call Realm at all, so I feel like there might be a way to do this.

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

import static org.junit.Assert.assertEquals;

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

    @Test
    public void testingString() {
        String resourceString = RuntimeEnvironment.application.getString(R.string.app_name);
        assertEquals(resourceString, "Yeah");
    }

}

      

It's like IS trying to summon Realm

java.lang.UnsatisfiedLinkError: Can't load library: 
/var/folders/3x/ddxtg5fs1hxgqrp6h63vsc140000gp/T/android-tmp-
robolectric3612430387389787158/app_lib/librealm-jni.dylib.3.5.0

      

EDIT: I tried some other things and it seems that setting manifest

in @Config annotation is the problem, but then I getandroid.content.res.Resources$NotFoundException: unknown resource 2131362072

Any other thoughts? Can I make another application class where Realm

it is not called? How does the / test directory know about this?

EDIT FOR DAVID:

I've tried this:

@RunWith(RobolectricGradleTestRunner.class)
@Config(application = TestingApplication.class, constants = BuildConfig.class, sdk = 21)
public class ResourceTester {
    @Test
    public void newTestingTests() throws Exception {
        String appName = RuntimeEnvironment.application.getString(R.string.app_name);
    }
}

      

but i get:

android.content.res.Resources$NotFoundException: unknown resource 2131362072

If I change it to

@RunWith(RobolectricTestRunner.class)
//@RunWith(RobolectricGradleTestRunner.class)
@Config(application = TestingApplication.class, constants = BuildConfig.class, sdk = 21)
public class ResourceTester {
    @Test
    public void newTestingTests() throws Exception {
        String appName = RuntimeEnvironment.application.getString(R.string.app_name);
    }
}

      

I get

WARNING: No manifest file found at ./AndroidManifest.xml.Falling back to the Android OS resources only.
To remove this warning, annotate your test class with @Config(manifest=Config.NONE).

android.content.res.Resources$NotFoundException: unknown resource 2131362072

      

+3


source to share


1 answer


If you have a heavy application class (for example with Realm dependencies, Crashlytics, etc.) and your unit tests do not refer to them, you can use android.app.Application

as your application class in config:

@RunWith(RobolectricTestRunner.class)
@Config(application = android.app.Application.class, manifest="src/main/AndroidManifest.xml", sdk = 23)
public class ResourceTester {

      



Also make sure to is Working Directory

set to $MODULE_DIR$

if you are using Mac or Linux following the getting started instructions

module dir in working directory

+7


source







All Articles