1

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

user2038596
  • 535
  • 1
  • 3
  • 14
  • 1
    I cannot reproduce that error. – Sweeper Jan 04 '22 at 13:10
  • I can't reproduce it either, but I also couldn't try the extension... So I'm curious what happens when you remove the `@ExtendWith` annotation? – Abby Jan 04 '22 at 13:34
  • @Abby : thanks a lot for your feedback ! The problem wasn't ExtendWith annotation but rather with SpringBootTest annotation (but you pointed me into the right direction) . I modified my code example accordingly. – user2038596 Jan 06 '22 at 11:46

1 Answers1

1

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
    }
}
user2038596
  • 535
  • 1
  • 3
  • 14