3

I am trying to run by junit5 tests from a test suite. however Im getting an error

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

import com.test.studentservice.controllers.StudentControllerTest2;
import com.test.studentservice.repo.StudentServiceRepoDataTest;
import com.test.studentservice.service.StudentServiceTest;

@RunWith(JUnitPlatform.class)
@SelectClasses({StudentControllerTest2.class, StudentServiceRepoDataTest.class, StudentServiceTest.class})
public class StudentServiceTestSuite {

}

This is the error I'm getting: this is the error im getting

Thanks

howlger
  • 31,050
  • 11
  • 59
  • 99
  • A common mistake in JUnit5 is to tag the test methods with `org.junit.Test` instead of `org.junit.jupiter.api.Test`. You can right-click a package and choose _Run As > JUnit Test_, so you don't need to write code to list all test classes of a package. If this does not help, tell whether you can run a class as JUnit test and show a minimal example containing also the code of the test class. – howlger Feb 03 '21 at 06:48
  • @howlger Thanks, right-clicking on the package and running the jUnit test is working fine. however I need to have a test suite as per the requirement. i have re-checked and im using `org.junit.jupiter.api.Test` for every test. im following https://www.baeldung.com/junit-5 test suite example – Chamidu Sumanasekara Feb 03 '21 at 11:28

2 Answers2

1

You get this error, because in the JUnit launch configuration (Run > Run Configurations...) in the Test tab, you selected as Test runner the option JUnit 5 instead of JUnit 4.

@RunWith(JUnitPlatform.class) is used for the purpose to run JUnit 5 tests as JUnit 4 tests, in case a tool only supports JUnit 4 but not JUnit 5 (for details see this answer). But you don't need that since Eclipse supports JUnit 5 and should not use it since running it via the JUnit 4 API has some limitations.

Baeldung - A Guide to JUnit 5 - 7. Test Suites which you mentioned in a comment you are following here, is wrong, or at least misleading in this point. Instead of a JUnit Test Suite class, tag your tests and use them to include or to exclude tests (in the JUnit launch configuration, in the Test tab, hit the Configure... button for that).

howlger
  • 31,050
  • 11
  • 59
  • 99
1

From October-November 2021 jUnit5 support @Suite annotation:

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

    @Suite
    @SuiteDisplayName("Build Verification Test")
    // Insert the class names in the order of execution
    @SelectClasses({
            FirstTest.class,
            SecondTest.class
    })
    public class SuiteTest {
        
    }

Pom:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit.jupiter.version>5.8.0</junit.jupiter.version>
    <junit.platform.version>1.8.0</junit.platform.version>
</properties>
    ...
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-suite</artifactId>
        <version>${junit.platform.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-suite-api</artifactId>
        <version>${junit.platform.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-suite-commons</artifactId>
        <version>${junit.platform.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-suite-engine</artifactId>
        <version>${junit.platform.version}</version>
        <scope>test</scope>
    </dependency>
    ...
Alex T
  • 11
  • 1