Add MockOnly junit 5 test extension

This commit is contained in:
Stypox 2022-03-16 17:24:55 +01:00
parent ef71a5fa0f
commit 73d1fd472f
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
2 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,20 @@
package org.schabi.newpipe.downloader;
import org.junit.jupiter.api.extension.ExtendWith;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Use this to annotate tests methods/classes that should only be run when the downloader is of type
* {@link DownloaderType#MOCK} or {@link DownloaderType#RECORDING}. This should be used when e.g. an
* extractor returns different results each time because the underlying service web page does so. In
* that case it makes sense to only run the tests with the mock downloader, since the real web page
* is not reliable, but we still want to make sure that the code correctly interprets the stored and
* mocked web page data.
* @see MockOnlyCondition
*/
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(MockOnlyCondition.class)
public @interface MockOnly {
}

View File

@ -0,0 +1,21 @@
package org.schabi.newpipe.downloader;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
/**
* @see MockOnly
*/
public class MockOnlyCondition implements ExecutionCondition {
private static final String MOCK_ONLY_REASON = "Mock only";
@Override
public ConditionEvaluationResult evaluateExecutionCondition(final ExtensionContext context) {
if (DownloaderFactory.getDownloaderType() == DownloaderType.REAL) {
return ConditionEvaluationResult.disabled(MOCK_ONLY_REASON);
} else {
return ConditionEvaluationResult.enabled(MOCK_ONLY_REASON);
}
}
}