gpt4 book ai didi

android - 如何覆盖 Robolectric 运行时依赖库 URL?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:15 24 4
gpt4 key购买 nike

我们正在尝试使用我们自己的内部 Nexus 存储库中的 org.robolectric:robolectric:3.0 依赖项。问题是 Robolectric 尝试在运行时从公共(public)存储库 ( as mentioned here ) 加载一些依赖项,并忽略 build.gradle 中的任何存储库覆盖。

由于我们无法从内部网访问该公共(public)位置,因此我的测试在尝试加载该依赖项后超时:

[WARNING] Unable to get resource 'org.robolectric:android-all:jar:5.0.0_r2-robolectric-1' from repository sonatype (https://oss.sonatype.org/content/groups/public/): Error transferring file: Operation timed out

Robolectric configuration documentation 的底部建议将此添加到您的 Gradle 配置以覆盖 URL:

android {
testOptions {
unitTests.all {
systemProperty 'robolectric.dependency.repo.url', 'https://local-mirror/repo'
systemProperty 'robolectric.dependency.repo.id', 'local'
}
}
}

不幸的是,我已经对此进行了测试,但我从未看到该系统属性被设置。我已经从我的自定义 Robolectric runner(它扩展了 RobolectricGradleTestRunner)中打印出来,并且该系统属性仍然设置为 null。

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

我也尝试做类似 this comment 的事情(但在 RobolectricGradleTestRunner 中不存在可覆盖的方法),而且我还尝试直接在我的自定义 Robolectric runner 中设置系统属性,但这似乎没有帮助。

@Config(constants = BuildConfig.class)
public class CustomRobolectricRunner extends RobolectricGradleTestRunner {
private static final String BUILD_OUTPUT = "build/intermediates";

public CustomRobolectricRunner(Class<?> testClass) throws InitializationError {
super(testClass);

System.setProperty("robolectric.dependency.repo.url", "https://nexus.myinternaldomain.com/content");
System.setProperty("robolectric.dependency.repo.id", "internal");

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

Robolectric source code似乎确实证实了这些系统属性的存在。

最佳答案

虽然不是直接使用属性的修复方法,但另一种方法是在 RobolectricTestRunner 子类中覆盖 getJarResolver() 并将其指向您的 Artifact 主持人:

public final class MyTestRunner extends RobolectricTestRunner {
public MyTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}

@Override protected DependencyResolver getJarResolver() {
return new CustomDependencyResolver();
}

static final class CustomDependencyResolver implements DependencyResolver {
private final Project project = new Project();

@Override public URL[] getLocalArtifactUrls(DependencyJar... dependencies) {
DependenciesTask dependenciesTask = new DependenciesTask();
RemoteRepository repository = new RemoteRepository();
repository.setUrl("https://my-nexus.example.com/content/groups/public");
repository.setId("my-nexus");
dependenciesTask.addConfiguredRemoteRepository(repository);
dependenciesTask.setProject(project);
for (DependencyJar dependencyJar : dependencies) {
Dependency dependency = new Dependency();
dependency.setArtifactId(dependencyJar.getArtifactId());
dependency.setGroupId(dependencyJar.getGroupId());
dependency.setType(dependencyJar.getType());
dependency.setVersion(dependencyJar.getVersion());
if (dependencyJar.getClassifier() != null) {
dependency.setClassifier(dependencyJar.getClassifier());
}
dependenciesTask.addDependency(dependency);
}
dependenciesTask.execute();

@SuppressWarnings("unchecked")
Hashtable<String, String> artifacts = project.getProperties();
URL[] urls = new URL[dependencies.length];
for (int i = 0; i < urls.length; i++) {
try {
urls[i] = Util.url(artifacts.get(key(dependencies[i])));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
return urls;
}

@Override public URL getLocalArtifactUrl(DependencyJar dependency) {
URL[] urls = getLocalArtifactUrls(dependency);
if (urls.length > 0) {
return urls[0];
}
return null;
}

private String key(DependencyJar dependency) {
String key =
dependency.getGroupId() + ":" + dependency.getArtifactId() + ":" + dependency.getType();
if (dependency.getClassifier() != null) {
key += ":" + dependency.getClassifier();
}
return key;
}
}
}

需要注意的是,这依赖于 Robolectric 的两个内部类,因此在升级版本时应格外小心。

关于android - 如何覆盖 Robolectric 运行时依赖库 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39358697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com