diff options
author | Zhihong Xu <zhihongx@google.com> | 2013-08-02 16:49:36 -0700 |
---|---|---|
committer | Zhihong Xu <zhihongx@google.com> | 2013-08-05 17:14:51 -0700 |
commit | b276278567ee51bf43669a5da4b55deea8e4df09 (patch) | |
tree | 50f1291eb66efeba2ad8768be6674360932d2f26 | |
parent | bd20b3c07ba0d63c0e62744eef3c90b69cd6b126 (diff) | |
download | robolectric-b276278567ee51bf43669a5da4b55deea8e4df09.tar.gz |
Fix Wallet robolectric tests, Part 1
The build server java compiler builds robolectric tests in such
a way that custom shadows are added to class loader after the
test classes are loaded. This causes some custom shadows to be
ignored when the real classes are referenced in a test class.
This and the followup CL fixed that (hopefully) by moving the
addition of custom shadows forward to the constructor of
WalletRoboTestRunner.
Bug:10071558
Change-Id: I76745fdfcc08bdc198de8d90cf15dbabbbada7e0
-rw-r--r-- | src/main/java/com/xtremelabs/robolectric/RobolectricTestRunner.java | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/src/main/java/com/xtremelabs/robolectric/RobolectricTestRunner.java b/src/main/java/com/xtremelabs/robolectric/RobolectricTestRunner.java index b03307976..50fc33bf5 100644 --- a/src/main/java/com/xtremelabs/robolectric/RobolectricTestRunner.java +++ b/src/main/java/com/xtremelabs/robolectric/RobolectricTestRunner.java @@ -117,6 +117,25 @@ public class RobolectricTestRunner extends BlockJUnit4ClassRunner implements Rob } /** + * Creates a runner to run {@code testClass}. Looks in your working directory for your AndroidManifest.xml file + * and res directory. + * + * @param testClass the test class to be run + * @param classLoader a custom RobolectricClassLoader to be used. + * @throws InitializationError if junit says so + */ + public RobolectricTestRunner(final Class<?> testClass, RobolectricClassLoader classLoader) + throws InitializationError { + this(testClass, + isInstrumented() ? null : ShadowWrangler.getInstance(), + isInstrumented() ? null : classLoader, + new RobolectricConfig( + new File(getSystemProperty(MANIFEST_PATH_PROPERTY, DEFAULT_MANIFEST_PATH)), + new File(getSystemProperty(RES_PATH_PROPERTY, DEFAULT_RES_PATH)), + new File(getSystemProperty(ASSETS_PATH_PROPERTY, DEFAULT_ASSETS_PATH)))); + } + + /** * Call this constructor in subclasses in order to specify non-default configuration (e.g. location of the * AndroidManifest.xml file and resource directory). * @@ -229,11 +248,12 @@ public class RobolectricTestRunner extends BlockJUnit4ClassRunner implements Rob * @throws InitializationError if junit says so */ protected RobolectricTestRunner(final Class<?> testClass, final ClassHandler classHandler, final RobolectricClassLoader classLoader, final RobolectricConfig robolectricConfig, final DatabaseMap map) throws InitializationError { - super(isInstrumented() ? testClass : classLoader.bootstrap(testClass)); + super(isInstrumented() ? testClass + : ensureClassLoaderNotNull(classLoader).bootstrap(testClass)); if (!isInstrumented()) { this.classHandler = classHandler; - this.classLoader = classLoader; + this.classLoader = ensureClassLoaderNotNull(classLoader); this.robolectricConfig = robolectricConfig; this.databaseMap = setupDatabaseMap(testClass, map); @@ -259,6 +279,11 @@ public class RobolectricTestRunner extends BlockJUnit4ClassRunner implements Rob } } + private static RobolectricClassLoader ensureClassLoaderNotNull( + RobolectricClassLoader classLoader) { + return classLoader == null ? getDefaultLoader() : classLoader; + } + protected static boolean isInstrumented() { return instrumentDetector.isInstrumented(); } |