Search code examples
kotlinjunit5tempdir

how to use Junit5 @TempDir with Kotlin ? ("JvmField can only be applied to final property" compile error)


I'm trying (without any luck so far) to use Junit5 @Tempdir annotation with Kotlin.
Following a previous stackoverflow post (link here), I've tried the code below:

@SpringBootTest
class MyClass {

    @TempDir
    @JvmField
    var tempFolder: File? = null
    
    @Test
    fun mytest() {
        assert(true);
    }

}

Unfortunately I get the following error at compilation: "JvmField can only be applied to final property"...
Any idea ?
Thanks a lot in advance for your expertise and your time.
Best Regards


Solution

  • For other people still looking for an answer, below code works around above-mentionned issue:

    @SpringBootTest
    class MyClass {
    
        @Test
        fun mytest() {
            assert(true);
        }
        
        companion object {
            @TempDir
            lateinit var tempFolder: File
        }
    }