0

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

Returns error

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)
    }
}
Thracian
  • 43,021
  • 16
  • 133
  • 222
  • is your test annotation a junit 5 @Test annotation, is it of type org.junit.jupiter.api.Test? – Daniel Jacob Aug 24 '20 at 08:33
  • 1
    @DanielJacob All tests are JUnit5 tests, it's org.junit.jupiter.api.Test – Thracian Aug 24 '20 at 08:33
  • 1
    RunWith and Suite are JUnit 4 mechanisms that do not work with JUnit 5. Nowhere in the answer you’ve linked is your approach being suggested. – johanneslink Aug 24 '20 at 12:07
  • @johanneslink how can i run JUnit5 tests using a test suite? Yeah, i checked it the answer again, my mistake i thought **It cannot be used when running on the JUnit Platform. You should be able to use the Suite runner, though.** was refering to running with JUnit5 test. Can you provide an answer how to run tests with Junit5 test suite in Kotlin? – Thracian Aug 24 '20 at 12:18
  • @Thracian Hand-crafted test suites are not a supported concept in JUnit 5. You can get around that by using RunWith(JUnitPlatform) but then you are using JUnit 4 to run JUnit 5. This construct was created as a workaround for the days when build tools and IDEs did not support JUnit 5. What is it you want to achieve with the suite? – johanneslink Aug 24 '20 at 16:21
  • @johanneslink i want to run all of the tests located in a module using a single file. I have over 15 test classes and more about 100 tests, including nested ones. What do you mean by **but then you are using JUnit 4 to run JUnit 5**, i can still use test suites with `RunWith(JUnitPlatform)`? I tried adding that to class i defined as test suite but AS couldn't find dependency. I assume from your comment if i add JUnit4 dependency i can run my JUnit5 tests with a test suite, right? – Thracian Aug 24 '20 at 16:28
  • @Thracian JUnit 5 has the feature to run all tests of a module (or a package or a class) built in. You do not need suites for that. How you invoke that depends on IDE or build tool you are using. – johanneslink Aug 25 '20 at 06:05
  • @Thracian "i want to run all of the tests located in a module using a single file." Why exactly you need **one** file? If you want to run all test in intellij, just right click on the base package and run all unit tests. If you want to run using build tool (gradle/maven) it does run **all** test automatically, don't need to specify files. The only real use-case for Suites is when you want to run tests in **parallel** groups with different contexts (e.g. spring `@DataJpaTest`, `@JsonTest`) – r6q Aug 25 '20 at 09:50

3 Answers3

3

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 {
}

You'll have to import the junit-platform-suite-engine:

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

There's some docs here:

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

Here's an official example:

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

Nico
  • 169
  • 6
1

For JUnit 5 I used JUnitPlatform and SelectClasses (or SelectPackages):

@RunWith(JUnitPlatform::class)
@SelectClasses(Some1Test::class, Some2Test::class)
class SuiteTest
dim42
  • 991
  • 7
  • 10
  • I'm want to use test suite with junit5 and kotlin. could you please let to know which library can I import to able to use SelectClasses and SelectPackages annotation? – Trung Đoan Jul 08 '21 at 12:32
  • You need module "junit-platform-suite-api" of JUnit 5. – dim42 Jul 12 '21 at 18:57
1

After checking for a few days, I found correctly libs to use Junit 5 test suite with kotlin

In build.gradle, add 2 libs as below:

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

Then you can use test suite as below:

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 {
}
Trung Đoan
  • 643
  • 7
  • 18