# Replace JUnit 4 Category with JUnit Jupiter Tag
# Description
This rule replaces JUnit 4 @Category (opens new window) annotations with one or more JUnit Jupiter @Tag (opens new window) annotations. These replacements are a further step towards a transition from JUnit 4 to JUnit Jupiter.
Requirements
This rule requires the following library to be present:
- junit:junit:4.13
- org.junit.jupiter:junit-jupiter-api:5.0.0
WARNING
In order to run the test suites, some further refactorings may be necessary. For example, the following suite:
import org.junit.experimental.categories.Categories;
import org.junit.experimental.categories.Categories.IncludeCategory;
import org.junit.runner.RunWith;
import org.junit.runners.Suite.SuiteClasses;
import org.test.jsparrow.examples.FirstCategory;
import org.test.jsparrow.examples.SecondCategory;
import org.test.jsparrow.examples.ClassATest;
import org.test.jsparrow.examples.ClassBTest;
@RunWith(Categories.class)
@IncludeCategory({FirstCategory.class, SecondCategory.class})
@SuiteClasses({ClassATest.class, ClassBTest.class})
public class TestSuiteJUnit4 {
}
should be refactored to
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;
import org.test.jsparrow.examples.ClassATest;
import org.test.jsparrow.examples.ClassBTest;
@RunWith(JUnitPlatform.class)
@IncludeTags({
"org.test.jsparrow.examples.FirstCategory",
"org.test.jsparrow.examples.SecondCategory" })
@SelectClasses({ClassATest.class, ClassBTest.class})
public class TestSuiteJupiter {
}
Additionally, the following libraries may by required for running the refactored tests:
- junit-platform-runner:1.7.1 for compilation
- junit-platform-engine:1.7.1 for running
- junit-jupiter-engine:5.5.2 for running
# Benefits
Migrates JUnit 4 tests to JUnit 5.
# Tags
# Code Changes
# Replacing Category with Tag Annotation
Pre
@Category(FirstCategory.class)
@Test
public void test() {
//...
}
Post
@Tag("org.test.jsparrow.examples.FirstCategory")
@Test
public void test() {
//...
}
# Replacing Multiple Categories with Tag Annotations
Pre
@Category({ FirstCategory.class, SecondCategory.class })
@Test
public void test() {
// ...
}
Post
@Tag("org.test.jsparrow.examples.FirstCategory")
@Tag("org.test.jsparrow.examples.SecondCategory")
@Test
public void test() {
// ...
}
# Category Annotation on a Class
Pre
@Category(ExampleCategory.class)
public class ExampleTest {
//...
}
Post
@Tag("org.test.jsparrow.examples.ExampleCategory")
public class ExampleTest {
//...
}
🛠️ Auto-refactor Available
You can auto refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:
Need help? Check out our installation guide.
# Properties
Property | Value |
---|---|
Rule ID | ReplaceJUnit4CategoryWithJupiterTag |
First seen in jSparrow version | 4.0.0 |
Minimum Java version | 8 |
Remediation cost | 5 min |