ServiceTestRule not support the Service whicha runs in a separat process

Open
#2,399 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

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.

Description

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.

Dominant language
Java
Stars
1.2k
Forks
342
Avg merge
1d 14h
Merged PRs (30d)
5

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from android/android-test

All issues in android/android-test

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.