lateinit variable doesn't work with interceptTestCase
See original GitHub issueI have a problem with init lateinit var
property in interceptTestCase
method.
Here is my example:
class ExampleTest : StringSpec() {
private lateinit var someString: String
override fun interceptTestCase(context: TestCaseContext, test: () -> Unit) {
println("Start Testcase")
someString = "Hello"
test()
println("End Testcase")
}
init {
"Hello should equal to Hello" {
someString shouldEqual "Hello"
}
}
}
And here is out put:
Start Testcase
kotlin.UninitializedPropertyAccessException: lateinit property someString has not been initialized
at com.locata.myapplication.ExampleTest.access$getSomeString$p(ExampleTest.kt:7)
at com.locata.myapplication.ExampleTest$1.invoke(ExampleTest.kt:18)
at com.locata.myapplication.ExampleTest$1.invoke(ExampleTest.kt:7)
at io.kotlintest.Spec$runTest$callable$1$1.invoke(Spec.kt:124)
at io.kotlintest.Spec$runTest$callable$1$1.invoke(Spec.kt:15)
at io.kotlintest.Spec$runTest$initialInterceptor$1$1.invoke(Spec.kt:116)
at io.kotlintest.Spec$runTest$initialInterceptor$1$1.invoke(Spec.kt:15)
at com.locata.myapplication.ExampleTest.interceptTestCase(ExampleTest.kt:12)
at io.kotlintest.Spec$runTest$initialInterceptor$1.invoke(Spec.kt:116)
at io.kotlintest.Spec$runTest$initialInterceptor$1.invoke(Spec.kt:15)
at io.kotlintest.Spec$runTest$callable$1.call(Spec.kt:124)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Process finished with exit code 255
It’s happen for both 2.0.0 and 2.0.1.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:7
- Comments:15 (12 by maintainers)
Top Results From Across the Web
Kotlintest interceptor and lateinit vars - Stack Overflow
When I step through it with a debugger and set a breakpoint in the interceptTestCase methods I see that it is executed before...
Read more >[Solved]-Kotlintest interceptor and lateinit vars-kotlin
The problem is that you are accessing your lateinit var s inside init {} block, which is default constructor and it is called...
Read more >Initializing lazy and lateinit variables in Kotlin - LogRocket Blog
This article will explain how the lateinit modifier and lazy delegation can take care of unused or unnecessary early initializations.
Read more >Table of Contents - Micronaut Documentation
OpenRewrite works with Micronaut applications written in Java, but OpenRewrite doesn't currently support Kotlin or Groovy. Like any automated tool it does much ......
Read more >Kotlin Cookbook - NIBM eHub
when statement to be exhaustive even when it doesn't return a value, and ... mark it as lateinit, as in the test case...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Proposed solution works only with some specs.
works with StringSpec
but it fails to work with BehaviorSpec
kotlin.UninitializedPropertyAccessException: lateinit property profilePresenter has not been initialized
I strongly feel that this is really big issue, because it’s SUPER unintuitive. To be honest I don’t get the idea behind and the problem with
oneInstancePerTest
. Your docs say:oneInstancePerTest - By default a single instance of the test class is created for all the test it contains.
This seems fine for me. What is the problem by simply callinginterceptTestCase
before each test and initialize\reinitialize lateinit property before each test? How this relates to single/multiple instances of test class? Why exactly we need to disable it?Please solve this issue!
[Edit 10 mins later]
With this piece of code you can intercept the
() -> Unit
block like a middleware and run it before any variable initialization.The use case is:
This is a hotfix for this problem, you can rename the method to any other name…