我正在尝试为我的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() {
....
}
}
在两个存储库URL中找不到robolectric
我尝试访问“ https://oss.sonatype.org/content/groups/public/”,但收到错误消息“目录列表被禁止”。当我从网址中删除结尾的/时,出现错误“找不到oss.sonatype.org页面”
转到“ https://repo1.maven.org/maven2”(必须是https而不是http),没有用于“ 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仓库
回答说是为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。
我正在尝试为我的Kotlin应用程序编写Robolectric测试,但由于无法下载Robolectric,因此无法运行我的测试。我已经搜索了StackOverflow和Google,但都没有...
我已断开与VPN的连接,并且robolectric下载成功。因此,问题一定是我的代理设置中的设置错误。