TransWikia.com

java.lang.InstantiationException: java.lang.Class cannot be instantiated

Stack Overflow Asked on November 16, 2021

I got following error when executed my unit test.

java.lang.InstantiationException: java.lang.Class<com.zhixin.wedeep.homepage.HiltTestApplication> cannot be instantiated
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateApplication(AppComponentFactory.java:76)
        at androidx.core.app.CoreComponentFactory.instantiateApplication(CoreComponentFactory.java:52)
        at android.app.Instrumentation.newApplication(Instrumentation.java:1158)
        at com.zhixin.wedeep.homepage.MockTestRunner.newApplication(MockTestRunner.kt:10)
        at android.app.LoadedApk.makeApplication(LoadedApk.java:1208)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6533)
        at android.app.ActivityThread.access$1300(ActivityThread.java:231)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1887)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7478)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)

My HiltTestApplication

@CustomTestApplication(BaseApplication::class)
interface HiltTestApplication

My BaseApplication

open class BaseApplication : Application() {


    override fun onCreate() {
        super.onCreate()
        mInstance = this
        init()
    }

    /**
     * 初始化
     */
    protected fun init() {}

    fun initApiClient() {}

    companion object {
        private var mInstance: Application? = null
        fun getInstance(): Application? {
            return mInstance

        }
    }
}

Any ideas for this problem?

One Answer

I ran into a similar exception when trying to run an espresso test, the problem might actually be with the test runner class you have configured in espresso.


Not Working

class CustomTestRunner : AndroidJUnitRunner() {
    override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
        return super.newApplication(cl, HiltTestApplication::class.java.name, context)
    }
}

Working

class CustomTestRunner : AndroidJUnitRunner() {
    override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
        return super.newApplication(cl, HiltTestApplication_Application::class.java.name, context)
    }
}

The Hilt documentation for custom test applications mentions that the @CustomTestApplication will generate a HiltTestApplication with a _Application appended to the interface name.

// Generates MyCustom_Application.class
@CustomTestApplication(MyBaseApplication::class)
interface MyCustom

Answered by Andrew Steinmetz on November 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP