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.Target;
import javax.annotation.Nonnull;
/**
* 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})
@Inherited
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() {
@Override
public void evaluate() throws Throwable {
final boolean hasAnnotation = description.getAnnotation(MockOnly.class) == null;
final boolean isMockDownloader = downloader == null ||
!downloader.equalsIgnoreCase(DownloaderType.REAL.toString());
Assume.assumeTrue(
"The test is not reliable against a website and thus skipped",
hasAnnotation && isMockDownloader
);
final MockOnly annotation = description.getAnnotation(MockOnly.class);
if (annotation != null) {
final boolean isMockDownloader = downloader == null ||
!downloader.equalsIgnoreCase(DownloaderType.REAL.toString());
Assume.assumeTrue("The test is not reliable against real website. Reason: "
+ annotation.reason(), isMockDownloader);
}
base.evaluate();
}