Skip to content

Commit 73d1fd4

Browse files
committed
Add MockOnly junit 5 test extension
1 parent ef71a5f commit 73d1fd4

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.schabi.newpipe.downloader;
2+
3+
import org.junit.jupiter.api.extension.ExtendWith;
4+
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
8+
/**
9+
* Use this to annotate tests methods/classes that should only be run when the downloader is of type
10+
* {@link DownloaderType#MOCK} or {@link DownloaderType#RECORDING}. This should be used when e.g. an
11+
* extractor returns different results each time because the underlying service web page does so. In
12+
* that case it makes sense to only run the tests with the mock downloader, since the real web page
13+
* is not reliable, but we still want to make sure that the code correctly interprets the stored and
14+
* mocked web page data.
15+
* @see MockOnlyCondition
16+
*/
17+
@Retention(RetentionPolicy.RUNTIME)
18+
@ExtendWith(MockOnlyCondition.class)
19+
public @interface MockOnly {
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.schabi.newpipe.downloader;
2+
3+
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
4+
import org.junit.jupiter.api.extension.ExecutionCondition;
5+
import org.junit.jupiter.api.extension.ExtensionContext;
6+
7+
/**
8+
* @see MockOnly
9+
*/
10+
public class MockOnlyCondition implements ExecutionCondition {
11+
private static final String MOCK_ONLY_REASON = "Mock only";
12+
13+
@Override
14+
public ConditionEvaluationResult evaluateExecutionCondition(final ExtensionContext context) {
15+
if (DownloaderFactory.getDownloaderType() == DownloaderType.REAL) {
16+
return ConditionEvaluationResult.disabled(MOCK_ONLY_REASON);
17+
} else {
18+
return ConditionEvaluationResult.enabled(MOCK_ONLY_REASON);
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)