Skip to content
Merged
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
2 changes: 2 additions & 0 deletions runner/android_junit_runner/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

**Bug Fixes**

* Inspect class-level annotations in `TestRequestBuilder.CustomFilters` so `@CustomFilter` annotations applied at the test class level are evaluated. (b/573501234)

**New Features**

**Breaking Changes**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,15 @@ static Suite createSuite(List<Runner> runners) {
private static class CustomFilters extends AbstractFilter {
@Override
protected boolean evaluateTest(Description description) {
Collection<Annotation> allAnnotations = description.getAnnotations();
List<Annotation> allAnnotations = new ArrayList<>(description.getAnnotations());
Class<?> testClass = description.getTestClass();
if (testClass != null) {
for (Annotation classAnnotation : testClass.getAnnotations()) {
if (description.getAnnotation(classAnnotation.annotationType()) == null) {
allAnnotations.add(classAnnotation);
}
}
}

for (Annotation a : allAnnotations) {
CustomFilter filter = a.annotationType().getAnnotation(CustomFilter.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,36 @@ public void testRun() {}
public void testNotRun() {}
}

@SampleCustomAnnotation(runTest = true)
public static class SampleCustomFilterClassIncludedTestClass {
@Test
public void testRunFromClassAnnotation() {}

@Test
@SampleCustomAnnotation(runTest = false)
public void testNotRunOverridingClassAnnotation() {}
}

@SampleCustomAnnotation(runTest = false)
public static class SampleCustomFilterClassExcludedTestClass {
@Test
public void testNotRunFromClassAnnotation() {}

@Test
@SampleCustomAnnotation(runTest = true)
public void testRunOverridingClassAnnotation() {}
}

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

@Override
protected boolean evaluateTest(Description description) {
return description.getAnnotation(SampleCustomAnnotation.class).runTest();
SampleCustomAnnotation annotation = description.getAnnotation(SampleCustomAnnotation.class);
if (annotation == null && description.getTestClass() != null) {
annotation = description.getTestClass().getAnnotation(SampleCustomAnnotation.class);
}
return annotation == null || annotation.runTest();
}

@Override
Expand Down Expand Up @@ -485,6 +509,7 @@ public void run() {
fail("Broken");
}
}

@Mock private ClassPathScanner mockClassPathScanner;

private TestRequestBuilder builder;
Expand Down Expand Up @@ -922,6 +947,23 @@ public void testCustomFilterAnnotation() {
Assert.assertEquals(1, result.getRunCount());
}

/** Test that {@link CustomFilter} on a class-level annotation filters tests as appropriate */
@Test
public void testCustomFilterAnnotation_classLevel() {
Request request =
builder
.addTestClass(SampleCustomFilterClassIncludedTestClass.class.getName())
.addTestClass(SampleCustomFilterClassExcludedTestClass.class.getName())
.build();
List<String> results = runRequest(request);
assertThat(results)
.containsExactly(
SampleCustomFilterClassIncludedTestClass.class.getName()
+ "#testRunFromClassAnnotation",
SampleCustomFilterClassExcludedTestClass.class.getName()
+ "#testRunOverridingClassAnnotation");
}

/** Test that {@link CustomFilter} throws when the filter class cannot be instantiated */
@Test
public void testMalformedCustomFilterAnnotation() {
Expand Down
Loading