Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Testing a string resource with Robolectric

I just want to test that getting a String resource equals what I think it should equal. My issue seems to be that I have Realm in my project. I know that Robolectric doesn't support Realm (it states it in the documentation), but I'm not invoking 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 does look like it IS trying to invoke 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 more things, and it seems like setting the manifestin the @Config annotation is the issue, but then I get a android.content.res.Resources$NotFoundException: unknown resource 2131362072

Any other thoughts? Can I make another Application class where Realm isn't called? How would the /test directory know that?

EDIT FOR DAVID:

I 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 a:

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
like image 973
EGHDK Avatar asked Jul 20 '17 22:07

EGHDK


1 Answers

If you have a heavyweight Application class (e.g., with dependencies on Realm, Crashlytics etc.) and your unit tests do not refer to these you can use android.app.Application as your Application class in the config:

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

Also make sure you have Working Directory set to $MODULE_DIR$ if you are using Mac or Linux as per the getting started instructions

module dir in working directory

like image 76
David Rawson Avatar answered Sep 20 '22 16:09

David Rawson