【问题标题】:Robolectric fails to download connection time outRobolectric 无法下载连接超时
【发布时间】:2020-03-16 17:39:24
【问题描述】:

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

我使用的是 Android Studio 3.6

我看到了错误

Downloading from maven 
Downloading: 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/
Error transferring file: Connection timed out: connect
[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
Downloading: org/robolectric/android-all/9-robolectric-4913185-2/android-all-9-robolectric-4913185-2.pom from repository central at http://repo1.maven.org/maven2
Error transferring file: Connection timed out: connect
[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
Downloading: 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/
Error transferring file: Connection timed out: connect
[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
Downloading: org/robolectric/android-all/9-robolectric-4913185-2/android-all-9-robolectric-4913185-2.jar from repository central at http://repo1.maven.org/maven2
Error transferring file: Connection timed out: connect
[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


Unable to resolve artifact: Missing:
----------
1) org.robolectric:android-all:jar:9-robolectric-4913185-2

  Try downloading the file manually from the project website.

  Then, install it using the command: 
      mvn install:install-file -DgroupId=org.robolectric -DartifactId=android-all -Dversion=9-robolectric-4913185-2 -Dpackaging=jar -Dfile=/path/to/file

  Alternatively, if you host your own repository you can deploy the file there: 
      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]

  Path to dependency: 
    1) org.apache.maven:super-pom:pom:2.0
    2) org.robolectric:android-all:jar:9-robolectric-4913185-2

----------
1 required artifact is missing.

for artifact: 
  org.apache.maven:super-pom:pom:2.0

from the specified remote repositories:
  central (http://repo1.maven.org/maven2),
  sonatype (https://oss.sonatype.org/content/groups/public/)

我的应用的“build.gradle”

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    testOptions {
        unitTests {
            includeAndroidResources = true
            returnDefaultValues = true
            all {
                systemProperty 'robolectric.dependency.repo.url', 'https://repo1.maven.org/maven2/'
            }
        }
    }
}

dependencies {
    // == APP DEPENDENCIES == 
    // excluded as not applicable for this question

    // == TEST DEPENDENCIES ==      
    testImplementation 'org.mockito:mockito-inline:2.25.0'
    testImplementation 'io.kotlintest:kotlintest-runner-junit5:3.3.2'
    testImplementation 'junit:junit:4.12'
    testImplementation 'androidx.test:runner:1.1.0-alpha4'
    testImplementation "androidx.test.ext:junit-ktx:1.1.1"
    testImplementation "androidx.test:core-ktx:1.2.0"
    testImplementation "org.robolectric:robolectric:4.3.1"
    testImplementation "androidx.arch.core:core-testing:2.1.0"

}

我的测试

@RunWith(RobolectricTestRunner::class)
class MyTest {
    @Rule @JvmField
    val instantExecutorRule = InstantTaskExecutorRule()

    @Before
    @Throws(Exception::class)
    fun setUp() {
        // set up all my mock objects
    }

    @Test
    fun test1() {
       ....
    }
}

在 2 个 repos URLS 中找不到 robolectric

尝试修复 1:代理

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

systemProp.http.proxyHost=myproxy-server.com
systemProp.https.proxyPort=80
systemProp.https.proxyHost=myproxy-server.com
systemProp.http.proxyPort=80

尝试修复 2:build.gradle 告诉它使用 maven repos

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

testOptions {
    unitTests {
        all {
            systemProperty 'robolectric.dependency.repo.url', 'https://repo1.maven.org/maven2/'
        }
    }

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

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

buildscript {
    ext.kotlin_version = '1.3.31'

    repositories {
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'org.robolectric:robolectric-gradle-plugin:0.11.+'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
}

建议的解决方案“enableUnitTestBinaryResources”

另一个帖子说要在 gradle.properties 中添加“android.enableUnitTestBinaryResources=true”。

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

【问题讨论】:

  • 您找到解决方案了吗?

标签: android kotlin robolectric


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我从 VPN 断开连接,robolectric 下载正常。因此,问题一定是我的代理设置中有错误的设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-09
      • 2019-09-30
      • 1970-01-01
      • 2011-10-12
      • 2023-03-27
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多