Skip to content

Running tests on other thread does not properly switch Main Looper to new thread #3829

@matejdro

Description

@matejdro

Description

When Robolectric test is run on another thread (for example due to JUnit's timeout feature), Looper.getMainLooper() will stay on current thread. This causes issues with libraries that check and throw exception if any code is run on non-main thread (for example LiveData.setValue() from android architecture components breaks).

Steps to Reproduce

  1. Create new blank android project
  2. Add robolectric dependency
  3. Run following test:
@RunWith(RobolectricTestRunner::class)
class LooperTest {
    @Test(timeout = 100L)
    fun mainLooperMatchesTimeout() {
        assertSame(Thread.currentThread(), Looper.getMainLooper().thread)
    }
}

If you remove timeout parameter from above test, it passes.

Robolectric & Android Version

Robolectric 3.8

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