android – 如何覆盖Robolectric运行时依赖性存储库URL?

前端之家收集整理的这篇文章主要介绍了android – 如何覆盖Robolectric运行时依赖性存储库URL?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们试图从我们自己的内部Nexus存储库中使用org.robolectric:robolectric:3.0依赖.问题是Robolectric尝试从公共存储库( as mentioned here)在运行时加载一些依赖项,并忽略build.gradle中的任何存储库覆盖.

由于我们无法从Intranet访问该公共位置,因此在尝试加载该依赖项后,我的测试会超时:

[WARNING] Unable to get resource
‘org.robolectric:android-all:jar:5.0.0_r2-robolectric-1’ from
repository sonatype (07001):
Error transferring file: Operation timed out

Robolectric configuration documentation底部部分建议将其添加到Gradle配置中以覆盖URL:

  1. android {
  2. testOptions {
  3. unitTests.all {
  4. systemProperty 'robolectric.dependency.repo.url','https://local-mirror/repo'
  5. systemProperty 'robolectric.dependency.repo.id','local'
  6. }
  7. }
  8. }

不幸的是,我已经测试了,我从未看到系统属性被设置.我从我的自定义Robolectric跑步者(扩展RobolectricGradleTestRunner)中打印出来,并且系统属性保持设置为空.

  1. System.out.println("robolectric.dependency.repo.url: " + System.getProperty("robolectric.dependency.repo.url"));

我也尝试过类似于this comment的东西(但在RobolectricGradleTestRunner中不存在该方法),我也尝试直接在我的自定义Robolectric运行器中设置系统属性,这似乎没有帮助.

  1. @Config(constants = BuildConfig.class)
  2. public class CustomRobolectricRunner extends RobolectricGradleTestRunner {
  3. private static final String BUILD_OUTPUT = "build/intermediates";
  4.  
  5. public CustomRobolectricRunner(Class<?> testClass) throws InitializationError {
  6. super(testClass);
  7.  
  8. System.setProperty("robolectric.dependency.repo.url","https://nexus.myinternaldomain.com/content");
  9. System.setProperty("robolectric.dependency.repo.id","internal");
  10.  
  11. System.out.println("robolectric.dependency.repo.url: " + System.getProperty("robolectric.dependency.repo.url"));
  12. }

Robolectric source code确实似乎确认存在这些系统属性.

解决方法

虽然不是直接使用属性的修复,但另一种让它工作的方法是在RobolectricTestRunner子类中重写getJarResolver()并将其指向工件主机:
  1. public final class MyTestRunner extends RobolectricTestRunner {
  2. public MyTestRunner(Class<?> testClass) throws InitializationError {
  3. super(testClass);
  4. }
  5.  
  6. @Override protected DependencyResolver getJarResolver() {
  7. return new CustomDependencyResolver();
  8. }
  9.  
  10. static final class CustomDependencyResolver implements DependencyResolver {
  11. private final Project project = new Project();
  12.  
  13. @Override public URL[] getLocalArtifactUrls(DependencyJar... dependencies) {
  14. DependenciesTask dependenciesTask = new DependenciesTask();
  15. RemoteRepository repository = new RemoteRepository();
  16. repository.setUrl("https://my-nexus.example.com/content/groups/public");
  17. repository.setId("my-nexus");
  18. dependenciesTask.addConfiguredRemoteRepository(repository);
  19. dependenciesTask.setProject(project);
  20. for (DependencyJar dependencyJar : dependencies) {
  21. Dependency dependency = new Dependency();
  22. dependency.setArtifactId(dependencyJar.getArtifactId());
  23. dependency.setGroupId(dependencyJar.getGroupId());
  24. dependency.setType(dependencyJar.getType());
  25. dependency.setVersion(dependencyJar.getVersion());
  26. if (dependencyJar.getClassifier() != null) {
  27. dependency.setClassifier(dependencyJar.getClassifier());
  28. }
  29. dependenciesTask.addDependency(dependency);
  30. }
  31. dependenciesTask.execute();
  32.  
  33. @SuppressWarnings("unchecked")
  34. Hashtable<String,String> artifacts = project.getProperties();
  35. URL[] urls = new URL[dependencies.length];
  36. for (int i = 0; i < urls.length; i++) {
  37. try {
  38. urls[i] = Util.url(artifacts.get(key(dependencies[i])));
  39. } catch (MalformedURLException e) {
  40. throw new RuntimeException(e);
  41. }
  42. }
  43. return urls;
  44. }
  45.  
  46. @Override public URL getLocalArtifactUrl(DependencyJar dependency) {
  47. URL[] urls = getLocalArtifactUrls(dependency);
  48. if (urls.length > 0) {
  49. return urls[0];
  50. }
  51. return null;
  52. }
  53.  
  54. private String key(DependencyJar dependency) {
  55. String key =
  56. dependency.getGroupId() + ":" + dependency.getArtifactId() + ":" + dependency.getType();
  57. if (dependency.getClassifier() != null) {
  58. key += ":" + dependency.getClassifier();
  59. }
  60. return key;
  61. }
  62. }
  63. }

应该注意的是,这依赖于两个内部类型的Robolectric,因此在升级版本时应该小心.

猜你在找的Android相关文章