【发布时间】:2020-05-24 04:23:47
【问题描述】:
我想运行一个包含此代码的测试:
@Test
public void deleteItemWithKeyGreaterThenTest() {
SparseArray<String> sparseArray = new SparseArray<String>();
sparseArray.put(0, "zero");
但是当我运行测试时,我得到:
java.lang.RuntimeException: Method put in android.util.SparseArray not mocked.
我已经阅读了这个Unit testing ArrayMap throws Method put in android.util.ArrayMap not mocked,但它没有提供明确的解决方案。
你能告诉我我做错了什么吗?
提前谢谢你!
编辑:完整代码
import org.junit.Test;
import static org.junit.Assert.*;
import android.util.SparseArray;
@Test
public void deleteItemWithKeyGreaterThenTest() {
SparseArray<String> sparseArray = new SparseArray<String>();
sparseArray.put(0, "zero");
assertEquals(sparseArray.size(), 1);
}
两个都试过了:
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.navmaze"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests.returnDefaultValues = true
unitTests.includeAndroidResources = true
}
}
和
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.navmaze"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
这些是我的依赖项:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
testImplementation 'androidx.test:core:1.2.0'
testImplementation 'org.mockito:mockito-core:1.10.19'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'org.jetbrains:annotations-java5:15.0'
}
【问题讨论】:
-
你没有做错什么,android开发是一团糟。是否按照此处的建议清除您的 Maven 存储库:github.com/robolectric/robolectric/issues/4740?
-
我没有任何要清理的 Maven 存储库...
-
好吧!我想不是这样的。 =/
-
你能发布你的依赖部分/模拟配置吗?
-
我添加了一些信息。
标签: android-studio junit4