Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to specify suite classes in custom Categories subclasses constructor #1737

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/main/java/org/junit/experimental/categories/Categories.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,20 @@ private static Set<Class<?>> copyAndRefine(Set<Class<?>> classes) {

public Categories(Class<?> klass, RunnerBuilder builder) throws InitializationError {
super(klass, builder);
initialize(klass);
}

protected Categories(RunnerBuilder builder, Class<?> klass, Class<?>[] suiteClasses) throws InitializationError {
super(builder, klass, suiteClasses);
initialize(klass);
}

private void initialize(Class<?> klass) throws InitializationError {
try {
Set<Class<?>> included= getIncludedCategory(klass);
Set<Class<?>> excluded= getExcludedCategory(klass);
boolean isAnyIncluded= isAnyIncluded(klass);
boolean isAnyExcluded= isAnyExcluded(klass);
Set<Class<?>> included = getIncludedCategory(klass);
Set<Class<?>> excluded = getExcludedCategory(klass);
boolean isAnyIncluded = isAnyIncluded(klass);
boolean isAnyExcluded = isAnyExcluded(klass);

filter(CategoryFilter.categoryFilter(isAnyIncluded, included, isAnyExcluded, excluded));
} catch (NoTestsRemainException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.junit.experimental.categories;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;

import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Categories.IncludeCategory;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;

public class CustomCategoriesSuiteTest {

public static class CustomCategories extends Categories {
public CustomCategories(Class<?> klass, RunnerBuilder builder)
throws InitializationError {
super(builder, klass, suiteClasses());
}

private static Class<?>[] suiteClasses() {
// Here a custom algorithm could be invoked to determine which
// classes should be run by the test suite.
// e.g. Reflections lib. (org.reflections) could be used to
// determine subclasses of a abstract test class
// which could be provided to the test suite by a custom annotation.
return new Class<?>[] { TestWithCategory.class };
}
}

public static class TestCategory {
}

@Category(TestCategory.class)
public static class TestWithCategory {
@Parameters
public static Iterable<String> getParameters() {
return Arrays.asList("first", "second");
}

@Parameterized.Parameter
public String value;

@Test
public void testSomething() {
Assert.assertTrue(true);
}
}

@RunWith(CustomCategories.class)
@IncludeCategory(TestCategory.class)
public static class SuiteWithTestWithCategory {
}

@Test
public void runsTestsWithCategory() {
Result result = new JUnitCore().run(SuiteWithTestWithCategory.class);
assertEquals(1, result.getRunCount());
assertEquals(0, result.getFailureCount());
}
}