Last active
April 2, 2023 21:08
-
-
Save patrickjh/a65505d53e25e5816b31bb44cbb0e492 to your computer and use it in GitHub Desktop.
Junit Retries annotation for Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package whatever; | |
import android.app.Instrumentation; | |
import android.os.Bundle; | |
import android.support.test.internal.runner.junit4.AndroidJUnit4ClassRunner; | |
import android.support.test.internal.util.AndroidRunnerParams; | |
import org.junit.rules.TestRule; | |
import org.junit.runners.model.InitializationError; | |
import java.util.List; | |
public class CustomizedAndroidRunner extends AndroidJUnit4ClassRunner { | |
public CustomizedAndroidRunner(Class<?> klass) throws InitializationError { | |
super(klass, new AndroidRunnerParams( | |
new Instrumentation(), | |
new Bundle(), | |
60000, | |
true)); | |
} | |
@Override | |
protected List<TestRule> getTestRules(Object target) { | |
List<TestRule> testRules = super.getTestRules(target); | |
testRules.add(new RetryTestsRule()); | |
return testRules; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package whatever; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.METHOD) | |
public @interface Retries { | |
int value(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package whatever; | |
import org.junit.rules.TestRule; | |
import org.junit.runner.Description; | |
import org.junit.runners.model.Statement; | |
public class RetryTestsRule implements TestRule { | |
RetryTestsRule() { } | |
@Override public Statement apply(final Statement base, Description description) { | |
Retries retries = description.getAnnotation(Retries.class); | |
if(retries == null) { | |
return base; | |
} | |
return new Statement() { | |
@Override | |
public void evaluate() throws Throwable { | |
for (int i = 1; i <= retries.value(); i++) { | |
try { | |
base.evaluate(); | |
break; | |
} catch (Throwable e) { | |
if (i == retries.value()) { | |
throw e; | |
} | |
} | |
} | |
} | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package whatever; | |
import whatever.Retries; | |
import whatever.CustomizedAndroidRunner; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
@RunWith(CustomizedAndroidRunner.class) | |
class SomeTestClass { | |
@Test | |
@Retries(3) | |
public void someTest() { | |
//Test logic here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment