简体   繁体   中英

How to write a test suite with junit 5 for eclipse?

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: 这是我得到的错误

Thanks

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).

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>
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM