Skip to content

Commit

Permalink
Evaluate TestRequestBuilder.CustomFilters also on class, not just on …
Browse files Browse the repository at this point in the history
…test.
  • Loading branch information
jan-varecka-signageos-io committed May 1, 2024
1 parent 0733bc6 commit 10f295e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Expand Up @@ -243,6 +243,15 @@ static Suite createSuite(List<Runner> runners) {
}

private static class CustomFilters extends AbstractFilter {

@Override
public boolean shouldRun(Description description) {
if (description.isSuite() && !evaluateTest(description)) {
return false;
}
return super.shouldRun(description);
}

@Override
protected boolean evaluateTest(Description description) {
Collection<Annotation> allAnnotations = description.getAnnotations();
Expand Down
Expand Up @@ -227,6 +227,50 @@ public String describe() {
}
}

@SampleCustomAnnotationOnClass(runTests = true)
public static class SampleCustomFilterOnClassRunTestsTestClass {
@Test
public void testOneRun() {}

@Test
public void testTwoRun() {}
}

@SampleCustomAnnotationOnClass(runTests = false)
public static class SampleCustomFilterOnClassSkipTestsTestClass {
@Test
public void testOneSkip() {}

@Test
public void testTwoSkip() {}
}

public static class SampleCustomFilterOnClass extends AbstractFilter {
public SampleCustomFilterOnClass() {}

@Override
public boolean shouldRun(Description description) {
return evaluateTest(description);
}

@Override
protected boolean evaluateTest(Description description) {
return description.getAnnotation(SampleCustomAnnotationOnClass.class).runTests();
}

@Override
public String describe() {
return "skip all tests in class if runTests is false";
}
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@CustomFilter(filterClass = SampleCustomFilterOnClass.class)
public @interface SampleCustomAnnotationOnClass {
boolean runTests() default true;
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@CustomFilter(filterClass = SampleCustomFilter.class)
Expand Down Expand Up @@ -971,6 +1015,24 @@ public void testCustomFilter() {
Assert.assertEquals(1, result.getRunCount());
}

/** Test that {@link CustomFilter} filters the class as appropriate */
@Test
public void testCustomFilterAnnotation_onClass_runTests() {
Request request = builder.addTestClass(SampleCustomFilterOnClassRunTestsTestClass.class.getName()).build();
JUnitCore testRunner = new JUnitCore();
Result result = testRunner.run(request);
Assert.assertEquals(2, result.getRunCount());
}

/** Test that {@link CustomFilter} filters the class as appropriate */
@Test
public void testCustomFilterAnnotation_onClass_skipTests() {
Request request = builder.addTestClass(SampleCustomFilterOnClassSkipTestsTestClass.class.getName()).build();
JUnitCore testRunner = new JUnitCore();
Result result = testRunner.run(request);
Assert.assertEquals(0, result.getRunCount());
}

/** Test that a custom RunnerBuilder is used. */
@Test
public void testCustomRunnerBuilder() {
Expand Down

0 comments on commit 10f295e

Please sign in to comment.