android / android/android-test

ServiceTestRule not support the Service whicha runs in a separat process

Open
#2,399 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
1.2k
Forks
342
Avg merge
11h 29m
Merged PRs (30d)
2

Description

I'm writing the unit tests for MyService, which runs in a separate process `android:process=":ai"`, and the code like this:
```kotlin
//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.

Contributor guide

Open the contributing guide

Research direction

Start at ServiceTestRule.bindService and compare its behavior for the shown MyServiceTest when MyService runs in the default process versus the separate ":ai" process. Reproduce the ClassCastException with the provided example; done means the separate-process service can be bound without the BinderProxy cast failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
mobile, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.