带有Kotlin的Junit5测试套件

问题描述

根据this link中的答案,创建具有测试类的测试套件

@RunWith(Suite::class)
@Suite.SuiteClasses(
    DTOtoEntityMapperTest::class,PostDataSourceCoroutinesTest::class,PostDataSourceRxJava3Test::class
)
class junit5TestSuite

返回错误

org.junit.runners.model.InvalidTestClassError: Invalid test class 'com.x.DTOtoEntityMapperTest':
  1. No runnable methods

但是添加的每个测试类都有测试方法,并且分别运行例如

class DTOtoEntityMapperTest {

    private val postDTOList by lazy {
        convertFromJsonToObjectList<PostDTO>(getResourceAsText(RESPONSE_JSON_PATH))!!
    }

    private val postEntityList by lazy {
        convertFromJsonToObjectList<PostEntity>(getResourceAsText(RESPONSE_JSON_PATH))!!
    }

    @Test
    fun `given PostDTO is input,should return PostEntity`() {

        val mapper = DTOtoEntityMapper()

        // GIVEN
        val expected = postEntityList

        // WHEN
        val actual = mapper.map(postDTOList)

        // THEN
        Truth.assertthat(actual).containsExactlyElementsIn(expected)
    }
}

解决方法

对于 JUnit 5,我使用了 JUnitPlatform 和 SelectClasses(或 SelectPackages):

@RunWith(JUnitPlatform::class)
@SelectClasses(Some1Test::class,Some2Test::class)
class SuiteTest
,

经过几天的检查,我发现正确的库可以使用带有 kotlin 的 Junit 5 测试套件

在 build.gradle 中,添加如下 2 个库:

testImplementation ("org.junit.platform:junit-platform-suite-api:1.7.0")
testImplementation ("org.junit.platform:junit-platform-runner:1.2.0")

然后您可以使用如下测试套件:

import org.junit.platform.runner.JUnitPlatform
import org.junit.platform.suite.api.SelectClasses
import org.junit.platform.suite.api.SelectPackages
import org.junit.runner.RunWith


@RunWith(JUnitPlatform::class)
@SelectClasses(BasicMockKUnitTest::class,HierarchicalMockKUnitTest::class)
@SelectPackages("exercise")
class TestAllSelectPackage {
}