Robolectric 连接超时无法下载

se2*_*2as 4 android robolectric kotlin

我正在尝试为我的 Kotlin 应用程序编写 Robolectric 测试,但无法运行我的测试,因为它无法下载 Robolectric。我在 StackOverflow 和 Google 上进行了搜索,但没有任何建议可以解决我的问题,因此我在这里提出问题。

\n\n

我使用的是Android Studio 3.6

\n\n

我看到错误

\n\n
Downloading from maven \nDownloading: org/robolectric/android-all/9-robolectric-4913185-2/android-all-9-robolectric-4913185-2.pom from repository sonatype at https://oss.sonatype.org/content/groups/public/\nError transferring file: Connection timed out: connect\n[WARNING] Unable to get resource \'org.robolectric:android-all:pom:9-robolectric-4913185-2\' from repository sonatype (https://oss.sonatype.org/content/groups/public/): Error transferring file: Connection timed out: connect\nDownloading: org/robolectric/android-all/9-robolectric-4913185-2/android-all-9-robolectric-4913185-2.pom from repository central at http://repo1.maven.org/maven2\nError transferring file: Connection timed out: connect\n[WARNING] Unable to get resource \'org.robolectric:android-all:pom:9-robolectric-4913185-2\' from repository central (http://repo1.maven.org/maven2): Error transferring file: Connection timed out: connect\nDownloading: org/robolectric/android-all/9-robolectric-4913185-2/android-all-9-robolectric-4913185-2.jar from repository sonatype at https://oss.sonatype.org/content/groups/public/\nError transferring file: Connection timed out: connect\n[WARNING] Unable to get resource \'org.robolectric:android-all:jar:9-robolectric-4913185-2\' from repository sonatype (https://oss.sonatype.org/content/groups/public/): Error transferring file: Connection timed out: connect\nDownloading: org/robolectric/android-all/9-robolectric-4913185-2/android-all-9-robolectric-4913185-2.jar from repository central at http://repo1.maven.org/maven2\nError transferring file: Connection timed out: connect\n[WARNING] Unable to get resource \'org.robolectric:android-all:jar:9-robolectric-4913185-2\' from repository central (http://repo1.maven.org/maven2): Error transferring file: Connection timed out: connect\n\n\nUnable to resolve artifact: Missing:\n----------\n1) org.robolectric:android-all:jar:9-robolectric-4913185-2\n\n  Try downloading the file manually from the project website.\n\n  Then, install it using the command: \n      mvn install:install-file -DgroupId=org.robolectric -DartifactId=android-all -Dversion=9-robolectric-4913185-2 -Dpackaging=jar -Dfile=/path/to/file\n\n  Alternatively, if you host your own repository you can deploy the file there: \n      mvn deploy:deploy-file -DgroupId=org.robolectric -DartifactId=android-all -Dversion=9-robolectric-4913185-2 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]\n\n  Path to dependency: \n    1) org.apache.maven:super-pom:pom:2.0\n    2) org.robolectric:android-all:jar:9-robolectric-4913185-2\n\n----------\n1 required artifact is missing.\n\nfor artifact: \n  org.apache.maven:super-pom:pom:2.0\n\nfrom the specified remote repositories:\n  central (http://repo1.maven.org/maven2),\n  sonatype (https://oss.sonatype.org/content/groups/public/)\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的应用程序的“build.gradle”

\n\n
apply plugin: \'com.android.application\'\napply plugin: \'kotlin-android\'\napply plugin: \'kotlin-android-extensions\'\napply plugin: \'kotlin-kapt\'\n\nandroid {\n    compileSdkVersion 28\n\n    defaultConfig {\n        minSdkVersion 21\n        targetSdkVersion 28\n        versionCode 1\n        versionName "1.0"\n        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"\n    }\n\n    buildTypes {\n        release {\n            minifyEnabled false\n            proguardFiles getDefaultProguardFile(\'proguard-android-optimize.txt\'), \'proguard-rules.pro\'\n        }\n    }\n\n    kotlinOptions {\n        jvmTarget = "1.8"\n    }\n\n    compileOptions {\n        sourceCompatibility JavaVersion.VERSION_1_8\n        targetCompatibility JavaVersion.VERSION_1_8\n    }\n\n    testOptions {\n        unitTests {\n            includeAndroidResources = true\n            returnDefaultValues = true\n            all {\n                systemProperty \'robolectric.dependency.repo.url\', \'https://repo1.maven.org/maven2/\'\n            }\n        }\n    }\n}\n\ndependencies {\n    // == APP DEPENDENCIES == \n    // excluded as not applicable for this question\n\n    // == TEST DEPENDENCIES ==      \n    testImplementation \'org.mockito:mockito-inline:2.25.0\'\n    testImplementation \'io.kotlintest:kotlintest-runner-junit5:3.3.2\'\n    testImplementation \'junit:junit:4.12\'\n    testImplementation \'androidx.test:runner:1.1.0-alpha4\'\n    testImplementation "androidx.test.ext:junit-ktx:1.1.1"\n    testImplementation "androidx.test:core-ktx:1.2.0"\n    testImplementation "org.robolectric:robolectric:4.3.1"\n    testImplementation "androidx.arch.core:core-testing:2.1.0"\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的测试

\n\n
@RunWith(RobolectricTestRunner::class)\nclass MyTest {\n    @Rule @JvmField\n    val instantExecutorRule = InstantTaskExecutorRule()\n\n    @Before\n    @Throws(Exception::class)\n    fun setUp() {\n        // set up all my mock objects\n    }\n\n    @Test\n    fun test1() {\n       ....\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

在 2 个存储库 URL 中找不到 robolectric

\n\n\n\n

尝试修复 1:代理

\n\n

一个答案说这个问题是代理问题。我在“gradle.properties”中设置了代理,并且没有其他依赖项存在下载问题。

\n\n
systemProp.http.proxyHost=myproxy-server.com\nsystemProp.https.proxyPort=80\nsystemProp.https.proxyHost=myproxy-server.com\nsystemProp.http.proxyPort=80\n
Run Code Online (Sandbox Code Playgroud)\n\n

尝试修复 2:build.gradle 告诉它使用 Maven 存储库

\n\n

一个答案说要指定用于 robolectric 下载的 Maven 存储库。但这没有什么区别。

\n\n
testOptions {\n    unitTests {\n        all {\n            systemProperty \'robolectric.dependency.repo.url\', \'https://repo1.maven.org/maven2/\'\n        }\n    }\n
Run Code Online (Sandbox Code Playgroud)\n\n

尝试修复 3:在我的存储库中包含“mavenCental()”

\n\n

我尝试将“mavenCentral()”添加到我的“build.gradle”文件中,但没有解决问题。

\n\n
buildscript {\n    ext.kotlin_version = \'1.3.31\'\n\n    repositories {\n        google()\n        jcenter()\n        mavenCentral()\n    }\n    dependencies {\n        classpath \'com.android.tools.build:gradle:3.5.1\'\n        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"\n        classpath \'org.robolectric:robolectric-gradle-plugin:0.11.+\'\n    }\n}\n\nallprojects {\n    repositories {\n        google()\n        jcenter()\n        mavenCentral()\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

建议的解决方案“enableUnitTestBinaryResources”

\n\n

另一篇文章说将“android.enableUnitTestBinaryResources=true”添加到gradle.properties。

\n\n

robolectric 说明说,Android Studio 3.3+ 不需要此配置,我使用的是 AS 3.6。

\n

Bil*_*ote 5

我尝试了你尝试过的一切。这些事情都没有为我解决问题——或者至少在我执行之前没有解决这个问题

rm -rf ~/.m2删除我的本地 Maven 缓存。