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.
You have to annotate the field with @JvmField
so that the Kotlin compiler generates an actual public field, instead of getter and setter:
@TempDir
@JvmField
var tempFolder: File? = null