Upgrade annotation to suppress inherited tests from junit 4 to junit 5 #4570
Unanswered
carstenartur
asked this question in
Q&A
Replies: 1 comment
-
This can be achieved using a import org.junit.platform.commons.support.AnnotationSupport;
import org.junit.platform.engine.FilterResult;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.TestSource;
import org.junit.platform.engine.support.descriptor.ClassSource;
import org.junit.platform.engine.support.descriptor.MethodSource;
import org.junit.platform.launcher.PostDiscoveryFilter;
import java.util.Optional;
public class InheritedTestsFilter implements PostDiscoveryFilter {
@Override
public FilterResult apply(TestDescriptor testDescriptor) {
return getSource(testDescriptor, MethodSource.class)
.map(methodSource -> excludeIfInherited(methodSource, getTestClass(testDescriptor)))
.orElseGet(() -> FilterResult.included("Descriptor is not a test method"));
}
private static FilterResult excludeIfInherited(MethodSource methodSource, Class<?> testClass) {
if (!AnnotationSupport.isAnnotated(testClass, IgnoreInheritedTests.class)) {
return FilterResult.included("Test class is not annotated with @" + IgnoreInheritedTests.class.getSimpleName());
}
Class<?> methodDeclaringClass = methodSource.getJavaMethod().getDeclaringClass();
return FilterResult.includedIf(methodDeclaringClass.equals(testClass),
() -> "Test method is not inherited",
() -> "Test method is inherited");
}
private Class<?> getTestClass(TestDescriptor testDescriptor) {
Optional<Class<?>> source = getSource(testDescriptor, ClassSource.class)
.map(ClassSource::getJavaClass);
return source.orElseGet(() -> testDescriptor.getParent()
.map(this::getTestClass)
.orElse(null));
}
private <T extends TestSource> Optional<T> getSource(TestDescriptor testDescriptor, Class<T> clazz) {
return testDescriptor.getSource()
.filter(clazz::isInstance)
.map(clazz::cast);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have created an annotation to suppress all inherited tests in a derived testing class using junit 4 a few years ago. The suppression completely hides the inherited tests so that they are no longer shown at all (not even as "ignored" tests). Now I need that for Junit 5. That is important because otherwise I cannot use the tests amount to check that the junit 5 upgrade did not change the number of tests.
The code for this implementation in junit 4 can be seen here:
https://github.com/eclipse-jdt/eclipse.jdt.ui/blob/master/org.eclipse.jdt.ui.tests.refactoring/test%20cases/org/eclipse/jdt/ui/tests/IgnoreInheritedTests.java
Unfortunately I failed to recreate the exactly same behavior in junit 5 so far. There I only have been able to supress the inherited tests as "disabled" what is not the same and causes issues.
Is there an "official" way to do it?
Beta Was this translation helpful? Give feedback.
All reactions