se2*_*2as 4 android robolectric kotlin
我正在尝试为我的 Kotlin 应用程序编写 Robolectric 测试,但无法运行我的测试,因为它无法下载 Robolectric。我在 StackOverflow 和 Google 上进行了搜索,但没有任何建议可以解决我的问题,因此我在这里提出问题。
\n\n我使用的是Android Studio 3.6
\n\n我看到错误
\n\nDownloading 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/)\nRun Code Online (Sandbox Code Playgroud)\n\n我的应用程序的“build.gradle”
\n\napply 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}\nRun 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}\nRun Code Online (Sandbox Code Playgroud)\n\n在 2 个存储库 URL 中找不到 robolectric
\n\n我尝试访问“ https://oss.sonatype.org/content/groups/public/ ”,但收到错误“目录列表被禁止”。当我从 URL 中删除尾随 / 时,出现错误“This oss.sonatype.org page can\xe2\x80\x99t be find”
转到“ https://repo1.maven.org/maven2 ”(必须是 https 而不是 http),“robolectric”没有子文件夹
尝试修复 1:代理
\n\n一个答案说这个问题是代理问题。我在“gradle.properties”中设置了代理,并且没有其他依赖项存在下载问题。
\n\nsystemProp.http.proxyHost=myproxy-server.com\nsystemProp.https.proxyPort=80\nsystemProp.https.proxyHost=myproxy-server.com\nsystemProp.http.proxyPort=80\nRun Code Online (Sandbox Code Playgroud)\n\n尝试修复 2:build.gradle 告诉它使用 Maven 存储库
\n\n一个答案说要指定用于 robolectric 下载的 Maven 存储库。但这没有什么区别。
\n\ntestOptions {\n unitTests {\n all {\n systemProperty \'robolectric.dependency.repo.url\', \'https://repo1.maven.org/maven2/\'\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n\n尝试修复 3:在我的存储库中包含“mavenCental()”
\n\n我尝试将“mavenCentral()”添加到我的“build.gradle”文件中,但没有解决问题。
\n\nbuildscript {\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}\nRun Code Online (Sandbox Code Playgroud)\n\n建议的解决方案“enableUnitTestBinaryResources”
\n\n另一篇文章说将“android.enableUnitTestBinaryResources=true”添加到gradle.properties。
\n\nrobolectric 说明说,Android Studio 3.3+ 不需要此配置,我使用的是 AS 3.6。
\n| 归档时间: |
|
| 查看次数: |
4420 次 |
| 最近记录: |