Add reason field to MockOnly

This enforces developers to document why a test is skipped
This commit is contained in:
XiangRongLin 2021-02-19 18:53:33 +01:00
parent ea52030613
commit adf9d7d10f
2 changed files with 16 additions and 7 deletions

View File

@ -6,6 +6,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import javax.annotation.Nonnull;
/** /**
* Marker annotation to skip test if it not run with mocks. * Marker annotation to skip test if it not run with mocks.
* *
@ -15,4 +17,9 @@ import java.lang.annotation.Target;
@Target({ElementType.METHOD, ElementType.TYPE}) @Target({ElementType.METHOD, ElementType.TYPE})
@Inherited @Inherited
public @interface MockOnly { public @interface MockOnly {
/**
* Explanation why this test shold only be run with mocks and not against real websites
*/
@Nonnull String reason();
} }

View File

@ -33,13 +33,15 @@ public class MockOnlyRule implements TestRule {
return new Statement() { return new Statement() {
@Override @Override
public void evaluate() throws Throwable { public void evaluate() throws Throwable {
final boolean hasAnnotation = description.getAnnotation(MockOnly.class) == null; final MockOnly annotation = description.getAnnotation(MockOnly.class);
final boolean isMockDownloader = downloader == null || if (annotation != null) {
!downloader.equalsIgnoreCase(DownloaderType.REAL.toString()); final boolean isMockDownloader = downloader == null ||
Assume.assumeTrue( !downloader.equalsIgnoreCase(DownloaderType.REAL.toString());
"The test is not reliable against a website and thus skipped",
hasAnnotation && isMockDownloader Assume.assumeTrue("The test is not reliable against real website. Reason: "
); + annotation.reason(), isMockDownloader);
}
base.evaluate(); base.evaluate();
} }