Spek: Test fails with RuntimeException Stub!

Created on 2 Jun 2020  路  17Comments  路  Source: spekframework/spek

We were writing test that references some android classes, custom views that extend from the Views class.

Stub!
java.lang.RuntimeException: Stub!
    at android.view.View.<init>(Unknown Source)
    at android.view.ViewGroup.<init>(ViewGroup.java:89)
    at androidx.constraintlayout.widget.ConstraintLayout.<init>(ConstraintLayout.java:580)
    at sg.com.sph.android.card.views.BaseView.<init>(BaseView.kt:10)
    at sg.com.sph.android.card.views.BaseCardView.<init>(BaseCardView.kt:13)
    at sg.com.sph.android.card.views.RegularArticleCardView.<init>(RegularArticleCardView.kt:29)
    at sg.com.sph.android.card.views.RegularArticleCardView.<init>(RegularArticleCardView.kt:28)
    at sg.com.sph.android.card.views.RegularArticleCardView.<init>(RegularArticleCardView.kt:24)
    at sg.com.sph.appbt.views.feed.viewholders.viewholderfactory.ViewHolderFactory.make(ViewHolderFactory.kt:50)
    at sg.com.sph.appbt.views.feed.viewholders.viewholderfactory.ViewHolderFactoryTest$1$3$3.invoke(ViewHolderFactoryTest.kt:79)
    at sg.com.sph.appbt.views.feed.viewholders.viewholderfactory.ViewHolderFactoryTest$1$3$3.invoke(ViewHolderFactoryTest.kt:48)
    at org.spekframework.spek2.runtime.scope.TestScopeImpl.execute(scopes.kt:136)
    at org.spekframework.spek2.runtime.Executor$execute$result$2$exception$1$job$1.invokeSuspend(Executor.kt:74)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:740)

Android Studio 3.6.2 and Spek 2 was working, the plugin info:
2.0.10-Studio3.6 - ok
2.0.11-Studio3.6 - ok
2.0.11-IJ2019.2 - exception, not working (it was listed as compatible, is it actually not?)

Android Studio 4.0 both available plugins are not working and throws the same exception.
2.0.11-IJ2019.3 - exception
2.0.10-IJ2019.3 - exception
was reading about it that android.jar is returning the exception during test but I have already set:

testOptions {
        unitTests.returnDefaultValues = true
    }

Could anyone suggest what can I do regarding this?

triage

Most helpful comment

Attached is a 4.0 build, can you guys try it out? Forgot to mention, anything without the -StudioX.Y suffix is not compatible with android studio. Plugins for AS has some special handling to stub out android.jar.

2.0.12-alpha.0.0+20200606T014723Z-Studio4.0.zip

All 17 comments

I'll provide a build for AS 4.0, hopefully it's not a big change plugin wise. Btw, does running in the CLI (gradle) yield the same result?

It seems that CLI ./gradlew testAppDebug works just fine for me. I get the above mentioned exception if run via IDE.
I am using Android Studio 3.6.3

Okay, can you share a sample project? I'll do a build for AS 4 and see if it works.

Attached is a 4.0 build, can you guys try it out? Forgot to mention, anything without the -StudioX.Y suffix is not compatible with android studio. Plugins for AS has some special handling to stub out android.jar.

2.0.12-alpha.0.0+20200606T014723Z-Studio4.0.zip

Forgot to mention, anything without the -StudioX.Y suffix is not compatible with android studio

This was it. My Android Studio automatically updated to -IJXXXX version of the plugin. When manually installed 2.0.11-Studio3.6 it works just fine.

I wonder if it is possible to prevent this situation in the future.

One way to do it is to make version compat of the IJ plugin to be more specific. At the moment, version is just from 193.1 until 193.* which is "compatible" with android studio (but not really). I could change it to193.WXYZ.1 until193.WXYZ.* which should make it more specific to a particular build of IJ. The problem is WXYZ can vary between releases (even minor ones IIRC) which makes maintaining the plugins a bit hectic.

Attached is a 4.0 build, can you guys try it out? Forgot to mention, anything without the -StudioX.Y suffix is not compatible with android studio. Plugins for AS has some special handling to stub out android.jar.

2.0.12-alpha.0.0+20200606T014723Z-Studio4.0.zip

This fix works! thanks a lot.

One way to do it is to make version compat of the IJ plugin to be more specific. At the moment, version is just from 193.1 until 193.* which is "compatible" with android studio (but not really). I could change it to193.WXYZ.1 until193.WXYZ.* which should make it more specific to a particular build of IJ. The problem is WXYZ can vary between releases (even minor ones IIRC) which makes maintaining the plugins a bit hectic.

Ahh.. thanks for the clarification. Probably for now the studio suffix is required I guess.

One way to do it is to make version compat of the IJ plugin to be more specific. At the moment, version is just from 193.1 until 193.* which is "compatible" with android studio (but not really). I could change it to193.WXYZ.1 until193.WXYZ.* which should make it more specific to a particular build of IJ. The problem is WXYZ can vary between releases (even minor ones IIRC) which makes maintaining the plugins a bit hectic.

This or using the plugin can be hectic around automatic updates. Maybe there is another way but I am no expert in how IntelliJ/AS handle plugin versioning.

@dariuszseweryn not really, this problem only happens when a major version of AS (which happens twice a year?) is released and I haven't prepared a proper build for it (hence why it's pulling the IJ build of the plugin). Compared to having to publish a new build every time AS and IJ updates (even with patch releases).

Does it change anything that I still use AS 3.6.3 (did not updated to 4.0) there apparently is plugin version for -Studio3.6 but I got upgraded to -IJ anyway?

That is indeed weird, if that's the case the only thing I can think of that can cause it is internal build number (WXYZ from before) has changed in 3.6.3.

Android Studio 3.6.3
Build #AI-192.7142.36.36.6392135, built on April 14, 2020
Runtime version: 1.8.0_212-release-1586-b4-5784211 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.15.5
GC: ParNew, ConcurrentMarkSweep
Memory: 3005M
Cores: 8
Registry: ide.new.welcome.screen.force=true
Non-Bundled Plugins: Lombook Plugin, org.intellij.plugins.markdown, org.jetbrains.kotlin, PythonCore, org.intellij.scala, org.spekframework

@raniejade I was having similar failures in our unit test after updating Spek plugin to .0.12-alpha.0.0+20200606T014723Z-Studio4.0.zip all those problems are fixed.

Is there any ETA when this plugin will get pushed to official plugin.

Thanks for the fix @raniejade

@snijsure most likely in two weeks time, I'm moving at the moment so I really don't have time to publish a release.

@raniejade so is there a plugin version that works with Android Studio 4.1 - tried 2.0.13-IJ2020.1 and it fails with RuntimeException.

Was this page helpful?
0 / 5 - 0 ratings