即使在androidTest包中,Kotlin类也会作为junit测试运行 – 对于Java类,它运行正常

我遇到了非常奇怪的错误。

当我尝试运行测试,在我的Kotlin类的androidTest包,他们正在运行测试junit mehthods,并出现此错误:

处理完成退出代码1未find类:“com.someampp.shoppinglistapp.SomeClassTest”空测试套件。

你可以自己尝试。 我正在使用Android Studio 3.0.1

当我在Java中创建这样的类时:

@RunWith(AndroidJUnit4.class) public class SomeTestClass{ @Test public void useAppContext() throws Exception { // Context of the app under test. Context appContext = InstrumentationRegistry.getTargetContext(); assertEquals("com.myapp.shoppinglistapp", appContext.getPackageName()); } } 

一切工作正常。

但是,当我将Java文件转换为Kotlin:

 @RunWith(AndroidJUnit4::class) class SomeTestClass{ @Test @Throws(Exception::class) fun useAppContext() { // Context of the app under test. val appContext = InstrumentationRegistry.getTargetContext() assertEquals("com.myapp.shoppinglistapp", appContext.packageName) } } 

它给了我这个错误。

怎么了?

您需要打开Run/Debug configurations并为Android Instrumentation测试而不是Android JUnit创建一个。