I want to convert the following (working) java test to Kotlin:
package my.project;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MyTempFileTest {
    @TempDir
    public File tempFolder;
    @Test
    public void testTempFolder() {
        Assertions.assertNotNull(tempFolder);
    }
    @Test
    public void testTempFolderParam(@TempDir File tempFolder) {
        Assertions.assertNotNull(tempFolder);
    }
 }
With IntelliJ's built in converter it becomes:
package my.project
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.io.File
class MyTempFileTest {
    @TempDir
    var tempFolder: File? = null
    
    @Test
    fun testTempFolder() {
        Assertions.assertNotNull(tempFolder)
    }
    @Test
    fun testTempFolderParam(@TempDir tempFolder: File?) {
        Assertions.assertNotNull(tempFolder)
    }
}
But this fails to initialize:
org.junit.jupiter.api.extension.ExtensionConfigurationException: @TempDir field [private java.io.File my.project.MyTempFileTest.tempFolder] must not be private.
Putting public in front of var however, makes no difference. I get the same error message and IntelliJ even suggests to remove the apparently 'redundant' public again.
Slightly more idiomatic solution from the Kotlin point of view that makes sure the type of the file property is non-nullable:
@field:TempDir
lateinit var tempFolder: File
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With