使用 Kotlin 的 Junit5 测试套件 [英] Junit5 test suite with Kotlin

查看:66
本文介绍了使用 Kotlin 的 Junit5 测试套件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此链接中的回答,使用Test创建了一个测试套件班级

Based on answer in this link, created a test suite with Test classes

@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

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

But every test class added has test methods and runs individually for instance

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 引擎运行套件:

You can now run Suites purely with jUnit 5 engines:

import org.junit.platform.suite.api.SelectClasses
import org.junit.platform.suite.api.Suite

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

您必须导入junit-platform-suite-engine:

https://search.maven.org/artifact/org.junit.platform/junit-platform-suite-engine

这里有一些文档:

https://junit.org/junit5/docs/snapshot/user-guide/#launcher-api-engines-customhttps://junit.org/junit5/docs/snapshot/api/org.junit.platform.suite.engine/org/junit/platform/suite/engine/package-summary.html

以下是官方示例:

https://github.com/junit-team/junit5/blob/main/documentation/src/test/java/example/SuiteDemo.java

这篇关于使用 Kotlin 的 Junit5 测试套件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆