File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package org .schabi .newpipe ;
2+
3+ import java .lang .annotation .ElementType ;
4+ import java .lang .annotation .Inherited ;
5+ import java .lang .annotation .Retention ;
6+ import java .lang .annotation .RetentionPolicy ;
7+ import java .lang .annotation .Target ;
8+
9+ /**
10+ * Marker annotation to skip test if it not run with mocks.
11+ *
12+ * {@link MockOnlyRule}
13+ */
14+ @ Retention (RetentionPolicy .RUNTIME )
15+ @ Target ({ElementType .METHOD , ElementType .TYPE })
16+ @ Inherited
17+ public @interface MockOnly {
18+ }
Original file line number Diff line number Diff line change 1+ package org .schabi .newpipe ;
2+
3+ import org .junit .Assume ;
4+ import org .junit .rules .TestRule ;
5+ import org .junit .runner .Description ;
6+ import org .junit .runners .model .Statement ;
7+ import org .schabi .newpipe .downloader .DownloaderType ;
8+
9+ import javax .annotation .Nonnull ;
10+
11+ /**
12+ * <p>
13+ * Checks if the system variable "downloader" is set to "REAL" and skips the tests if it is.
14+ * Otherwise execute the test.
15+ * </p>
16+ *
17+ * <p>
18+ * Use it by creating a public variable of this inside the test class and annotate it with
19+ * {@link org.junit.Rule}. Then annotate the tests to be skipped with {@link MockOnly}
20+ * </p>
21+ *
22+ * <p>
23+ * Allows skipping unreliable or time sensitive tests in CI pipeline.
24+ * </p>
25+ */
26+ public class MockOnlyRule implements TestRule {
27+
28+ final String downloader = System .getProperty ("downloader" );
29+
30+ @ Override
31+ @ Nonnull
32+ public Statement apply (@ Nonnull Statement base , @ Nonnull Description description ) {
33+ return new Statement () {
34+ @ Override
35+ public void evaluate () throws Throwable {
36+ final boolean hasAnnotation = description .getAnnotation (MockOnly .class ) == null ;
37+ final boolean isMockDownloader = downloader == null ||
38+ !downloader .equalsIgnoreCase (DownloaderType .REAL .toString ());
39+ Assume .assumeTrue (
40+ "The test is not reliable against a website and thus skipped" ,
41+ hasAnnotation && isMockDownloader
42+ );
43+
44+ base .evaluate ();
45+ }
46+ };
47+ }
48+ }
You can’t perform that action at this time.
0 commit comments