JUnit 4 based Runner which runs tests on the JUnit Platform in a JUnit 4 environment
npx @tessl/cli install tessl/maven-org-junit-platform--junit-platform-runner@1.12.0JUnit Platform Runner is a JUnit 4 based Runner that allows tests to be executed on the JUnit Platform in a JUnit 4 environment. It serves as a bridge component enabling JUnit 5 (Jupiter) tests to run in environments that support JUnit 4 but do not yet support the JUnit Platform directly.
⚠️ Deprecated Notice: This module is deprecated since JUnit Platform 1.8 in favor of junit-platform-suite-engine with @Suite annotation support, and is scheduled for removal in JUnit Platform 2.0.
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.12.2</version>
</dependency>import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runner.notification.RunNotifier;
import org.junit.runner.Description;Additional imports for configuration:
import org.junit.platform.suite.api.*;import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.platform.runner.JUnitPlatform;
@RunWith(JUnitPlatform.class)
public class MyJUnit5TestClass {
@Test
void shouldPassTest() {
// JUnit 5 test logic
}
@Test
void anotherTest() {
// Another JUnit 5 test
}
}import org.junit.runner.RunWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.*;
@RunWith(JUnitPlatform.class)
@SuiteDisplayName("My Test Suite")
@SelectPackages("com.example.tests")
@ExcludeTags("slow")
public class MyTestSuite {
// Empty class body - configuration is done via annotations
}The JUnit Platform Runner serves as a bridge between JUnit 4 and the JUnit Platform, enabling JUnit 5 tests to run in JUnit 4 environments:
Runner interface to integrate with existing build tools and IDEsjunit-platform-suite-apiThe runner discovers tests using JUnit Platform mechanisms, builds an internal test tree representation, then executes tests while translating events to JUnit 4's RunNotifier system for compatibility with existing tooling.
The main Runner class that enables JUnit Platform tests to run in JUnit 4 environments.
/**
* JUnit 4 based Runner which runs tests on the JUnit Platform in a JUnit 4 environment.
* Annotating a class with @RunWith(JUnitPlatform.class) allows it to be run with IDEs
* and build systems that support JUnit 4 but do not yet support the JUnit Platform directly.
*
* Note: Test classes and suites annotated with @RunWith(JUnitPlatform.class) cannot be
* executed directly on the JUnit Platform and can only be executed using JUnit 4 infrastructure.
*
* @since 1.0
* @deprecated since 1.8, in favor of the @Suite support provided by junit-platform-suite-engine;
* to be removed in JUnit Platform 2.0
* @status DEPRECATED
*/
@Deprecated
public class JUnitPlatform extends Runner implements Filterable {
/**
* Creates a new JUnitPlatform runner for the specified test class.
* @param testClass the test class to run; must be public to be picked up by IDEs and build tools
*/
public JUnitPlatform(Class<?> testClass);
/**
* Returns the suite description for the test tree.
* @return Description object representing the test suite
*/
@Override
public Description getDescription();
/**
* Executes the tests using the provided RunNotifier.
* @param notifier the RunNotifier to report test execution events
*/
@Override
public void run(RunNotifier notifier);
/**
* Applies a filter to limit which tests are executed.
* @param filter the Filter to apply
* @throws NoTestsRemainException if no tests remain after filtering
*/
@Override
public void filter(Filter filter) throws NoTestsRemainException;
}Usage Examples:
// Basic test class usage - must be public
@RunWith(JUnitPlatform.class)
public class SimpleTestClass {
@Test
void testMethod() {
// test implementation
}
}
// Test suite with package selection and tag filtering
@RunWith(JUnitPlatform.class)
@SelectPackages({"com.example.unit", "com.example.integration"})
@ExcludeTags("slow")
@IncludeTags("important | critical") // Tag expression with OR
public class ComprehensiveTestSuite {
}
// Configuration with parameters
@RunWith(JUnitPlatform.class)
@SelectClasses({DatabaseTest.class, NetworkTest.class})
@ConfigurationParameter(key = "junit.jupiter.execution.parallel.enabled", value = "true")
@ConfigurationParameter(key = "junit.jupiter.execution.parallel.mode.default", value = "concurrent")
public class ParallelTestSuite {
}Annotations for selecting which tests to include in the test run.
/**
* Select specific test classes to include
* @since 1.0
* @status MAINTAINED
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface SelectClasses {
/** One or more classes to select */
Class<?>[] value() default {};
/**
* One or more classes to select by their fully qualified names
* @since 1.10
* @status EXPERIMENTAL
*/
String[] names() default {};
}
/**
* Select tests from specific packages
* @since 1.0
* @status MAINTAINED
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface SelectPackages {
String[] value();
}
/**
* Select tests from classpath resources
* @since 1.0
* @status MAINTAINED
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface SelectClasspathResource {
String value();
}
/**
* Select tests from specific directories
* @since 1.0
* @status MAINTAINED
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface SelectDirectories {
String[] value();
}
/**
* Select tests from specific files
* @since 1.0
* @status MAINTAINED
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface SelectFile {
String value();
}
/**
* Select tests from specific modules
* @since 1.0
* @status MAINTAINED
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface SelectModules {
String[] value();
}
/**
* Select tests using URIs
* @since 1.0
* @status MAINTAINED
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface SelectUris {
String[] value();
}Annotations for filtering tests based on various criteria.
/**
* Include tests matching class name patterns
* @since 1.0
* @status MAINTAINED
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface IncludeClassNamePatterns {
/** Default pattern matches Test classes/methods */
String[] value() default "^(Test.*|.+[.$]Test.*|.*Tests?)$";
}
/**
* Exclude tests matching class name patterns
* @since 1.0
* @status MAINTAINED
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface ExcludeClassNamePatterns {
String[] value();
}
/**
* Include specific test engines
* @since 1.0
* @status MAINTAINED
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface IncludeEngines {
String[] value();
}
/**
* Exclude specific test engines
* @since 1.0
* @status MAINTAINED
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface ExcludeEngines {
String[] value();
}
/**
* Include specific packages
* @since 1.0
* @status MAINTAINED
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface IncludePackages {
String[] value();
}
/**
* Exclude specific packages
* @since 1.0
* @status MAINTAINED
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface ExcludePackages {
String[] value();
}
/**
* Include tests with specific tags. Supports tag expressions
* with operators: ! (not), & (and), | (or), and parentheses.
* @since 1.0
* @status MAINTAINED
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface IncludeTags {
String[] value();
}
/**
* Exclude tests with specific tags. Supports tag expressions
* with operators: ! (not), & (and), | (or), and parentheses.
* @since 1.0
* @status MAINTAINED
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface ExcludeTags {
String[] value();
}Annotations for customizing test display and configuration.
/**
* Set custom display name for test suite
* @since 1.0
* @status MAINTAINED
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface SuiteDisplayName {
String value();
}
/**
* Use technical names instead of display names
* @deprecated since 1.8, to be removed in JUnit Platform 2.0
* @since 1.0
* @status DEPRECATED
*/
@Deprecated
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface UseTechnicalNames {
}
/**
* Set configuration parameters (repeatable annotation)
* @since 1.8
* @status STABLE
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Repeatable(ConfigurationParameters.class)
public @interface ConfigurationParameter {
String key();
String value();
}
/**
* Container for multiple configuration parameters
* @since 1.8
* @status STABLE
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface ConfigurationParameters {
ConfigurationParameter[] value();
}
/**
* Load configuration from resource
* @since 1.8
* @status STABLE
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface ConfigurationParametersResource {
String value();
}The modern @Suite annotation that replaces the deprecated JUnit Platform Runner.
/**
* Marks a class as a test suite on the JUnit Platform. This is the modern
* replacement for @RunWith(JUnitPlatform.class).
* @since 1.8
* @status STABLE
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Suite {
/**
* Configures whether the test suite should fail if no tests are found.
* @since 1.9
*/
boolean failIfNoTests() default true;
}The JUnit Platform Runner requires the following dependencies:
Core Dependencies:
junit:junit (JUnit 4) - Required for Runner infrastructureorg.junit.platform:junit-platform-launcher - Required for test discovery and executionorg.junit.platform:junit-platform-suite-api - Required for configuration annotationsOptional Dependencies:
org.apiguardian:apiguardian-api - Provides API status annotationsSince this runner is deprecated, consider migrating to the modern approach:
Old (Deprecated):
@RunWith(JUnitPlatform.class)
@SelectPackages("com.example")
public class TestSuite {}New (Recommended):
@Suite
@SelectPackages("com.example")
public class TestSuite {}The new approach requires junit-platform-suite-engine instead of junit-platform-runner.
public to be picked up by IDEs and build tools