Skip to content

ServiceTestRule not support the Service whicha runs in a separat process #2399

@Leojangh

Description

@Leojangh

I'm writing the unit tests for MyService, which runs in a separate process android:process=":ai", and the code like this:

//The Service:
class MyService: Service() {
    inner class TestBinder() : Binder() { 
        fun getService() = this@MyService
    }

    override fun onBind(intent: Intent?): IBinder? {
        return TestBinder()
    }
}
//The test:
@RunWith(AndroidJUnit4::class)
internal class MyServiceTest { 

    private val rule = ServiceTestRule()

    @Test
    fun test() {
        val binder = rule.bindService(Intent(ApplicationProvider.getApplicationContext(), MyService::class.java))
        val service = (binder as MyService.TestBinder).getService() 
    }
}

But ClassCastException was thrown:
java.lang.ClassCastException: android.os.BinderProxy cannot be cast to MyService$TestBinder

But test works if I make MyService without separate process.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions