I'm trying to run unit tests using Robolectric; they run fine under Android Studio, but the exact same tests fail when running in the command line - which is a big deal, I need to be able to run them from my continuous integration platform, not just from an IDE.
我試着用Robolectric來運行單元測試;它們在Android Studio下運行良好,但在命令行中運行時,同樣的測試也會失敗——這很重要,我需要能夠在我的持續集成平台上運行它們,而不僅僅是在IDE上。
I suspect that I'm missing some command-line argument (say, a classpath or something similar) or calling the wrong task - otherwise the test wouldn't run at all from Android Studio. Some relevant details; the test looks like this:
我懷疑我漏掉了一些命令行參數(比如類路徑或類似的東西)或者調用了錯誤的任務——否則這個測試將無法在Android Studio中運行。一些相關的細節;測試是這樣的:
@RunWith(RobolectricTestRunner.class)
@Config(manifest = "app/src/main/AndroidManifest.xml", resourceDir = "res", emulateSdk = 19)
public class SplashActivityTest {
@Test
public void testActivity() {
SplashActivity splashActivity = new SplashActivity();
String appName = splashActivity.getString(R.string.app_name); // HERE, line 20
assertEquals(appName, "App");
}
}
As mentioned above, it runs fine in Android Studio (by right-clicking the test file and selecting Run 'SplashActivityTest') but when running it from the command line it fails in the line marked with HERE, with the following stack trace:
如前所述,它在Android Studio中運行良好(通過右鍵單擊測試文件並選擇Run 'SplashActivityTest'),但是當從命令行運行它時,它會在這里標記的行中失敗,其堆棧跟蹤如下:
android.content.res.Resources$NotFoundException: unknown resource 2131492893
at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:309)
at org.robolectric.shadows.ShadowAssetManager.getResourceText(ShadowAssetManager.java:69)
at android.content.res.AssetManager.getResourceText(AssetManager.java)
at android.content.res.Resources.getText(Resources.java:240)
at org.robolectric.shadows.ShadowResources.getText(ShadowResources.java:361)
at android.content.res.Resources.getText(Resources.java)
at android.content.res.Resources.getString(Resources.java:330)
at org.robolectric.shadows.ShadowContext.getString(ShadowContext.java:39)
at org.robolectric.shadows.ShadowContextWrapper.getString(ShadowContextWrapper.java:69)
at android.content.Context.getString(Context.java)
at path.to.myApp.activities.SplashActivityTest.testActivity(SplashActivityTest.java:20)
// ... and so on ...
I'm using this to run from the command line (notice that in here and in Android Studio I'm using the Gradle wrapper):
我使用這個命令行運行(注意,在這里和Android Studio中,我使用的是Gradle包裝):
project-root$ ./gradlew test --continue
Also: I'm using Android Studio 1.1.0, Gradle version is 2.3, Robolectric's version is 3.0-SNAPSHOT and Robolectric's Gradle plugin version is 1.0.1
另外:我使用的是Android Studio 1.1.0, Gradle版本2.3,Robolectric版本3.0-SNAPSHOT, Robolectric版本的Gradle插件版本1.0.1
2 个解决方案
#1
8
It turns out that this is a known issue. The resources of a project are looked for in the wrong directory when running tests from the command line, here's a link to the issue; for now the workaround is to write a custom test runner as demonstrated here:
事實證明,這是一個已知的問題。當從命令行運行測試時,在錯誤的目錄中查找項目的資源,這里是問題的鏈接;目前的解決方案是編寫一個自定義測試運行器,如下所示:
public class RobolectricGradleTestRunner extends RobolectricTestRunner {
public RobolectricGradleTestRunner(Class> testClass) throws InitializationError {
super(testClass);
String buildVariant = (BuildConfig.FLAVOR.isEmpty() ? "" : BuildConfig.FLAVOR+ "/") + BuildConfig.BUILD_TYPE;
String intermediatesPath = BuildConfig.class.getResource("").toString().replace("file:", "");
intermediatesPath = intermediatesPath.substring(0, intermediatesPath.indexOf("/classes"));
System.setProperty("android.package", BuildConfig.APPLICATION_ID);
System.setProperty("android.manifest", intermediatesPath + "/manifests/full/" + buildVariant + "/AndroidManifest.xml");
System.setProperty("android.resources", intermediatesPath + "/res/" + buildVariant);
System.setProperty("android.assets", intermediatesPath + "/assets/" + buildVariant);
}
}
The test cases must be updated accordingly to use the custom test runner instead of the @Config annotation:
必須相應地更新測試用例,以使用自定義測試運行器,而不是使用@Config注釋:
@RunWith(RobolectricGradleTestRunner.class)
public class SplashActivityTest {
// tests same as before
}
Also, it's recommended to perform a clean when running the tests from the command line, given that the workaround depends on the contents of the build directory, we don't want to have stale data there:
另外,建議在運行來自命令行的測試時執行干凈,因為工作區取決於構建目錄的內容,我們不希望在那里有陳舊的數據:
project-root$ ./gradlew clean test --continue
#2
0
I've solved the same error, but in different way. I used custom class shadows in tests, So I have to create my own TestRunner class, like this:
我用不同的方法解決了同樣的錯誤。我在測試中使用了自定義類陰影,所以我必須創建自己的TestRunner類,如下所示:
public class MyRobolectricTestRunner extends RobolectricTestRunner {
public MyRobolectricTestRunner(Class> klass) throws InitializationError {
super(klass);
}
public InstrumentationConfiguration createClassLoaderConfig() {
InstrumentationConfiguration.Builder builder = InstrumentationConfiguration.newBuilder();
builder.addInstrumentedClass(AppUtils.class.getName());
return builder.build();
}
}
But above code worked only when I run the unit-test from Andorid studio. Please, check out code that works for me in both ways: in Studio and from the command-line:
但是上面的代碼只在我運行來自Andorid studio的單元測試時有效。請在Studio和命令行中查看適合我的代碼:
public class MyRobolectricTestRunner extends RobolectricTestRunner {
public MyRobolectricTestRunner(Class> klass) throws InitializationError {
super(klass);
}
@Override
public InstrumentationConfiguration createClassLoaderConfig() {
InstrumentationConfiguration.Builder builder = InstrumentationConfiguration.newBuilder();
builder.addInstrumentedPackage(com.example.stackoverflow.AppUtils.class.getPackage().getName());
return builder.build();
}
}
Please, note I have the dependency:
請注意,我有依賴性:
testCompile 'org.robolectric:robolectric:3.0'