Java – Robolectric test case for Kotlin code

Robolectric test case for Kotlin code… here is a solution to the problem.

Robolectric test case for Kotlin code

I have a MainActivity written in Kotlin. I can run setupActivity:: in kotlin like this

@RunWith(RobolectricTestRunner::class)
@Config(constants = BuildConfig::class)
class MyActivityTestKotlin {
    @Before
    public fun setup() {
        Robolectric.setupActivity(MainActivity::class.java)
    }
}

However, when I write a test in java, I get the error:

android.content.res.Resources$NotFoundException: String resource ID #0x7f0c001f

Java code:

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class MyActivityTest {
    @Before
    public void setup() {
        setupActivity(MainActivity.class);
    }
}

Is it possible to write tests in Java for this case? Thanks!!

Solution

Added Access to compiled resources during testing in the gradel unit test option for resources

testOptions {
    unitTests {
        includeAndroidResources = true
    }
}

RoboElectric docs

Related Problems and Solutions