5

as we are upgrading to junit5 from junit4 for unit testing purpose, i couldn't able to find solution to creating a test suite in junit5 like in junit4 : below is the our junit4 suite class :

@RunWith(Suite.class)
@SuiteClasses({ 
    ClassA.class, 
    ClassB.class, 
    ClassC.class 
    } )
public class TestSuite {

}

The Code i tried after searching around is below :

@RunWith(JUnitPlatform.class)

@SelectClasses( { 
    OrderAnnotationAlphanumericExperiment.class, 
    orderAnnotationExperiment.class, 
    ParameterizedAnnotation.class 
    } )
public class TestSuite {
    

}

on searching for the solution, most of them were providing a solution of using the same test suite to run it using vintage api, but what am looking for is to create test suite in junit 5.

Few suggesting @ExtendsWith(SpringExtension.class), but there is also less documentation for it, couldn't able to find a solution to create a suite in junit 5

few blogs/question/sites i referred :

Create TestSuite in JUnit5 (Eclipse) Are test suites considered deprecated in JUnit5? https://howtodoinjava.com/junit5/junit5-test-suites-examples/

some one help me out to solve this problem.

Dhuruvan
  • 91
  • 10
  • Does this help: https://junit.org/junit5/docs/current/user-guide/#migrating-from-junit4-tips ? _"@RunWith no longer exists; superseded by @ExtendWith."_ – Fildor Jul 03 '20 at 08:01
  • `@ExtendWith(SpringExtension.class) @SelectClasses( { OrderAnnotationAlphanumericExperiment.class, orderAnnotationExperiment.class, ParameterizedAnnotation.class } ) public class TestSuite {}` Now i have tried this one! version of junit-jupiter-engine : 5.5.0 i have the following dependency still it throws error : *java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: org/junit/platform/engine/EngineDiscoveryListener* – Dhuruvan Jul 03 '20 at 08:16
  • I believe that the second code example you have is correct, according to the documentation: https://junit.org/junit5/docs/current/user-guide/#running-tests-junit-platform-runner-test-suite But as it is stated in the documentation: "Test classes and suites annotated with @RunWith(JUnitPlatform.class) cannot be executed directly on the JUnit Platform (or as a "JUnit 5" test as documented in some IDEs). Such classes and suites can only be executed using JUnit 4 infrastructure." – Igorski Jul 11 '20 at 11:21
  • Does this answer your question? [Are test suites considered deprecated in JUnit5?](https://stackoverflow.com/questions/50565724/are-test-suites-considered-deprecated-in-junit5) – Mahozad Sep 13 '21 at 19:34

1 Answers1

1

It's actually pretty easy. The following will find all JUnit tests in directory foo.bar.tests as well as it's subdirectories:

import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.SuiteDisplayName;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
@SuiteDisplayName("JUnit Platform Suite Demo")
@SelectPackages("foo.bar.test")
public class FullTestSuite 

}

See Sec 4.4.4 of the User's Guide.

LJ in NJ
  • 196
  • 1
  • 1
  • 12
  • This actually runs the test with the JUnit 4 engine, i.e. you'll need the `org.junit.vintage:junit-vintage-engine` dependency. – rü- Apr 09 '21 at 03:25