英文:
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中添加以下配置,但没有成功。
testOptions {
unitTests.all {
it.useJUnitPlatform()
}
}
尝试了所有这些后,我开始怀疑是否可行。
英文:
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.
testOptions {
unitTests.all {
it.useJUnitPlatform()
}
}
Trying all this I started wondering if this is doable or not.
答案1
得分: 1
以下是您要翻译的内容:
以下步骤适用于我:
a)在项目根目录的build.gradle中添加以下内容
依赖项
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20"
b)在您的应用级别的build.gradle中添加Kotlin插件
apply plugin: 'kotlin-android'
c)在您的单元测试包中创建一个测试类,确保类的名称以Test结尾。我尝试了以下类名
以Test结尾的类
状态
工作
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import junit.framework.TestCase.assertEquals
@RunWith(JUnit4::class)
class Kotlin1KotlinTest {
@Test
fun test1() {
val res = 1 + 1
assertEquals(2, res)
}
}
不以Test结尾的类
状态
不工作
获得
未接收到测试事件
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import junit.framework.TestCase.assertEquals
@RunWith(JUnit4::class)
class Kotlin1 {
@Test
fun test1() {
val res = 1 + 1
assertEquals(2, res)
}
}
英文:
Following steps worked for me
a) Add following in build.gradle at project root level in
> dependencies
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20"
b) Add kotlin plugin in build.gradle at your app level
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
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import junit.framework.TestCase.assertEquals
@RunWith(JUnit4::class)
class Kotlin1KotlinTest {
@Test
fun test1() {
val res = 1 + 1
assertEquals(2, res)
}
}
Named the class that does not ends with Test
status
> Not working
Got
> Test events were not received
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import junit.framework.TestCase.assertEquals
@RunWith(JUnit4::class)
class Kotlin1 {
@Test
fun test1() {
val res = 1 + 1
assertEquals(2, res)
}
}
答案2
得分: 0
这是一个具有Java类和Kotlin测试的简单应用程序示例。
Java类
路径:src/main/java/MyClass.java
public class MyClass {
public int addNumbers(int a, int b) {
System.out.println("正在计算...");
return a + b;
}
}
Kotlin测试
路径:src/test/kotlin/MyClassTest.kt
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
class MyClassTest {
@Test
fun test() {
assertEquals(3, MyClass().addNumbers(1, 2))
}
}
Gradle文件(Kotlin)
plugins {
kotlin("jvm") version "1.8.21"
id("java")
}
group = "com.example"
version = "1.0"
repositories {
mavenCentral()
}
dependencies {
// JUnit 5的依赖项
testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.2")
// 用于测试中支持Kotlin的依赖项
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:1.8.21")
}
tasks.test {
useJUnitPlatform()
}
英文:
here is an example of a simple application with java classes and kotlin tests.
Java class
Path: src/main/java/MyClass.java
public class MyClass {
public int addNumbers(int a, int b) {
System.out.println("Calculating...");
return a + b;
}
}
Kotlin test
Path: src/test/kotlin/MyClassTest.kt
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
class MyClassTest {
@Test
fun test() {
assertEquals(3, MyClass().addNumbers(1, 2))
}
}
Gradle file (Kotlin)
plugins {
kotlin("jvm") version "1.8.21"
id("java")
}
group = "com.example"
version = "1.0"
repositories {
mavenCentral()
}
dependencies {
// Зависимости для JUnit 5
testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.2")
// Зависимость для поддержки Kotlin в тестах
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:1.8.21")
}
tasks.test {
useJUnitPlatform()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论