robolectric / robolectric

Android Unit Testing Framework
http://robolectric.org
Other
5.9k stars 1.37k forks source link

java.lang.NullPointerException at com.android.internal.app.WindowDecorActionBar.getDecorToolbar #5185

Open emartynov opened 5 years ago

emartynov commented 5 years ago

Description

I have Robolectric tests in the module project.

They are run with:

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], application = TestApplication::class)

Where Test application has the next lines:

    override fun onCreate() {
        super.onCreate()
        setTheme(androidx.appcompat.R.style.Theme_AppCompat_Light_DarkActionBar)
    }

I'm testing fragments and I just add them to AppCompatActivity. All tests are working fine expect ones that try to show AlertDialog:

java.lang.NullPointerException
    at com.android.internal.app.WindowDecorActionBar.getDecorToolbar(WindowDecorActionBar.java:243)
    at com.android.internal.app.WindowDecorActionBar.init(WindowDecorActionBar.java:195)
    at com.android.internal.app.WindowDecorActionBar.__constructor__(WindowDecorActionBar.java:177)
    at com.android.internal.app.WindowDecorActionBar.<init>(WindowDecorActionBar.java)
    at android.app.Dialog.show(Dialog.java:317)
    at android.app.Dialog$GeneratedProxy/683649386.show(Unknown Source)
    at org.robolectric.shadows.ShadowDialog.show(ShadowDialog.java:64)
    at android.app.Dialog.show(Dialog.java)

Steps to Reproduce

Test showing alert dialog.

Robolectric & Android Version

We are on API 28 and Robolectric 4.3.

Link to a public git repo demonstrating the problem:

Sorry it is the company project. I will try to create some test project this week.

marcinbak commented 5 years ago

Quick answer: Usage of android.app.AlertDialog or passing proper themeResId to fragment fixes the problem.

I've encountered same issue and prepared a sample project showcasing the exception: https://github.com/marcinbak/robolectric-alertdialog-issue

During implementation I realised that the problem was caused by usage of androidx.appcompat.app.AlertDialog together with launchFragmentInContainer extension function without passing proper themeResId.