如何在Kotlin中使用JUnit5的TempDir? [英] How to use Junit 5's TempDir with Kotlin?
本文介绍了如何在Kotlin中使用JUnit5的TempDir?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将以下(正在运行的)Java测试转换为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);
}
}
有了IntelliJ的内置转换器,它就变成:
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)
}
}
但无法初始化:
org.junit.jupiter.api.extension.ExtensionConfigurationException: @TempDir field [private java.io.File my.project.MyTempFileTest.tempFolder] must not be private.
但是,将public
放在var
前面并没有什么不同。我收到了同样的错误信息,IntelliJ甚至建议再次删除明显‘多余’的public
。
推荐答案
您必须使用@JvmField
注释该字段,以便kotlin编译器生成一个实际的公共字段,而不是Getter和Setter:
@TempDir
@JvmField
var tempFolder: File? = null
这篇关于如何在Kotlin中使用JUnit5的TempDir?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文