a little custom method, but it works.
public class TestTemplate extends BaseTest{ @Category(DefaultCategory.CategoryA.class) @Test public void testA(){ System.out.println("A"); } @Category(DefaultCategory.CategoryB.class) @Test public void testB1(){ System.out.println("B1"); } @Category(DefaultCategory.CategoryB.class) @Test public void testB(){ System.out.println("B"); } }
Next comes the description of the base class, where it is determined which methods need to be executed. You yourself can create your own categories Rus, Usa, Study in the image of CategoryA and CategoryB
import lombok.extern.slf4j.Slf4j; import org.junit.*; import org.junit.experimental.categories.Category; import org.junit.rules.TestName; import org.junit.rules.TestRule; import org.junit.rules.TestWatcher; import org.junit.runner.Description; import java.lang.annotation.Annotation; import java.util.HashSet; @Slf4j public class BaseTest { public static String caseCategories; public static String[] currentCaseCategories = null; public static HashSet<String> ignoringCategories = new HashSet<>(); static { System.setProperty("test.category.for.exec", "CategoryB"); } @Rule public TestName name = new TestName(); @Before public void beforeMethod() { currentCaseCategories = getCategories(); if (currentCaseCategories != null) for (String category : currentCaseCategories) { if (!category.contains(System.getProperty("test.category.for.exec"))) ignoringCategories.add(category); } boolean isCurrentCatIgnore = isContains(ignoringCategories, currentCaseCategories); org.junit.Assume.assumeFalse("Ignore next test cases", isCurrentCatIgnore); log.info("###################################################################"); log.info(name.getMethodName()); log.info(System.lineSeparator()); currentCaseCategories = null; } /** * Нам нужно создать категории, которые будем вешать на тестовый метод. */ public static class DefaultCategory { public static class CategoryA implements Category { @Override public Class<?>[] value() { return new Class<?>[0]; } /** * Returns the annotation type of this annotation. * * @return the annotation type of this annotation */ @Override public Class<? extends Annotation> annotationType() { return null; } } public static class CategoryB implements Category { @Override public Class<?>[] value() { return new Class<?>[0]; } /** * Returns the annotation type of this annotation. * * @return the annotation type of this annotation */ @Override public Class<? extends Annotation> annotationType() { return null; } } } /** * Возвращает текущие категории * * @return */ public String[] getCategories() { String cases = null; String[] caseArr = null; if (this.caseCategories == null) return null; // удаляем все лишнеи пути до класса с категориями cases = this.caseCategories.substring(51, this.caseCategories.length() - 2); cases = cases.replace("class ", "").replaceAll("\\s+", ""); caseArr = cases.split(","); for (int i = 0; i < caseArr.length; i++) { caseArr[i] = caseArr[i].replace("$", "."); } return caseArr; } @Rule public TestRule watchman = new TestWatcher() { // этот ментод выполняется до выполнения метода @Test @Override protected void starting(Description description) { super.starting(description); caseCategories = null; for (Annotation annotation : description.getAnnotations()) { String d = annotation.toString(); if (d.startsWith("@org.junit.experimental.categories.Category")) { // запоминаем все аннотации caseCategories = d; } } } }; private boolean isContains(HashSet<String> hashSet, String[] categories) { if (categories != null) for (String s : categories) { if (hashSet.contains(s)) return true; } return false; } }
in this case, the testA test () will be ignored just as with the @Ignore annotation