我们可以为 Android 项目中的 Java 类编写 Kotlin 的 JUnit 测试用例吗?

huangapple go评论101阅读模式
英文:

Can we write junit test cases in Kotlin for the Java classes in an android project?

问题

我期待着将一个传统项目从Java迁移到Kotlin,显然缺少单元测试案例。

我为一个Java编写的Util类编写了Kotlin测试案例,但在运行时遇到了"Tests events were not received"的问题。经过大量搜索和尝试各种Stack Overflow的答案后,我仍然无法通过这个问题。

如果我在一个Java类中编写相同的测试案例,那么所有的测试都会按预期工作,并生成一个适当的报告。

我想提一下几件事:

  • 没有编译时错误,构建成功。
  • 我使用的是junit:4.13.2
  • Android Studio版本是Dolphin | 2021.3.1 Patch 1
  • 测试类位于app/src/test/java
  • 尝试将其放在app/src/test/kotlin下,但没有效果
  • 尝试在build.gradle中添加以下配置,但没有成功。
  1. testOptions {
  2. unitTests.all {
  3. it.useJUnitPlatform()
  4. }
  5. }

尝试了所有这些后,我开始怀疑是否可行。

英文:

I am looking forward to migrate a legacy project form Java to Kotlin, which apparently lacks unit test cases.

I wrote test cases in kotlin for a Util class which is written in Java, but when running it I am facing "Tests events were not received". After a lot of searching and trying various SO answers I am still not able to get pass through this.

If i write the same test cases in a Java class then all tests work as expected with a proper report.

Couple of things i wanted to mention:

  • There are no build time errors and the build is successful.
  • I am using junit:4.13.2
  • Android Studio version is Dolphin | 2021.3.1 Patch 1
  • The test class is under app/src/test/java
  • Tried keeping it under app/src/test/kotlin too but doesn't work
  • Tried adding below config under build.gradle but no luck.
  1. testOptions {
  2. unitTests.all {
  3. it.useJUnitPlatform()
  4. }
  5. }

Trying all this I started wondering if this is doable or not.

答案1

得分: 1

以下是您要翻译的内容:

以下步骤适用于我:

a)在项目根目录的build.gradle中添加以下内容

依赖项

  1. classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20"

b)在您的应用级别的build.gradle中添加Kotlin插件

  1. apply plugin: 'kotlin-android'

c)在您的单元测试包中创建一个测试类,确保类的名称以Test结尾。我尝试了以下类名

以Test结尾的类

状态

工作

  1. import org.junit.Test
  2. import org.junit.runner.RunWith
  3. import org.junit.runners.JUnit4
  4. import junit.framework.TestCase.assertEquals
  5. @RunWith(JUnit4::class)
  6. class Kotlin1KotlinTest {
  7. @Test
  8. fun test1() {
  9. val res = 1 + 1
  10. assertEquals(2, res)
  11. }
  12. }

不以Test结尾的类

状态

不工作

获得

未接收到测试事件

  1. import org.junit.Test
  2. import org.junit.runner.RunWith
  3. import org.junit.runners.JUnit4
  4. import junit.framework.TestCase.assertEquals
  5. @RunWith(JUnit4::class)
  6. class Kotlin1 {
  7. @Test
  8. fun test1() {
  9. val res = 1 + 1
  10. assertEquals(2, res)
  11. }
  12. }
英文:

Following steps worked for me

a) Add following in build.gradle at project root level in

> dependencies

  1. classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20"

b) Add kotlin plugin in build.gradle at your app level

  1. apply plugin: 'kotlin-android'

c) Create a test class in your unit test package, make sure that name of class ends with Test. I tried with following name for classes

Named the class ends with Test

status
> Worked

  1. import org.junit.Test
  2. import org.junit.runner.RunWith
  3. import org.junit.runners.JUnit4
  4. import junit.framework.TestCase.assertEquals
  5. @RunWith(JUnit4::class)
  6. class Kotlin1KotlinTest {
  7. @Test
  8. fun test1() {
  9. val res = 1 + 1
  10. assertEquals(2, res)
  11. }
  12. }

Named the class that does not ends with Test

status
> Not working

Got

> Test events were not received

  1. import org.junit.Test
  2. import org.junit.runner.RunWith
  3. import org.junit.runners.JUnit4
  4. import junit.framework.TestCase.assertEquals
  5. @RunWith(JUnit4::class)
  6. class Kotlin1 {
  7. @Test
  8. fun test1() {
  9. val res = 1 + 1
  10. assertEquals(2, res)
  11. }
  12. }

答案2

得分: 0

这是一个具有Java类和Kotlin测试的简单应用程序示例。

Java类

路径:src/main/java/MyClass.java

  1. public class MyClass {
  2. public int addNumbers(int a, int b) {
  3. System.out.println("正在计算...");
  4. return a + b;
  5. }
  6. }

Kotlin测试

路径:src/test/kotlin/MyClassTest.kt

  1. import org.junit.jupiter.api.Test
  2. import kotlin.test.assertEquals
  3. class MyClassTest {
  4. @Test
  5. fun test() {
  6. assertEquals(3, MyClass().addNumbers(1, 2))
  7. }
  8. }

Gradle文件(Kotlin)

  1. plugins {
  2. kotlin("jvm") version "1.8.21"
  3. id("java")
  4. }
  5. group = "com.example"
  6. version = "1.0"
  7. repositories {
  8. mavenCentral()
  9. }
  10. dependencies {
  11. // JUnit 5的依赖项
  12. testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.2")
  13. testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.2")
  14. // 用于测试中支持Kotlin的依赖项
  15. testImplementation("org.jetbrains.kotlin:kotlin-test-junit:1.8.21")
  16. }
  17. tasks.test {
  18. useJUnitPlatform()
  19. }

项目屏幕截图

英文:

here is an example of a simple application with java classes and kotlin tests.

Java class

Path: src/main/java/MyClass.java

  1. public class MyClass {
  2. public int addNumbers(int a, int b) {
  3. System.out.println("Calculating...");
  4. return a + b;
  5. }
  6. }

Kotlin test

Path: src/test/kotlin/MyClassTest.kt

  1. import org.junit.jupiter.api.Test
  2. import kotlin.test.assertEquals
  3. class MyClassTest {
  4. @Test
  5. fun test() {
  6. assertEquals(3, MyClass().addNumbers(1, 2))
  7. }
  8. }

Gradle file (Kotlin)

  1. plugins {
  2. kotlin("jvm") version "1.8.21"
  3. id("java")
  4. }
  5. group = "com.example"
  6. version = "1.0"
  7. repositories {
  8. mavenCentral()
  9. }
  10. dependencies {
  11. // Зависимости для JUnit 5
  12. testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.2")
  13. testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.2")
  14. // Зависимость для поддержки Kotlin в тестах
  15. testImplementation("org.jetbrains.kotlin:kotlin-test-junit:1.8.21")
  16. }
  17. tasks.test {
  18. useJUnitPlatform()
  19. }

Project screen

huangapple
  • 本文由 发表于 2023年6月15日 00:03:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定