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

问题描述 投票:1回答:1

我遇到了很奇怪的错误。

当我尝试在androidTest包中运行我的Kotlin类中的测试时,它们作为测试junit mehthods运行并出现此错误:

进程已完成退出代码1未找到类:“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)
  }
}

它给了我这个错误。

怎么了?

java android junit kotlin android-testing
1个回答
2
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.